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='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>javascript

相關文章
相關標籤/搜索