让IE浏览器支持png透明的方法
其实这个问题网上早就有人一直在讨论了,IE6的浏览器的好像一直都不能直接的支持png图片的透明,但是IE7以上的版本,以及最新发布的IE8以及都已经支持PNG图片的透明里,那我这里就来讲讲我最近发现的好好的办法吧。以前找到的让png图片透明的方法感觉都不怎么好使,今天发现的这个方法个人感觉还是比较有效的。
方法一:
将下面代码保存为correctPNG.js:
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; margin:6px; 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);
然后在你需要透明的网页中的<head>....</head>区加入:<script type="text/javascript" src="correctPNG.js"></script>
方法二:
最近做网站时,发现IE不支持PNG透明图片,其实这个问题很早就发现了这个问题,一直没有得到解决
今天去蓝色理想论坛上发贴,希望得到决于.一个朋友讲用一段JS可以实现此透明透明.看着一段JS代码发麻,
幸好在在闪吧上找到了解决方案.现在贴出来,与在家分享.
解决思路:
IE直接是不支持PNG格式的图片的透明效果的,但通过 AlphaImageLoader 滤镜可以解决这个问题。
代码示例:
<body bgcolor="#eeeeee">
PNG透明:
<div style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=demo.png);width:200px;height:150px"></div>
PNG不透明:<br>
<img src="demo.png">
注意:使用本代码前必须保证PNG图片有透明的部分
