IE6下PNG背景透明的显示问题

来源:互联网 发布:成本计算软件 编辑:程序博客网 时间:2024/06/02 11:20

转自


PNG图像格式介绍:

PNG是20世纪90年代中期开始开发的图像文件存储格式,其目的是企图替代GIF和TIFF文件格式,同时增加一些GIF文件格式所不具备的特性。流式 网络图形格式(Portable Network Graphic Format,PNG)名称来源于非官方的“PNG’s Not GIF”,是一种位图文件(bitmap file)存储格式,读成“ping”。PNG用来存储灰度图像时,灰度图像的深度可多到16位,存储彩色图像时,彩色图像的深度可多到48位,并且还可 存储多到16位的α通道数据。

IE6下PNG背景透明的显示问题

PNG格式比起GIF来表现色彩更丰富,特别是表现渐变以及背景透明的渐变要比GIF格式出色很多,目前,最新的浏览器基本上都支持PNG格式。但是IE6不支持PNG背景透明,会显示一个灰色的框。

IE6下PNG背景透明的解决办法

一.IE6使用gif,其他则使用png来解决PNG背景灰

  1. .pngImg { background:url(image.png); _background:url(image.gif);} 

注意上文的_号,目前IE7,8以及Firefox浏览器等都不支持此CSS语法,只有IE6识别。因此,其他浏览器会调用PNG,而IE6刚调用GIF。

二.滤镜filter解决IE6下背景灰

  1. background:url(a.png) repeat-x 0 0
  2. _background:none
  3. _filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="a.png" ,sizingMethod="crop"); 

上面的原理是其他调用PNG,IE6,则先设背景没有,然后调用滤镜使之显示PNG图片。

缺陷:IE6下背景无法平铺,这个问题很严重。同时在性能上也有小问题,页面中次数不是很多的时候该办法还是可行的。

AlphaImageLoader滤镜会导致该区域的链接和按钮无效,解决的办法是为链接或按钮添加:position: relative;这样条代码,使其相对浮动。AlphaImageLoader无法设置背景的重复,所以对图片的切图精度会有很高的精确度要求。

解决IE下的链接无效可用最后面的方法:

