CSS圖片倒影效果兼容firefox、IE等各主流瀏覽器
發(fā)布時間:2012-12-27 17:49:20 作者:佚名
我要評論

無需flash,完全用css就可以做出超炫的圖片倒影效果,大伙不要不信;網(wǎng)上流傳很多種版本,經(jīng)過本人的一番研究,做成能夠兼容firefox、chrome、IE等各主流瀏覽器的版本,跟大家分享一下
無需flash,完全用css就可以做出超炫的圖片倒影效果。網(wǎng)上流傳很多種版本,經(jīng)過本人的一番研究,做成能夠兼容firefox、chrome、IE等各主流瀏覽器的版本,跟大家分享一下。最終完成的效果

新瀏覽器的實現(xiàn)
指的是firefox、chrome和IE9。新瀏覽器都支持CSS3新添的transform屬性,所以實現(xiàn)倒影效果非常簡單。從下面的代碼看到,各家瀏覽器對transform的實現(xiàn)有點不同
-webkit-transform: scaleY(-1); /* webkit內(nèi)核瀏覽器的實現(xiàn),例如safari */
-moz-transform: scaleY(-1); /* firefox 的實現(xiàn) */
-ms-transform: scaleY(-1); /* IE 的實現(xiàn) */
-o-transform: scaleY(-1); /* Opera的實現(xiàn) */
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);
}
在倒轉(zhuǎn)的圖片上面還加了一個DIV.overlay層,做出漸變的效果,使倒影看起來更真實。
兼容舊瀏覽器的實現(xiàn)
考慮到還有相當(dāng)多的人在使用舊版瀏覽器,程序員絞盡腦汁為這部分人做兼容。這里指的是IE7/IE8。IE6怎么辦?提示用戶升級瀏覽器吧。
舊IE不支持transform屬性,可以使用濾鏡 filter:flipv 來生成圖片倒轉(zhuǎn),但會跟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);
}
運行一下,在各版本的瀏覽器能看到最終的效果了。

新瀏覽器的實現(xiàn)
指的是firefox、chrome和IE9。新瀏覽器都支持CSS3新添的transform屬性,所以實現(xiàn)倒影效果非常簡單。從下面的代碼看到,各家瀏覽器對transform的實現(xiàn)有點不同
-webkit-transform: scaleY(-1); /* webkit內(nèi)核瀏覽器的實現(xiàn),例如safari */
-moz-transform: scaleY(-1); /* firefox 的實現(xiàn) */
-ms-transform: scaleY(-1); /* IE 的實現(xiàn) */
-o-transform: scaleY(-1); /* Opera的實現(xiàn) */
HTML
復(fù)制代碼
代碼如下:<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
復(fù)制代碼
代碼如下: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);
}
在倒轉(zhuǎn)的圖片上面還加了一個DIV.overlay層,做出漸變的效果,使倒影看起來更真實。
兼容舊瀏覽器的實現(xiàn)
考慮到還有相當(dāng)多的人在使用舊版瀏覽器,程序員絞盡腦汁為這部分人做兼容。這里指的是IE7/IE8。IE6怎么辦?提示用戶升級瀏覽器吧。
舊IE不支持transform屬性,可以使用濾鏡 filter:flipv 來生成圖片倒轉(zhuǎn),但會跟IE9的transform沖突。所以要用到各種 hack 來解決。修改后的CSS如下,添加了IE9 hack,覆蓋掉上面的filter:flipv的屬性。
復(fù)制代碼
代碼如下: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);
}
運行一下,在各版本的瀏覽器能看到最終的效果了。
相關(guān)文章
css圖片垂直居中 css中如何實現(xiàn)圖片垂直居中
使用純CSS實現(xiàn)未知尺寸的圖片(但高寬都小于200px)在200px的正方形容器中水平和垂直居中,下面是一個權(quán)衡的相對結(jié)構(gòu)干凈,CSS簡單的解決方法2013-04-17- 瀏覽網(wǎng)頁時經(jīng)常會碰到圖文并茂的文章,如果圖片的尺寸過大,常會把頁面結(jié)構(gòu)撐得變形直接影響了界面美觀度,一直用DIV+CSS的方式來制作頁面,發(fā)現(xiàn)用CSS來得更方便,相信處理2013-03-28
一個新的CSS圖片替換的技巧(背景顯示與文本移離屏)告別9999px
-9999 px的形象替代技術(shù)已經(jīng)流行了一個十年最好的部分。一項新技術(shù)已被發(fā)現(xiàn),可以提高性能,因為瀏覽器的不再畫一個9999 px箱在幕后2012-12-30純CSS圖片預(yù)加載實例 擺脫Javascript預(yù)載的束縛
有很多種方法來實現(xiàn)圖片的預(yù)加載,通常大部分使用Javascript讓事情滾動。不要再受Javascript預(yù)載的束縛了吧,用CSS你就可以毫不麻煩的預(yù)載你的圖片,需要的朋友可以了解下2012-12-19css實現(xiàn)圖片圓角 兼容所有瀏覽器實現(xiàn)代碼
今天處理了一個頁面刷新隨機(jī)顯示圖片的功能,發(fā)現(xiàn)直角太丑,想實現(xiàn)圖片圓角,兼容所有瀏覽器,于是網(wǎng)上搜集整理了一下,拿出來和大家分享2012-12-06- 看到問此問題的很多,所以花點時間整理下,歡迎大家提意見,做補充修改,謝謝2012-01-21
- CSS圖片垂直居中問題,困擾了我許久,今天終于可以總結(jié)下來了2012-01-21
- CSS圖片提取工具,是我為了方便制作網(wǎng)站而編寫的一個小巧的工具。它功能單一,但可以省去不少的麻煩2011-10-09
華華CSS圖片下載器 v1.1 網(wǎng)站必備的小工具
如今DIV+CSS的網(wǎng)站設(shè)計成為主流,越來越的圖片不直接插在HTML中而選擇用CSS來展示了,這為仿站帶來很大的困難。2011-09-20- 動畫效果的CSS圖片導(dǎo)航菜單特效,鼠標(biāo)放到圖片上,會出現(xiàn)菜單提示,因為是用CSS實現(xiàn)的動畫效果,所以流暢度當(dāng)然沒有JavaScript和Flash的效果好,僅供參考。2010-11-18