IE6下PNG图片背景不透明的问题

日期 : 2008年8月10日 20:00 pm 标签 : - 代码浅谈 - - - 评论 : 6 feet 日历 : 10 Aug 2008

周末调试的时候,发现之前的ie6下显示png代码不够完善,于是上网转了篇详细的,一起学习。

CSS解决IE6下PNG图片背景不透明的问题

使用IE6的滤镜来解决

background: url(你的图.png)  no-repeat;
_filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=’你的图.png’); /* IE6  注意这里的png路径最好是绝对路径,因为它是相对于调用的html页面的路径,而不是css目录的相对路径 */
_background-image: none;

JS解决IE6下PNG图片不透明的问题

背景png图片可以使用css来解决,如果是img 标签的图片,我则是用js来实现,也比较简单。
以下是网上流行的pngfix.js的代码。下载地址


function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
  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 && window.attachEvent("onload", correctPNG);

===========补充==========

jQuery插件解决IE6下PNG图片不透明的问题

1.插件下载地址
Download jQuery
Download pngFix.zip jquery.pngFix.js
2.使用方法

<script type="text/javascript" src="jquery-latest.pack.js"></script>
<script type="text/javascript" src="jquery.pngFix.js"></script>
<script type="text/javascript">
  $(document).ready(function(){
  $(document).pngFix();
});
</script>
哟?不错哎 订阅飞鱼的RSS Feeds 回去慢慢看^_^

您可能还对这些日志感兴趣

该文章已经被摸了5,153次。 共有 6 条评论

  1. 1. Anonymous 2010年2月2日 at 16:50

    第一种方法好像不管用在ie6下图片没了

    回复

  2. 2. 飞鱼 2010年2月2日 at 21:00

    你代码咋写的·?

    回复

  3. 3. seri 2010年8月12日 at 18:11

    还是jquery牛X。终于解决了。不明白为什么偶在这个问题上徘徊这么久 orz(有时真是难免会微弱的NC一下 唉

    回复

    飞鱼 Reply:

    杯具··· 还折腾这个·~

    你的blog 咋一直都大姨妈了? 都好多个月了

    回复

  4. 4. MrQ 2010年10月3日 at 20:00

    请问这个png jQuery 支持background-position定位吗? 请您给我回的信好吗

    回复

    飞鱼 Reply:

    用过了,不可以,jquery 只是解决 ie6下的png 显示问题,可以针对性解解,避开backgroud-position 嘎嘎~

    回复

发表评论

评论仅支持“a、abbr、strong、em、blockquote、code”几个简单的标签