一次性解决png在i6下的透明问题(包括做为img和背景的两种情况)
2011-11-07 15:45
417 查看
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 } } } function alphaBackgrounds(){ var rslt = navigator.appVersion.match(/MSIE (d+.d+)/, ''); var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5); for (i=0; i<document.all.length; i++){ var bg = document.all[i].currentStyle.backgroundImage; if (bg){ if (bg.match(/.png/i) != null){ var mypng = bg.substring(5,bg.length-2); document.all[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+mypng+"', sizingMethod='crop')"; document.all[i].style.backgroundImage = "url('')"; } } } } if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) { window.attachEvent("onload", correctPNG); window.attachEvent("onload", alphaBackgrounds); }
将这段代码放入页面中,可以将png的图片在i6下透明度问题一次性解决,不论在页面内以img标签的形式,还是在css里以background的形式。不用在css里去一个个设滤镜了。
这段代码极大的方便了页面的制作,但仍然有个问题:因为js会自动将css的background-img清零,所以background-repeaqt和background-position还是不能使用的,大家切页面的时候还是要小心。
相关文章推荐
- [转载 js]一次性解决png在i6下的透明问题(包括做为img和背景的两种情况)
- [转载 js]一次性解决png在i6下的透明问题(包括做为img和背景的两种情况)
- CSS解决IE6下PNG图片背景不透明的问题
- IE6 PNG不透明问题 (只解决img标签的图片)
- Qt 解决png图片转jpg图片透明背景变成黑色的问题
- 基于jQuery IE6下PNG图片背景透明问题解决方法
- 完美解决ie6不支持png 透明图片 和标签背景的问题
- 解决ie6下png的透明背景问题
- 解决ie6下png不透明,出现灰色背景的问题
- 使用JS插件解决IE6不支持PNG透明背景问题
- DD_belatedPNG 与 unitpngfix 两种解决 IE6 中 PNG 图片透明问题方案横向对比
- 使用DD_belatedPNG让IE6支持PNG透明图片 及IE6下背景图闪烁问题的解决方法
- DD_belatedPNG.js解决透明PNG图片背景灰色问题(DD_belatedPNG.js下载)
- 解决IE6png背景图片透明的问题
- DD_belatedPNG 与 unitpngfix 两种解决 IE6 中 PNG 图片透明问题方案横向对比
- IE6下滤镜插入透明PNG背景,解决失焦问题
- [Javascript] 解决 IE6 中 png 图片背景不支持透明效果的问题
- 解决IE6下png图片文件背景不透明的问题
- IE6下png图片和背景图片透明问题解决方法
- IE6下PNG背景透明的显示问题解决方案