png圖檔有很好的品質,陰影效果也不會有雜邊,很流暢。如果插入網頁的話可以給網站增色不少。更重要的是,在不增加圖檔容量大小的情況下,提高了頁面圖檔的品質。對于有複雜背景,如在有顔色過度背景上插入不規則邊框的圖檔帶來極大很便利!
但目前IE下對于插入的透明背景的png圖檔是不能正常顯示的。IE會自動給png格式的圖檔加上一個灰色背景。解決這個問題有兩種方法。
第一種方法:把下面的javascript代碼加入網頁的<head>與</head>之間,這樣網頁中所有透明背景的png圖檔都可以正常顯示:(經一葉扁舟測試可以)
<script language="javascript">
function correctPNG()
{
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>
第二種方法:把下面的代碼加在網頁中的png圖檔顯示代碼中:
<span style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='圖檔名稱.png',sizingMethod='scale');"></span>
第三種 在頭部加入
<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 j=0; j<document.images.length; j++)
{
var img = document.images[j]
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
j = j-1
}
}
}
本文轉自 yeybz 51CTO部落格,原文連結:http://blog.51cto.com/hmlwl/1166985