IE6不支持PNG24的解决办法--如何让PNG格式的透明图片在IE6下正常显示
|
做网站时,少不了要用PNG格式的图片,PNG(Portable Network Graphics)是W3C推荐的网页图片通用格式,但是Microsoft的IE6以下(IE7已经支持)没有把PNG的Alpha 通道打开,造成透明PNG图片的效果出不来。在Firefox、Opera下显示正常的透明PNG图片,在IE下浏览时就会带有灰色背景色,so ugly。。。 不透明的就像这样:
解决方法之一是给透明PNG图片加一个滤镜: 运行代码框 或是用CSS的方式控制,把style写在CSS文件中,就不用每贴一张图都要写这么长的代码了。 PS:因为IE5.0以下不支持AlphaImageLoader滤镜,所以IE5.0以下就不能显示透明PNG效果了,不过现在还用IE5.0以下的恐怕是没人了吧。
关于AlphaImageLoader滤镜
sizingMethod : 可选项。字符串(String)。设置或检索滤镜作用的对象的图片在对象容器边界内的显示方式。 crop : 剪切图片以适应对象尺寸。 src : 必选项。字符串(String)。使用绝对或相对 url 地址指定背景图像。假如忽略此参数,滤镜将不会作用。 说明:
核心代码CSS: filter : progid:DXImageTransform.Microsoft.AlphaImageLoader ( enabled=bEnabled , sizingMethod=sSize , src=sURL )
属性: 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);
/* Correctly handle PNG transparency in Win IE 5.5 & 6. Copyright 2007 Ignia, LLC Based in part on code from from http://homepage.ntlworld.com/bobosola. Use in with DEFER keyword wrapped in conditional comments: <script type="text/javascript" defer="true" src="pngfix.js"></script> */ function fixPng() { var arVersion = navigator.appVersion.split("MSIE") var version = parseFloat(arVersion[1]) if ((version >= 5.5 && version < 7.0) && (document.body.filters)) { for(var i=0; i<document.images.length;></document.images.length;> var img = document.images[i]; var imgName = img.src.toUpperCase(); if (imgName.indexOf(".PNG") > 0) { var width = img.width; var height = img.height; var sizingMethod = (img.className.toLowerCase().indexOf("scale") >= 0)? "scale" : "image"; img.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img.src.replace('%23', '%2523').replace("'", "%27") + "', sizingMethod='" + sizingMethod + "')"; img.src="images/blank.gif" mce_src="images/blank.gif"; img.width = width; img.height = height; } } } }
|
