PNG 半透明在 IE6 裡會變出一層灰底,這是老前端的共同記憶。當年我通常直接改用 GIF 閃過去,但其實是有解的。核心答案是微軟自創的 AlphaImageLoader 濾鏡。
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(
enabled=bEnabled, sizingMethod=sSize, src=sURL
)
參數說明:
crop 是裁切以符合容器、image(預設)是調整容器以符合圖片、scale 是縮放圖片填滿容器。這個濾鏡的優點是:即使 PNG 的某個區域完全透明,你仍然可以選取到它下方覆蓋的文字。
.qq {
height: 90px;
width: 90px;
background-image: url(icon_home.png) !important; /* FF、IE7 */
background-repeat: no-repeat;
_filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='icon_home.png'); /* 只有 IE6 */
_background-image: none; /* 只有 IE6 */
}
底線前綴(_)是 IE6 專屬的 hack,IE7 以上與 Firefox 會直接忽略,因此不必額外寫條件註解。
.mypng img {
azimuth: expression(
this.pngSet ? this.pngSet = true :
(this.nodeName == "IMG" && this.src.toLowerCase().indexOf('.png') > -1 ?
(this.runtimeStyle.backgroundImage = "none",
this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src + "', sizingMethod='image')",
this.src = "transparent.gif") :
(this.origBg = this.origBg ? this.origBg : this.currentStyle.backgroundImage.toString().replace('url("','').replace('")',''),
this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.origBg + "', sizingMethod='crop')",
this.cssText = this.cssText)) , this.pngSet = true);
}
這招只對直接插入的 img 有效,對背景圖無效。另外 expression 會持續重算, page 上圖片一多就會明顯變慢,切勿大量使用。
function correctPNG() {
var arVersion = navigator.appVersion.split("MSIE");
var version = parseFloat(arVersion[1]);
if ((version >= 5.5) && (document.body.filters)) {
for (var j = 0; j < document.images.length; j++) {
var img = document.images[j];
var imgName = img.src.toUpperCase();
if (imgName.substring(imgName.length - 3) == "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;
img.outerHTML = "<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>";
j = j - 1; // outerHTML 替換後集合會變動,索引要補回來
}
}
}
}
window.attachEvent("onload", correctPNG);
同樣只處理 img,背景圖無效。注意它把 img 換成了 span,因此原本掛在 img 上的事件會一併遺失。
function fixPNG(myImage) {
var arVersion = navigator.appVersion.split("MSIE");
var version = parseFloat(arVersion[1]);
if ((version >= 5.5) && (version < 7) && (document.body.filters)) {
var imgID = myImage.id ? "id='" + myImage.id + "' " : "";
var imgClass = myImage.className ? "class='" + myImage.className + "' " : "";
var imgTitle = myImage.title ? "title='" + myImage.title + "' "
: "title='" + myImage.alt + "' ";
var imgStyle = "display:inline-block;" + myImage.style.cssText;
myImage.outerHTML = "<span " + imgID + imgClass + imgTitle +
" style=\"width:" + myImage.width + "px; height:" + myImage.height + "px;" +
imgStyle + ";filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" +
"(src='" + myImage.src + "', sizingMethod='scale');\"></span>";
}
}
用法是掛在 onload 上,只處理你指定的那張圖,效能開銷最小:
<img src="logo.png" width="328" height="325" border="0" onload="fixPNG(this)" />

圖片數量少的版面用方法四最穩妥;大量裝飾性小圖用方法一搭配 class;方法二與三雖然省事,但 expression 與 outerHTML 替換都有明顯副作用。當然,如果專案已經可以不支援 IE6,這整篇就當作前端考古資料來讀即可。