当前位置: 移动技术网 > IT编程>网页制作>CSS > CSS3 Notes: -webkit-box-reflect实现倒影的实例

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

2019年07月25日  | 移动技术网IT编程  | 我要评论

平常我们要实现倒影的效果,一般的做法是使用多个dom元素绝对定位+scale(负-1)或者rotate。这种方法的缺点是占据空间以及dom元素过多。

在使用webkit内核的浏览器中(chrome,safari,移动端浏览器),可以使用-webkit-box-reflect属性来实现倒影,语法如下所示

[ above | below | right | left ]? <length>? <image>?

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

方位是必不可少的;在使用遮罩层的时候,偏移量是不可少的,如没有则用零代替

!!!重要:遮罩层的效果与颜色无关,例如使用渐变颜色做遮罩,都是实色则透明,透明则暴漏原始颜色

使用示例如下所示:

<!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>
  <div class="box"></div>
</body>
</html>

效果如下:

如果需要在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>
  <div class="box box1" id="box1"></div>
  <div class="box box2" id="box2"></div>
</body>
</html>

在chrome下使用-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('canvas'),
    ctx = canvas.getcontext('2d');

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 = 'destination-out';

ctx.fillstyle = lineargradient2;
ctx.fillrect(0,0,200,200);

以上便是倒影实现的各种方法,对比之下用css3的-webkit-box-reflect实现最简单效果也好。希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网