繁体中文
设为首页
加入收藏
当前位置:网页设计首页 >> HTML/Xhtml >> png图片在ie不透明的解决方案

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='image')></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='attachments/month_0703/h20073223321.png',sizingMethod='image')"></div>


第二种方法就是用javascript来控制,程序代码如下:
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);

</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>

责任编辑:探索者


相关文章
 

最新文章

更多

· png图片在ie不透明的解决...
· 如何禁用 radio ,设置为只读
· 符号对照表
· XHTML常用标签介绍
· 根据网页HTML结构选择最...
· HTML 5新增的元素
· 三种简洁的Tab导航简析
· XHTML 2 抢先试用
· 如何为Web页面选择合适的...
· HTML向XHTML转化

推荐文章

更多

· png图片在ie不透明的解决...
· 如何禁用 radio ,设置为只读
· 符号对照表
· XHTML常用标签介绍
· 根据网页HTML结构选择最...
· HTML 5新增的元素
· 三种简洁的Tab导航简析
· XHTML 2 抢先试用
· 如何为Web页面选择合适的...
· HTML向XHTML转化

热点文章

更多

· png图片在ie不透明的解决...