PNG背景在不同瀏覽器下的應(yīng)用
更新時間:2009年06月22日 00:16:29 作者:
論壇中kayeun在我的版面發(fā)了一個問題,關(guān)于PNG透明背景在FIREFOX瀏覽器下顯示異常。
這里談一下不同瀏覽器內(nèi),如何應(yīng)用PNG圖片作背景。
一、在IE6中使用PNG背景
IE6本身并不認識PNG圖片的透明特性,雖然有讓IE6支持PNG透明背景的JS程序:
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, 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
}
}
}
}
window.attachEvent("onload", correctPNG);
但是如果需要實現(xiàn)的地方僅有一處,還是用CSS來實現(xiàn)效率更高一些。這里應(yīng)用的是IE5.5+的AlphaImageLoader濾鏡:
filter:
progid:DXImageTransform.Microsoft.AlphaImageLoader
(src='temp.png',sizingMethod='scale')
需要注意的是:AlphaImageLoader濾鏡會導(dǎo)致該區(qū)域的鏈接和按鈕無效,解決的辦法是為鏈接或按鈕添加:position: relative;使其相對浮動。另外AlphaImageLoader無法設(shè)置背景的重復(fù),所以對圖片的切圖精度會有較高要求。
二、在IE7、Opera和firefox中使用PNG背景
這幾款瀏覽器對PNG背景的支持很好,可以直接應(yīng)用。而實際應(yīng)用中,我們需要同時照顧IE6瀏覽器,所以需要給樣式表加上*html來做兼容處理。也就是給同一標簽兩次背景。
例如:
.uicss_cn{background:transparent url(../images/temp.png) repeat-x bottom left;height:3px;position:absolute;width:100%; font-size:0px;}
*html .uicss_cn{background:transparent;
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="../images/temp.png", sizingMethod="crop");}
三、補充一點
3月27號,無意中發(fā)現(xiàn),自己插入的PNG背景兼容代碼無效。最后證實問題出在圖片文件上。使用fireworks生成的部分png圖片,需要導(dǎo)出為PSD格式,再從PS里另存為PNG文件,即可。
IE6本身并不認識PNG圖片的透明特性,雖然有讓IE6支持PNG透明背景的JS程序:
復(fù)制代碼 代碼如下:
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, 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
}
}
}
}
window.attachEvent("onload", correctPNG);
但是如果需要實現(xiàn)的地方僅有一處,還是用CSS來實現(xiàn)效率更高一些。這里應(yīng)用的是IE5.5+的AlphaImageLoader濾鏡:
復(fù)制代碼 代碼如下:
filter:
progid:DXImageTransform.Microsoft.AlphaImageLoader
(src='temp.png',sizingMethod='scale')
需要注意的是:AlphaImageLoader濾鏡會導(dǎo)致該區(qū)域的鏈接和按鈕無效,解決的辦法是為鏈接或按鈕添加:position: relative;使其相對浮動。另外AlphaImageLoader無法設(shè)置背景的重復(fù),所以對圖片的切圖精度會有較高要求。
二、在IE7、Opera和firefox中使用PNG背景
這幾款瀏覽器對PNG背景的支持很好,可以直接應(yīng)用。而實際應(yīng)用中,我們需要同時照顧IE6瀏覽器,所以需要給樣式表加上*html來做兼容處理。也就是給同一標簽兩次背景。
例如:
復(fù)制代碼 代碼如下:
.uicss_cn{background:transparent url(../images/temp.png) repeat-x bottom left;height:3px;position:absolute;width:100%; font-size:0px;}
*html .uicss_cn{background:transparent;
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="../images/temp.png", sizingMethod="crop");}
三、補充一點
3月27號,無意中發(fā)現(xiàn),自己插入的PNG背景兼容代碼無效。最后證實問題出在圖片文件上。使用fireworks生成的部分png圖片,需要導(dǎo)出為PSD格式,再從PS里另存為PNG文件,即可。
相關(guān)文章
學(xué)習(xí)JavaScript設(shè)計模式(策略模式)
這篇文章主要帶領(lǐng)大家學(xué)習(xí)JavaScript設(shè)計模式,其中重點介紹策略模式,以年終獎為實例對策略模式進行分析,對策略模式進行詳細剖析,感興趣的小伙伴們可以參考一下2015-11-11怎樣在CocosCreator中使用物理引擎關(guān)節(jié)
這篇文章主要介紹了怎樣在CocosCreator中使用物理引擎關(guān)節(jié),對物理引擎感興趣的同學(xué),著重要看一下2021-04-04layer.open的自適應(yīng)及居中及子頁面標題的修改方法
今天小編就為大家分享一篇layer.open的自適應(yīng)及居中及子頁面標題的修改方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09