兼容各瀏覽器的CSS倒影效果
無需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的實現 */
-moz-transform: scaleY(-1); /* firefox 的實現 */
-ms-transform: scaleY(-1); /* IE 的實現 */
-o-transform: scaleY(-1); /* Opera的實現 */
HTML
<div class="wrap"> <div class="image"><img src="1.jpg" /></div> <div class="down"> <div class="reflection"></div> <div class="overlay"></div> </div> </div>
CSS
body{background:#000;color:#f00} .wrap{position:relative;} .image{margin-bottom:3px;} .down{position: relative;} .reflection{width:421px;height:180px;background:url(1.jpg) bottom center no-repeat; -webkit-transform: scaleY(-1); -moz-transform: scaleY(-1); -ms-transform: scaleY(-1); -o-transform: scaleY(-1); transform: scaleY(-1); opacity:0.5; filter:alpha(opacity='50'); } .overlay{position: relative;width:421px;height:180px;bottom:149px; background-image: -moz-linear-gradient(center bottom, rgb(0,0,0) 20%, rgba(0,0,0,0) 90%); background-image: -o-linear-gradient(rgba(0,0,0,0) 10%, rgb(0,0,0) 30%); background-image: -webkit-gradient(linear, left bottom, left top, color-stop(0.20, rgb(0,0,0)), color-stop(0.90, rgba(0,0,0,0))); filter: progid:DXImageTransform.Microsoft.Gradient(gradientType=0,startColor=0, EndColorStr=#000000); }
在倒轉的圖片上面還加了一個DIV.overlay層,做出漸變的效果,使倒影看起來更真實。
兼容舊瀏覽器的實現
考慮到還有相當多的人在使用舊版瀏覽器,程序員絞盡腦汁為這部分人做兼容。這里指的是IE7/IE8。IE6怎么辦?提示用戶升級瀏覽器吧。
舊IE不支持transform屬性,可以使用濾鏡 filter:flipv 來生成圖片倒轉,但會跟IE9的transform沖突。所以要用到各種 hack 來解決。修改后的CSS如下,添加了IE9 hack,覆蓋掉上面的filter:flipv的屬性。
body{background:#000;color:#f00} .wrap{position:relative;} .image{margin-bottom:3px;} .down{position: relative;} .reflection{width:421px;height:180px;background:url(1.jpg) bottom center no-repeat; -webkit-transform: scaleY(-1); -moz-transform: scaleY(-1); -ms-transform: scaleY(-1); -o-transform: scaleY(-1); transform: scaleY(-1); opacity:0.5; filter:flipv alpha(opacity='50'); /*ALL IE*/ } @media all and (min-width:0) { .reflection{filter:alpha(opacity='50') \0/;} /*IE9*/ } .overlay{position: relative;width:421px;height:180px;bottom:149px; background-image: -moz-linear-gradient(center bottom, rgb(0,0,0) 20%, rgba(0,0,0,0) 90%); background-image: -o-linear-gradient(rgba(0,0,0,0) 10%, rgb(0,0,0) 30%); background-image: -webkit-gradient(linear, left bottom, left top, color-stop(0.20, rgb(0,0,0)), color-stop(0.90, rgba(0,0,0,0))); filter: progid:DXImageTransform.Microsoft.Gradient(gradientType=0,startColor=0, EndColorStr=#000000); }
運行一下,在各版本的瀏覽器能看到最終的效果了。
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!