IE6.0中使用Javascript支持png格式图片 - Go语言中文社区

IE6.0中使用Javascript支持png格式图片


你知道如何用Javascript让IE6.0支持png格式的图片吗,这里和大家分享一下,如果把PNG图片插入网页的话可以给网站内容增色不少!更重要的是在不增加图片容量大小的情况下提高了页面的图片的质量。

用Javascript让IE6.0支持png格式的图片

PNG图片有很好的品质。阴影效果也不会有杂边,很流畅。如果插入网页的话可以给网站内容增色不少!更重要的是在不增加图片容量大小的情况下提高了页面的图片的质量。对于有复杂背景,如:在有颜色过度背景上插入不规则边框的图片带来极大很便利!

但目前IE中对于插入的透明背景的.png的图片是不能正常显示的。IE会自动给”.png”格式的图片加个灰色背景。

解决这个的方法是增加javascript。具体方法如下:

把下面的代码放在head区就可以解决问题了。

以下是引用片段:

  1. <script language=”javascript”> 
  2. function correctPNG()  
  3. {  
  4. for(var i=0; i<document.images.length; i++)  
  5. {  
  6.  
  7. var img = document.images[i];  
  8. var imgimgName = img.src.toUpperCase();  
  9. if (imgName.substring(imgName.length-3, imgName.length) == “PNG”)  
  10. {  
  11. var imgID = (img.id) ? “id=’” + img.id + “‘ ” : “”;  
  12. var imgClass = (img.className) ? “class=’” + 
  13. img.className + “‘ ” : “”;  
  14. var imgTitle = (img.title) ? “title=’” + img.title +
  15.  “‘ ” : “title=’” + img.alt + “‘ “;  
  16. var imgStyle = “display:inline-block;” + img.style.cssText;  
  17. if (img.align == “left”) imgStyle = “float:left;” + imgStyle;  
  18. if (img.align == “right”) imgStyle = “float:right;” + imgStyle;  
  19. if (img.parentNode.href) imgStyle = “cursor:hand;” + imgStyle;  
  20. var strNewHTML = “<span ” + imgID + imgClass + imgTitle  
  21. + ” style=”” + “width:” + img.width + “px; height:”
  22.  + img.height + “px;” + imgStyle + “;”  
  23. + “filter:progid:DXImageTransform.Microsoft.AlphaImageLoader”  
  24. + “(src=’” + img.src + “’, sizingMethod=’scale’);”></span>”;  
  25. img.outerHTML = strNewHTML ;  
  26.  
  27. }  
  28. }  
  29. }  
  30. </script> 
  31.  

也可以把这段代码单独加在一张图片上:

以下是引用片段:

  1. <span style="filter:progid:   
  2. DXImageTransform.Microsoft.AlphaImageLoader  
  3. (src='male.png'sizingMethod='scale');"> </span> 

还有一点:所有的png格式的图片一定要加上宽度和 高度 不然图片大小会发生变化。

版权声明:本文来源51CTO,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:http://developer.51cto.com/art/201009/228847.htm
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-05-16 07:40:22
  • 阅读 ( 447 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