解决PNG图片在IE6下背景不透明的问题让IE6支持PNG透明背景

by 清泉 11. 九月 2008 09:19

PNG图片在FF下是透明,但IE6下会变成浅蓝色的,下面的方法可以有效解决PNG图片在IE6下背景不透明的问题

IE6下PNG图片背景不透明的问题解决方法一:
给CSS控制文件加入如下CSS属性进行控制就可以解决PNG在IE6下面不透明的问题:
 
* html div { 
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src="tran.png"); 
background:none; 
}

注意:这里我们使用了 “*”的CSS hack,这个CSS Hack是Internet Explorer 6独有的,在Ineternet Explorer 6的DOM结构中,默认HTML的父节点为*,而在标准的DOM结构中HTML就是根节点。所以上面的CSS 规则只有Internet Explorer 6 认识。

这样,我们在Internet Explorer 6、7、Firefox、Opera等最常用的浏览器的都实现了半透明效果了。

IE6下PNG图片背景不透明的问题解决方法二:
在网页head部分引用下面的这段JS

  1. function CorrectPNG()   
  2. {      
  3.    for(var i=0; i<document.images.length; i++)   
  4.    {      
  5.      var img = document.images[i];   
  6.      var imgName = img.src.toUpperCase();   
  7.      if (imgName.substring(imgName.length-3, imgName.length) == "PNG")   
  8.      {   
  9.        var imgID = (img.id) ? "id='" + img.id + "' " : "";   
  10.        var imgClass = (img.className) ? "class='" + img.className + "' " : "";   
  11.        var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";   
  12.        var imgStyle = "display:inline-block;" + img.style.cssText ;   
  13.        if (img.align == "left") { imgStyle = "float:left;" + imgStyle; }   
  14.        if (img.align == "right") { imgStyle = "float:right;" + imgStyle; }   
  15.        if (img.parentElement.href) { imgStyle = "cursor:hand;" + imgStyle; }   
  16.        var strNewHTML = "<span " + imgID + imgClass + imgTitle;   
  17.        + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";";   
  18.        + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader";   
  19.        + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";   
  20.        img.outerHTML = strNewHTML;   
  21.        i = i-1;   
  22.      };   
  23.    };   
  24. };   
  25. if(navigator.userAgent.indexOf("MSIE") > -1)   
  26. {   
  27.   window.attachEvent("onload", CorrectPNG);   
  28. };  

不过我在使用的时候发现一个问题.如果加上这段代码IE7下面将看不到PNG图片.所以不推荐使用此方法.

Tags:

学习心得

评论

添加评论



(将显示你的Gravatar头像)  

biuquote
微笑得意调皮害羞酷大笑惊讶发呆喜欢可怜尴尬闭嘴噘嘴皱眉伤心抓狂呕吐坏笑漫骂发怒
Loading



Supidea.com 晨飞的梦 @ All Rights Reserved. Powered by BlogYi.NET ver:1.8.0.0. 苏ICP备09011404号

关于博主

kamau
抱着美好的理想背井离乡,这酸甜苦辣只能默默忍受。既然选择了路,就得风雨兼程……

Calendar

<<  五月 2012  >>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

在日历中查看文章

最近的评论

Comment RSS

声明

      本博所发一切破解相关附件只作学习研究交流之用,严禁用于商业用途,请在下载24小时内删除。
      本博所有网友评论不代表本博立场,版权归其作者所有。

© Copyright 2009