png图片在ie不透明的解决方案
2008-05-19 11:09:59  作者:  来源:互联网  浏览次数:10  文字大小:【】【】【
简介:PNG(Portable Network Graphics)是W3C推荐的网页图片通用格式,但是Microsoft的IE6以下(IE7已经支持)没有把PNG的Alpha 通道打开,造成透明PNG图片的效果出不来。在Firefox、Opera下显示正常的透明PNG图片,在IE ...
关键字:图片 方案

PNG(Portable Network Graphics)是W3C推荐的网页图片通用格式,但是Microsoft的IE6以下(IE7已经支持)没有把PNG的Alpha 通道打开,造成透明PNG图片的效果出不来。在Firefox、Opera下显示正常的透明PNG图片,在IE下浏览时就会带有灰色背景色,不透明的就像 下图示例:


解决方法之一是给透明PNG图片加一个滤镜:
引用内容
<div style="width:100%; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='透明PNG图片路径 ',sizingMethod='p_w_picpath')></div>
或是用CSS的方式控制,把style写在CSS文件中,就不用每贴一张图都要写这么长的代码了。

透明的效果就像这样:
HTML 代码<style type="text/css"> <!-- body { background-color: #FF9900; } --> </style> <div style="width:128px; height:128px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='p_w_uploads/month_0703/h20073223321.png',sizingMethod='p_w_picpath')"></div>


第二种方法就是用javascript来控制,程序代码如下:
function correctPNG()
{
for(var i=0; i<document.p_w_picpaths.length; i++)
{
var img = document.p_w_picpaths[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(" correctPNG);

</script>

在页面中加入此代码PNG图片即可透明。。。



浏览器通用透明效果:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>runcode</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Author" content="Sheneyan" />
<style type="text/css">
body{background:gray;}
div#test{
   background:#000;
   color:white;
   width:200px;
   position:absolute;
   left:10px;
   top:10px;
   filter: Alpha(opacity=50);
   -moz-opacity:.50;
   opacity:0.5;
}
</style>

</head>
<body>
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
<div id="test">我应该是半透明的……</div>
</body>
</html>