zoukankan      html  css  js  c++  java
  • IE6下PNG背景透明的显示问题1

     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=\"" + "" + 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的平铺,但是至少是实现了拉伸,使得很多情况下可以代替平铺来使用。当然效率的问题任然是存在

  • 相关阅读:
    第二次作业循环语句
    c语言01次作业分支,顺序结构
    PAT 1027. Colors in Mars
    PAT 1026 Table Tennis
    PAT 1035 Password
    PAT 1038. Recover the Smallest Number
    PAT 1028 List Sorting (25)
    PAT 1041 Be Unique (20)
    PAT 1025 PAT Ranking
    1037. Magic Coupon
  • 原文地址:https://www.cnblogs.com/liuu/p/2992779.html
Copyright © 2011-2022 走看看