三.利用JS解决html中的img(插入在网页中的png图像)png背景灰问题

  1. function correctPNG() 
  2. for(var i=0; i { 
  3. var img = document.images[i] 
  4. var imgName = img.src.toUpperCase() 
  5. if (imgName.substring(imgName.length-3, imgName.length) == "PNG"
  6. var imgID = (img.id) ? "id='" + img.id + "' " : "" 
  7. var imgClass = (img.className) ? "class='" + img.className + "' " : "" 
  8. var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' " 
  9. var imgStyle = "display:inline-block;" + img.style.cssText 
  10. if (img.align == "left") imgStyle = "float:left;" + imgStyle 
  11. if (img.align == "right") imgStyle = "float:right;" + imgStyle 
  12. if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle 
  13. var strNewHTML = " + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" 
  14. "(src=\'" + img.src + "\', sizingMethod='scale');\">" 
  15. img.outerHTML = strNewHTML 
  16. i = i-1 
  17. window.attachEvent("onload", correctPNG); 

页面中插入一段js即可。原理同上,只是将img放入了background。它会将所有插入的PNG都如此处理。

这也是一端同样的JS

在网页head部分引用下面的这段JS

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

四。调用iepngfix.htc解决IE6PNG背景灰及拉伸问题

此方法来自:http://www.twinhelix.com/css/iepngfix/ 此方法基于Winodws平台,在Linux下不支持htc,没有验证过,但有网友发文证实。

以下片段添加至css文件

  1. .pngImg {behavior: url(iepngfix.htc);} 

以下片段添加至html文件

  1. <div class="pngImg">PNG背景图片</div> 
  2. <img src="png图片" class="pngImg" alt=""> 

详细的应用方法这里就不介绍啦。

在逼不得已且身不由己必须使用PNG的情况下,这种方法应该是比较优秀的,虽然不能完美的解决IE6的平铺,但是至少是实现了拉伸,使得很多情况下可以代替平铺来使用。当然效率的问题任然是存在

五.让“块”透明的方法

  1. .div { FILTER: alpha(opacity=20); moz-opacity: 0.2; opacity: 0.2;} 

测试IE6,IE7,IE8,FF2,FF3均通过。提示:IE6,IE7需设置一个宽度(100%也行),否则看不到效果。

 六.DD_belatedPNG,解决IE6不支持PNG绝佳方案

整个互联网上解决这个IE6的透明PNG的方案也是多不胜数,从使用IE特有的滤镜或是e xpression,再到javascript+透明GIF替代.但是这些方法都有一个缺点,就是不支持CSS中backgrond-position与 background-repeat. 

而我今天介绍DD_belatedPNG,只需要一个理由,就是它支持backgrond-position与background-repeat.这是其他js插件不具备的.同时DD_belatedPNG还支持a:hover属性,以及<img>. 

看Demo: http://www.ediyang.com/demo/DD_Png/ 

原理 
这个js插件使用了微软的VML语言进行绘制,而其他多数解决PNG问题的js插件用的是AlphaImageLoader滤镜. 

使用方法 
1.在这里下载DD_belatedPNG.js文件. 
http://dillerdesign.com/experiment/DD_belatedPNG/#download 

2.在网页中引用,如下:

  1. <!--[if IE 6]>   
  2. <script src="DD_belatedPNG.js" mce_src="DD_belatedPNG.js"></script>   
  3. <script type="text/javascript">  
  4.    
  5. /* EXAMPLE */   
  6. DD_belatedPNG.fix('.png_bg');   
  7. /* 将 .png_bg 改成你应用了透明PNG的CSS选择器,例如我例子中的'.trans'*/   
  8. </script> 
  9. <![endif]-->   

3.有2种调用函数,一种是DD_belatedPNG.fix(),如上代码.另一种是fix(),这中方法需要在函数内指出css选择器名.  
  
使用a:hover请留意  
5-25 更新:如果你也像jutoy同学一样想要用透明PNG作为a:hover时的背景图片,那么你需要留意你的代码,需要以”a:hover”来作为选择器. 否则可能会导致无法成功.同时我也更新了demo,请需要的更新查看.接着我们看看正确的代码:

  1. <!--[if IE 6]>   
  2. <script type="text/javascript" src="js/DD_belatedPNG.js" ></script>   
  3. <script type="text/javascript">   
  4. DD_belatedPNG.fix('.trans,.box a:hover');   
  5. </script>   
  6. <![endif]-->   

七.通过 javascript 和 css 滤镜解决 IE 整站 png 背景透明问题

  1. <script type="text/javascript" language="javascript"> 
  2. function enablePngImages() { 
  3. var imgArr = document.getElementsByTagName("IMG"); 
  4. for(i=0; i<imgArr.length; i++){ 
  5.    if(imgArr[i].src.toLowerCase().lastIndexOf(".png") != -1){ 
  6.     imgArr[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + imgArr[i].src + "', sizingMethod='auto')"; 
  7.     imgArr[i].src = "spacer.gif"
  8.    } 
  9.    
  10.    if(imgArr[i].currentStyle.backgroundImage.lastIndexOf(".png") != -1){ 
  11.     var img = imgArr[i].currentStyle.backgroundImage.substring(5,imgArr[i].currentStyle.backgroundImage.length-2); 
  12.     imgArr[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img+"', sizingMethod='crop')"; 
  13.     imgArr[i].style.backgroundImage = "url(spacer.gif)"
  14.    } 
  15.  
  16. function enableBgPngImages(bgElements){ 
  17. for(i=0; i<bgElements.length; i++){ 
  18.    if(bgElements[i].currentStyle.backgroundImage.lastIndexOf(".png") != -1){ 
  19.     //alert(bgElements[i]); 
  20.     var img = bgElements[i].currentStyle.backgroundImage.substring(5,bgElements[i].currentStyle.backgroundImage.length-2); 
  21.     bgElements[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img+"', sizingMethod='crop')"; 
  22.     bgElements[i].style.backgroundImage = "url(spacer.gif)"
  23.    } 
  24. </script> 
  25.  
  26. <img src="pngpic.png" alt="" border="0" /> 
  27. <!--[if lt IE 7]> 
  28. <script type='text/javascript'> 
  29. var bgElements; 
  30. enablePngImages(); 
  31. if(bgElements){ 
  32.    enableBgPngImages(bgElements); 
  33. </script> 
  34. <![endif]--> 

资料参考来源:

http://ask.cttstar.com/www/2/32/283132.html

http://www.11gz.com/blog/2009/04/11/ie6-png-transparent/

http://bufanliu.javaeye.com/blog/227980

http://www.twinhelix.com/css/iepngfix/

http://xssep.cn/view-332.html

http://www.geekso.com/PNG-IE6/

http://hi.baidu.com/zhao0p/blog/item/3ee026d5c1abdac751da4bb8.html

原创粉丝点击