天天看點

相容IE6下PNG24透明的幾種方法

方法一:css(這種方法可以使用在那些png圖檔不多,且不需要repeat的情況下。)

css:

.pngs24 {width:108px; height:108px; background-image:url(png24.png); _background-image:none; _filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='png24.png'); background-repeat:no-repeat}

html:

<div class="pngs24"></div>

方法二:javascript方法(使用起來的确友善,無論多少圖檔都可以解決,但是依然無法repeat。)

直接調用:

<script language="javascript">

function correctPNG()

{

    var arVersion = navigator.appVersion.split("MSIE")

    var version = parseFloat(arVersion[1])

    if ((version >= 5.5) && (document.body.filters))

    {

       for(var j=0; j<document.images.length; j++)

       {

          var img = document.images[j]

          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

             j = j-1

          }

       }

    }   

}

window.attachEvent("onload", correctPNG);

</script>

方法三:iepngfix.htc

iepngfix.htc的使用方法:

1、下載下傳腳本腳本,将其中的iepngfix.htc和blank.gif解壓縮到合适的目錄内,.htc即Html Components,該檔案需要在CSS中被調用;blank.gif是一個1×1像素的透明GIF圖檔,缺少該檔案會使<img>标簽插入的PNG圖象顯示為紅色的叉燒包。

2、在iepngfix.htc中修改blank.gif的路徑,var blankImg =‘blank.gif的正确路徑’,這是惟一一個需要修改的配置。

3、在css中将需要使用透明PNG的元素與.htc檔案關聯。 例如:*{behavior: url(iepngfix.htc) }

通過以上三步,IE6就能支援透明PNG圖檔了。注意:請使用IE6測試該代碼效果。

版權聲明:本文為CSDN部落客「weixin_34080951」的原創文章,遵循CC 4.0 BY-SA版權協定,轉載請附上原文出處連結及本聲明。

原文連結:https://blog.csdn.net/weixin_34080951/article/details/91921298