CSS解決IE6下PNG圖片背景不透明的問題

來源:互聯網
上載者:User
 半透明效果有時候會給頁面增加不少色彩,特別是Vista盛行之後,半透明效果更加受推崇。在諸多可用於Web瀏覽的圖片格式中,只有PNG格式和Gif格式可以實現半透明效果,不過Gif格式的半透明效果很有限,而且會大範圍失真,所以目前最流行的方式就是使用PNG格式圖片。不過可惜的是,在Internet Explorer 6及以下版本中都不支援PNG半透明效果(至少是不直接支援)。不過幸好Microsoft在這些瀏覽器中內建了其他的功能,可以協助我們來實這種半透明的效果。

一、我們看一下普通情況下在現代瀏覽器中半透明效果的實現
    首先要有一張半透明的PNG格式圖片,這個製作很簡單,在Photoshop中建立一個檔案,在該檔案中建立一個圖層填充白色,然後調節透明度,移除背景,儲存為PNG格式即可。你也可以下載這個PNG檔案使用。我們要做的工作很簡單:只要把這個PNG圖片指定為某個<div>的背景即可。例如可以使用下面的CSS規則:

程式碼  body {  
background:black url(bg.jpg) no-repeat 0 0;  
text-align:center;  
}  
  div {  
width:80%;  
margin:0 auto;  
text-align:left;  
padding:7px;  
background-image:url(tran.png);  
border:3px solid #fff;  
}  

二、在Internet Explorer 6中的實現
    Internet Explorer中提供了提供了專屬的濾鏡效果,他通過filter:progid:DXImageTransform.Microsoft.AlphaImageLoader實現,現在是關於這個屬性的有關知識:

程式碼enabled  : 可選項。布爾值(Boolean)。設定或檢索濾鏡是否啟用。
true | false true  : 預設值。濾鏡啟用。
false  : 濾鏡被禁止。

sizingMethod  : 可選項。字串(String)。設定或檢索濾鏡作用的對象的圖片在對象容器邊界內的顯示方式。
crop  : 剪下圖片以適應對象尺寸。
image  : 預設值。增大或減小對象的尺寸邊界以適應圖片的尺寸。
scale  : 縮放圖片以適應對象的尺寸邊界。
src  : 必選項。字串(String)。使用絕對或相對 url 地址指定背景映像。假如忽略此參數,濾鏡將不會作用。

特性:
Enabled  : 可讀寫。布爾值(Boolean)。參閱 enabled 屬性。
sizingMethod  : 可讀寫。字串(String)。參閱 sizingMethod 屬性。
src  : 可讀寫。字串(String)。參閱 src 屬性。

說明:
在對象容器邊界內,在對象的背景和內容之間顯示一張圖片。並提供對此圖片的剪下和改變尺寸的操作。如果載入的是PNG(Portable Network Graphics)格式,則0%-100%的透明度也被提供。
PNG(Portable Network Graphics)格式的圖片的透明度不妨礙你選擇文本。也就是說,你可以選擇顯示在PNG(Portable Network Graphics)格式的圖片完全透明地區後面的內容。

因此在Internet Explorer 6 中我們還要加上現在這段話:

程式碼* html div {  
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src="tran.png");  
background:none;  
}

注意:這裡我們使用了 “*”的CSS hack,這個CSS Hack是Internet Explorer 6專屬的,在Ineternet Explorer 6的DOM結構中,預設HTML的父節點為*,而在標準的DOM結構中HTML就是根節點。所以上面的CSS 規則只有Internet Explorer 6 認識。

這樣,我們在Internet Explorer 6、7、Firefox、Opera等最常用的瀏覽器的都實現了半透明效果了。

三、其它情況
    但是上面都用PNG做背景的情況,有時候我們還會用PNG來做遮罩,比如下面這張Logo圖片,

我們可以使用PNG遮罩來達到漸層效果:

那麼我們可以使用下面的方法來實現
HTML程式碼片段

程式碼<div>  
      <img src="logo.jpg" alt="圖片說明" />  
      <span></span>  
</div>  

注意:這種寫法完全是為了迎合Internet Explorer 6,<div>容器用來協助內部元素定位,<span>用來覆蓋在<img>標籤的上面達到半透明效果。下面我們只需要設定一下它們的位置就好了:

程式碼  div { position:relative;}  
  span {  
    filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='filter.png');  
    float:left;  
    width:200px;  
    height:100px;  
    position:absolute;  
    top:0;  
    left:0;  
  }

不過,在Firefox中並沒有filter這個屬性,因此我們需要單獨為Firefox中定製一個樣式:

程式碼div > span {  
    background:url(filter.png);  
}  

這樣我們就可以使用遮罩來實現半透明效果了

不過如果有太多的地方需要實現這樣的遮罩效果的話,上面的處理方式還不是最好的,首先它有冗餘的HTML標籤,此外還使用了絕對位置和相對定位。如果我們把上面的代碼進行封閉效果會更佳。這裡我們可以使用下面這段JS代碼進行封閉

程式碼<!--[if lt IE 7]>  
<script language="JavaScript">  
function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.  
{  
var arVersion = navigator.appVersion.split("MSIE")  
var version = parseFloat(arVersion[1])  
if ((version >= 5.5) && (document.body.filters))  
{  
for(var i=0; i<document.images.length; i++)  
{  
var img = document.images[i]  
var imgName = img.src.toUpperCase()  
if (imgName.substring(imgName.length-3, imgName.length) == "PNG")  
{  
var imgID = (img.id) ? "id='" + img.id + "' " : ""  
var imgClass = (img.className) ? "class='" + img.className + "' " : ""  
var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "  
var imgStyle = "display:inline-block;" + img.style.cssText  
if (img.align == "left") imgStyle = "float:left;" + imgStyle  
if (img.align == "right") imgStyle = "float:right;" + imgStyle  
if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle  
var strNewHTML = "<span " + imgID + imgClass + imgTitle  
+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"  
+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"  
+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"  
img.outerHTML = strNewHTML  
i = i-1  
}  
}  
}  
}  
window.attachEvent("onload", correctPNG);  
</script>  
<![endif]-->  

這段代碼只在Internet Explorer 6中才會被運行。
下面我們可以像在Internet Explorer 7 和Firefox中一樣寫代碼:

程式碼<div><img src="filter.png" alt="圖片說明" /></div>
程式碼div {  
      background:url(logo.jpg) no-repeat;  
}  

解決了PNG跨瀏覽器的問題之後,我們可以利用它來實現更加複雜和更加絢麗的頁面效果。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.