標籤:
在css3之前要想做背景色漸層就只能採用添加背景圖片的方法,但是隨著css3:linear-gradient屬性的出現,就可以避免使用添加背景圖片的方法,從而最佳化了效能。但是inear-gradient屬性在ie9以下是無效的,但是可以採用ie濾鏡的方法。
比如:黑色漸層到白色,代碼如下:
.gradient{ background: -moz-linear-gradient(top, #000000 0%, #ffffff 100%); background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#000000), color-stop(100%,#ffffff)); background: -webkit-linear-gradient(top, #000000 0%,#ffffff 100%); background: -o-linear-gradient(top, #000000 0%,#ffffff 100%); background: -ms-linear-gradient(top, #000000 0%,#ffffff 100%); background: linear-gradient(to bottom, #000000 0%,#ffffff 100%); }
ie 濾鏡:filter
linear-gradient 在 ie9 以下是不支援的,所以對於 ie6 - ie8 我們可以使用濾鏡來解決,代碼如下:
.gradient{ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr=‘#000000‘, endColorstr=‘#ffffff‘,GradientType=0 ); }
由於 filter 是 ie 的私人屬性,所以我們需要針對 ie9 單獨處理濾鏡效果,代碼如下:
:root {filter:none;}
綜上所述,線性漸層的相容寫法如下:
.gradient{ background: #000000; background: -moz-linear-gradient(top, #000000 0%, #ffffff 100%); background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#000000), color-stop(100%,#ffffff)); background: -webkit-linear-gradient(top, #000000 0%,#ffffff 100%); background: -o-linear-gradient(top, #000000 0%,#ffffff 100%); background: -ms-linear-gradient(top, #000000 0%,#ffffff 100%); background: linear-gradient(to bottom, #000000 0%,#ffffff 100%); filter: progid:DXImageTransform.Microsoft.gradient( startColorstr=‘#000000‘, endColorstr=‘#ffffff‘,GradientType=0 ); } :root .gradient{filter:none;}
ie相容CSS3漸層寫法