使用者工具

網站工具


webdesign:png_in_ie

PNG vs Internet Explorer (IE)

http://www.mike.idv.tw/2006/12/09/274

一般架設網站會對一些測試做的比較嚴謹,其中測試圖片的顯示也是重要的一環!

這幾天依序為這個網誌作了一些小功能的增加,突然發現在 IE 的環境瀏覽下‥‥ PNG 影像背景透明的顯示似乎在 IE 5.x and 6 上出了小小的問題(有礙觀瞻‥‥)

簡單說就是具有背景透明的 PNG 圖檔在 IE 瀏覽的情況下會出現圖片無法背景透明的情形!

於是就在 The PNG problem in Windows Internet Explorer 這個網站找到了解決方法!!

其實 PNG 這個圖形格式去背的定義早在 n 年前就已經通過 W3C 的認可規範~ 但殊不知偉大的 MicroSoft 的 Internet Explorer 5.x 6 都不遵循 W3C 的規範‥‥ 導致有些具有去背的 PNG 無法在 IE 瀏覽下呈現應有的效果!但是IE 7 卻可以瀏覽~

好家在有了該網站所提供的 JavaScript 才能得以讓 IE 正確的瀏覽具有透明背景的 PNG 圖檔~ 同時也暫時解決了我的心頭之患‥‥

其實個人強烈建議有使用到 PNG 圖檔的網站朋友們加入這個 JavaScript 加入的方法很簡單, 將 pngfix.js 下載後放至於您的個人網站目錄‥‥

在將以下的文字貼到 </head> 之前‥‥

<!–[if lt IE 7.]>
<script defer type="text/javascript" src="pngfix.js"></script>
<![endif]–>

貼上之後之後網頁中有出現 <img src=“xxxxx.png> 的圖形背景透明就全部自動搞定了!! 但是仍需注意的是,若於 IE 下仍沒有 PNG 圖檔去背,請先試試指定PNG圖片的長與寬吧~

pngfix.js

http://homepage.ntlworld.com/bobosola/pngfix.js

/*
 
Correctly handle PNG transparency in Win IE 5.5 & 6.
http://homepage.ntlworld.com/bobosola. Updated 18-Jan-2006.

Use in <HEAD> with DEFER keyword wrapped in conditional comments:
<!--[if lt IE 7]>
<script defer type="text/javascript" src="pngfix.js"></script>
<![endif]-->

*/

var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])

if ((version >= 5.5) && (document.body.filters)) 
{
   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
      }
   }
}
webdesign/png_in_ie.txt · 上一次變更: 2006/12/09 14:58 由 wenpei