首頁 > web前端 > css教學 > 主體

CSS3 Notes: -webkit-box-reflect實現倒影

高洛峰
發布: 2017-02-28 14:03:28
原創
1779 人瀏覽過

平常我們要實現倒影的效果,一般的做法是使用多個DOM元素絕對定位+scale(負-1)或rotate。這種方法的缺點是佔據空間以及DOM元素太多。

在使用webkit核心的瀏覽器中(chrome,safari,行動裝置瀏覽器),可以使用-webkit-box-reflect屬性來實現倒影,語法如下所示

[ above | below | right | left ]? ? ?

該值包涵了三部分:方位+偏移量+遮罩層

方位是必不可少的;在使用遮罩層的時候,偏移量是必不可少的,如沒有則用零代替

! ! !重要:遮罩層的效果與顏色無關,例如使用漸層顏色做遮罩,都是實色則透明,透明則暴漏原始顏色

使用範例如下所示:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <style type="text/csss">
    .box{ width:200px; height:200px; margin-bottom:20px;transform:scale(-1,1); background-image:linear-gradient(90deg,red,yellow);-webkit-box-reflect:below 10px linear-gradient(180deg,transparent,#000); }
  </style>
</head>
<body>
  <p class="box"></p>
</body>
</html>
登入後複製

效果如下:

CSS3 Notes: -webkit-box-reflect实现倒影

#如果需要在firefox中實作類似效果,可以使用-moz-element( )函數來實現,但是在旋轉下效果差異較大,如下所示。

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <style type="text/css">
    .box{ width:200px; height:200px; margin:100px 0 0 100px; }
    .box1{ background-image:linear-gradient(180deg,red,yellow); transform:scale(1,-1) rotate(45deg)}
    .box2{ background-image:-moz-element(#box1); }
  </style>
</head>
<body>
  <p class="box box1" id="box1"></p>
  <p class="box box2" id="box2"></p>
</body>
</html>
登入後複製

CSS3 Notes: -webkit-box-reflect实现倒影

CSS3 Notes: -webkit-box-reflect实现倒影

#如果要相容IE瀏覽器也可以使用SVG或canvas來做,SVG主要利用pattern+mask+linearGradient+scale來做,canvas使用scale+globalCompositeOperation。

SVG範例部分程式碼如下:


<svg width="200" height="200">
    <defs>
      <linearGradient id="a" x1="0" y1="0" x2="0" y2="1">
        <stop offset="0%" style="stop-color:yellow"/>
        <stop offset="100%" style="stop-color:red"/>
      </linearGradient>
      <linearGradient id="b" x1="0" y1="0" x2="0" y2="100%">
        <stop offset="0%" style="stop-color:rgba(255,255,255,0)"/>
        <stop offset="100%" style="stop-color:rgba(255,255,255,1)"/>
      </linearGradient>
      <mask id="c" x="0" y="0" width="1" height="1">
        <rect x="0" y="0" width="100%" height="100%" style="fill:url(#b)" />
      </mask>
    </defs>
    <rect x="0" y="0" width="200" height="200" style="fill:url(#a);" mask="url(#c)">
</svg>
登入後複製


#canvas範例部分程式碼如下########
var canvas = document.getElementById(&#39;canvas&#39;),
    ctx = canvas.getContext(&#39;2d&#39;);

var linearGradient1 = ctx.createLinearGradient(0,0,0,200);
linearGradient1.addColorStop(0,"red");
linearGradient1.addColorStop(1,"yellow");

var linearGradient2 = ctx.createLinearGradient(0,0,0,200);
linearGradient2.addColorStop(0,"transparent");
linearGradient2.addColorStop(1,"#ffffff");

ctx.fillStyle = linearGradient1;
ctx.fillRect(0,0,200,200);

ctx.globalCompositeOperation = &#39;destination-out&#39;;

ctx.fillStyle = linearGradient2;
ctx.fillRect(0,0,200,200);
登入後複製
##### #####以上便是倒影實現的各種方法,對比之下用css3的-webkit-box-reflect實現最簡單效果也好。希望對大家的學習有幫助,也希望大家多多支持PHP中文網。 ######更多CSS3 Notes: -webkit-box-reflect實現倒影相關文章請關注PHP中文網! ################
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!