PNG兼容IE6解决方法


虽然说现在早就不用ie6浏览器了,可以还是有一小部分还在使用 ,刚好公司也有要求~~~

<p>     E6不兼容png图片,确实让网页的图片质量大大下降,为了兼容万恶的IE6,总结了下面几种方法:
1,通过CSS滤镜使背景图的PNG对IE6进行兼容。
2,给img定义样式,页面上所有透明png即自动透明了。
3,通过JS,插入一段代码,实现img标签png兼容IE6的问题。
4,可以把png图片,转换为gif图片。(最简单常用的方法)
</p>

第一种方法:

1、通过CSS滤镜使背景图的PNG对IE6进行兼容
定义一个样式,给某个div应用这个样式后,div的透明png背景图片自动透明了。

 1 <style>  2  body{background: lightblue;}
 3  .png{
 4  width: 550px;
 5  height: 500px;
 6  background-image: url(pic.png)!important;/* FF IE7 */
 7  background-repeat: no-repeat;
 8  _filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='pic.png');  /*IE6*/
 9  _ background-image: none; /* IE6 */
10  overflow: hidden;
11         }
12  </style>
<div class="png">
        <div>通过CSS滤镜使背景图的PNG对IE6进行兼容</div>
    </div>
 
第二种方法
    给img定义样式,页面上所有透明png即自动透明了。注意:这方法只对直接插入的图片有效,对背景图无效。
要准备一个透明的小图片transparent.gif,大小不限,并将gif的图片放在跟png图片一个文件夹里。请勿大量
使用,否则会导致页面打开很慢!!!不过这种方法设置图片的不能控制其大小。


 <style> body{background: lightblue;} .imgpng img { azimuth: expression( this.pngSet?this.pngSet=true:(this.nodeName == "IMG" && this.src.toLowerCase().indexOf('.png')>-1?(this.runtimeStyle.backgroundImage = "none", this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src + "', sizingMethod='image')", this.src = "transparent.gif"):(this.origBg = this.origBg? this.origBg :this.currentStyle.backgroundImage.toString().replace('url("','').replace('")',''), this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.origBg + "', sizingMethod='crop')", this.runtimeStyle.backgroundImage = "none")),this.pngSet=true); } </style>
 
<div class="imgpng">
        <img src="pic.png" />
</div>

第三种方法:

通过JS,插入一段代码,实现img标签png兼容IE6的问题
 1 <script language="JavaScript">
 2         function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
 3  {  4             var arVersion = navigator.appVersion.split("MSIE")  5             var version = parseFloat(arVersion[1])  6             if ((version >= 5.5) && (document.body.filters))  7  {  8                 for(var j=0; j<document.images.length; j++)  9  { 10                     var img = document.images[j] 11                     var imgName = img.src.toUpperCase() 12                     if (imgName.substring(imgName.length-3, imgName.length) == "PNG") 13  { 14                         var imgID = (img.id) ? "id='" + img.id + "' " : ""
15                         var imgClass = (img.className) ? "class='" + img.className + "' " : ""
16                         var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
17                         var imgStyle = "display:inline-block;" + img.style.cssText 18                         if (img.align == "left") imgStyle = "float:left;" + imgStyle 19                         if (img.align == "right") imgStyle = "float:right;" + imgStyle 20                         if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle 21                         var strNewHTML = "<span " + imgID + imgClass + imgTitle 22                                 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
23                                 + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
24                                 + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
25                         img.outerHTML = strNewHTML 26                         j = j-1
27  } 28  } 29  } 30  } 31         window.attachEvent("onload", correctPNG); 32     </script>
1 <style> 2  body{background: lightblue;}
3  div img{bottom: 3px solid yellow;}
4  </style> 5 
6  <div class="imgpng"> 7  <img src="pic.png"/> 8  </div>

番外:

使用IE6的hack来书写只有IE6能识别的css样式。

1 .bb{
2 height:32px;
3 background-color:#f1ee18;/*所有识别*/
4 .background-color:#00deff\9; /*IE6、7、8识别*/
5 +background-color:#a200ff;/*IE6、7识别*/
6 _background-color:#1e0bd1;/*IE6识别*/
7 }
8 于是大家还可以这样来区分firefox,IE7,IE6 9 background:green!important; *background:orange; _background:blue;

 

关于兼容ie6的一些方法:
背景png图片,括号里的是样式选择器,中间用逗号隔开

<!--[if IE 6]>
<script type="text/javascript" src="js/DD_belatedPNG.js"></script>
<script type="text/javascript"> DD_belatedPNG.fix('.tel,.head_content'); </script>

 




免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM