IE6上PNG图片的透明背景问题完全解决方案
这个问题其实包括两个方面,一是PNG图片作为背景情况下的图片透明度问题,二是作为图片的透明度问题,即似乎用IMG标签的PNG图片的透明度。
问题一:PNG图片作为背景时的透明问题
解决办法:
如果希望使用png格式图片作为DOM元素的背景,那么就可以使用滤镜来加载png图片
.png{
background: url(/angel.png) no-repeat !important;
_filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=”angel.png”);
background:none;
width:100px;
height:100px;
}
HTML代码:
<代码开始 lang=”html”>
<div>背景PNG透明<div>
解决的办法就是使用IE的Microsoft.AlphaImageLoader滤镜。以上代码使用了一些IE的Hack,IE7,IE8,Firefox直接应用background,IE6则会应用filter!
该特性的详细语法如下:
语法:
filter : progid:DXImageTransform.Microsoft.AlphaImageLoader ( enabled=bEnabled , sizingMethod=sSize , src=sURL )
属性:
enabled : 可选项。布尔值(Boolean)。设置或检索滤镜是否激活。true | false
true : 默认值。滤镜激活。
false : 滤镜被禁止。sizingMethod : 可选项。字符串(String)。设置或检索滤镜作用的对象的图片在对象容器边界内的显示方式。 crop : 剪切图片以适应对象尺寸。
image : 默认值。增大或减小对象的尺寸边界以适应图片的尺寸。
scale : 缩放图片以适应对象的尺寸边界。
src : 必选项。字符串(String)。使用绝对或相对 url 地址指定背景图像。假如忽略此参数,滤镜将不会作用。说明:
在对象容器边界内,在对象的背景和内容之间显示一张图片。并提供对此图片的剪切和改变尺寸的操作。如果载入的是PNG(Portable Network Graphics)格式,则0%-100%的透明度也被提供。
PNG(Portable Network Graphics)格式的图片的透明度不妨碍你选择文本。也就是说,你可以选择显示在PNG(Portable Network Graphics)格式的图片完全透明区域后面的内容。
也可以使用hack的方式整理一下代码,方法如下:
#div1 {
height: 600px;
width: 260px;
padding: 20px;
background-repeat: repeat;}
/* for ie7 ff*/
html>body #div1 {
background:url(../images/menu1.png) no-repeat;
}
/* for ie6 */
* #div1 {
background:none;
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=’images/menu1.png’, sizingMethod=’crop’);
}
需要注意的是:AlphaImageLoader滤镜会导致该区域的链接和按钮无效,解决的办法是为链接或按钮添加:position: relative;这样条代码,使其相对浮动。AlphaImageLoader无法设置背景的重复,所以对图片的切图精度会有很高的精确度要求。
问题二:PNG作为插入图片的透明度问题
解决办法1:使用脚本
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
};
};
};
if(navigator.userAgent.indexOf(“MSIE”)>-1)
{
window.attachEvent(“onload”, correctPNG);
};
解决办法2:使用css express+一张透明小图片
.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.runtimeStyle.backgroundImage = “none”)),this.pngSet=true);
}
HTML代码:
<div>
<img src=”icon_face_07.png” width=”30″ height=”30″ />
<img src=”icon_face_10.png” width=”30″ height=”30″ />
<img src=”icon_face_08.png” width=”30″ height=”30″ />
</div>
注意,要准备一个透明的小图片transparent.gif,大小不限。
最后,
另外,还有2个重要的库或者插件不得不提:
1. DD_belatedPNG库,可以很好的解决png透明问题。
http://www.dillerdesign.com/experiment/DD_belatedPNG/
2.jquery.pngfix.js插件,简单易用,需要jQuery库支持。
http://jquery.andreaseberhard.de/pngFix/