MozTW 討論區
https://forum.moztw.org/

讓 IE6 顯示 PNG 透明的語法
https://forum.moztw.org/viewtopic.php?f=8&t=26045
1 頁 (共 1 頁)

發表人:  adam [ 2009-02-07, 14:30 ]
文章主題 :  讓 IE6 顯示 PNG 透明的語法

我以前有找到這個語法,但忘記在哪,只要加上一小段語法,就能讓顯示 PNG 透明,完全不用載入任何 .js 檔,不知有沒有人知道這段語法?

發表人:  Merci chao [ 2009-02-07, 15:21 ]
文章主題 : 

代碼:
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--;
      }
   }
}

手上沒有 IE6, 請自行試試是否正確

發表人:  余弘兵 [ 2009-02-08, 17:26 ]
文章主題 : 

請問一下,要把這段語法放在那裡才能夠在IE6 顯示透明PNG?

發表人:  風痕影 [ 2009-02-08, 19:03 ]
文章主題 : 

放哪都可以,記得要等所有圖片語法都出現了才執行
所以可以在 <body> 中加上 onload="correctPNG()"

記得加上 <script type="text/javascript"> 和 </script>

發表人:  adam [ 2009-02-08, 21:08 ]
文章主題 : 

試了 Merci chao 大的方法還是行不通,不過非常謝謝。後來我 Google 到一段語法就可以了:

代碼:
<script>
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>

發表人:  不惑仔Boohover [ 2009-02-10, 12:46 ]
文章主題 : 

ie AlphaImageLoader filter 並沒有限制只在 img 元素可用

msdn

1 頁 (共 1 頁) 所有顯示的時間為 UTC + 8 小時
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/