欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

兼容各浏览器的CSS倒影效果

shiping1 的头像
无需flash,完全用css就可以做出超炫的图片倒影效果。网上流传很多种版本,经过本人的一番研究,做成能够兼容firefox、chrome、IE等各主流浏览器的版本,跟大家分享一下。最终完成的效果

 
新浏览器的实现
指的是firefox、chrome和IE9。新浏览器都支持CSS3新添的transform属性,所以实现倒影效果非常简单。从下面的代码看到,各家浏览器对transform的实现有点不同
     -webkit-transform: scaleY(-1);     /* webkit内核浏览器的实现,例如safari */
     -moz-transform: scaleY(-1);     /* firefox 的实现 */
     -ms-transform: scaleY(-1);     /* IE 的实现 */
     -o-transform: scaleY(-1);     /* Opera的实现 */
 
HTML
[html] view plaincopy
 
  1. <div class="wrap">  
  2.      <div class="image"><img src="1.jpg" /></div>  
  3.      <div class="down">  
  4.           <div class="reflection"></div>  
  5.           <div class="overlay"></div>  
  6.      </div>  
  7. </div>  
 
CSS
  1. body{background:#000;color:#f00}  
  2. .wrap{position:relative;}  
  3. .image{margin-bottom:3px;}  
  4. .down{positionrelative;}  
  5. .reflection{width:421px;height:180px;background:url(1.jpg) bottom center no-repeat;  
  6.      -webkit-transform: scaleY(-1);  
  7.      -moz-transform: scaleY(-1);  
  8.      -ms-transform: scaleY(-1);  
  9.      -o-transform: scaleY(-1);  
  10.      transform: scaleY(-1);  
  11.      opacity:0.5;       
  12.      filter:alpha(opacity='50');  
  13.      }  
  14. .overlay{positionrelative;width:421px;height:180px;bottom:149px;  
  15.      background-image: -moz-linear-gradient(center bottomrgb(0,0,020%, rgba(0,0,0,090%);  
  16.      background-image: -o-linear-gradient(rgba(0,0,0,010%rgb(0,0,030%);  
  17.      background-image: -webkit-gradient(linear, left bottomleft top, color-stop(0.20rgb(0,0,0)), color-stop(0.90, rgba(0,0,0,0)));  
  18.      filter: progid:DXImageTransform.Microsoft.Gradient(gradientType=0,startColor=0, EndColorStr=#000000);  
  19. }  
 
在倒转的图片上面还加了一个DIV.overlay层,做出渐变的效果,使倒影看起来更真实。
 
兼容旧浏览器的实现
考虑到还有相当多的人在使用旧版浏览器,程序员绞尽脑汁为这部分人做兼容。这里指的是IE7/IE8。IE6怎么办?提示用户升级浏览器吧。
旧IE不支持transform属性,可以使用滤镜 filter:flipv 来生成图片倒转,但会跟IE9的transform冲突。所以要用到各种 hack 来解决。修改后的CSS如下,添加了IE9 hack,覆盖掉上面的filter:flipv的属性。
 
  1. body{background:#000;color:#f00}  
  2. .wrap{position:relative;}  
  3. .image{margin-bottom:3px;}  
  4. .down{positionrelative;}  
  5. .reflection{width:421px;height:180px;background:url(1.jpg) bottom center no-repeat;  
  6.      -webkit-transform: scaleY(-1);  
  7.      -moz-transform: scaleY(-1);  
  8.      -ms-transform: scaleY(-1);  
  9.      -o-transform: scaleY(-1);  
  10.      transform: scaleY(-1);  
  11.      opacity:0.5;       
  12.      filter:flipv alpha(opacity='50');     /*ALL IE*/  
  13.      }  
  14. @media all and (min-width:0) {  
  15.      .reflection{filter:alpha(opacity='50') \0/;} /*IE9*/  
  16. }  
  17. .overlay{positionrelative;width:421px;height:180px;bottom:149px;  
  18.      background-image: -moz-linear-gradient(center bottomrgb(0,0,020%, rgba(0,0,0,090%);  
  19.      background-image: -o-linear-gradient(rgba(0,0,0,010%rgb(0,0,030%);  
  20.      background-image: -webkit-gradient(linear, left bottomleft top, color-stop(0.20rgb(0,0,0)), color-stop(0.90, rgba(0,0,0,0)));  
  21.      filter: progid:DXImageTransform.Microsoft.Gradient(gradientType=0,startColor=0, EndColorStr=#000000);  
  22. }  
 
运行一下,在各版本的浏览器能看到最终的效果了。
普通分类: