png 透明针对 IE6 一直是件挺麻烦的事情,使用的方法也是各有不同,大多的原理是用 IE 的滤镜来解决的。

语法:

filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=bEnabled, sizingMethod=sSize, src=sURL)
  • enabled:可选项。布尔值(Boolean)。设置或检索滤镜是否激活。true | false true : 默认值。滤镜激活。 false : 滤镜被禁止。
  • sizingMethod: 可选项。字符串(String)。设置或检索滤镜作用的对象的图片在对象容器边界内的显示方式。 crop : 剪切图片以适应对象尺寸。
  • image: 默认值。增大或减小对象的尺寸边界以适应图片的尺寸。scale : 缩放图片以适应对象的尺寸边界。
  • src: 必选项。字符串(String)。使用绝对或相对 url 地址指定背景图像。假如忽略此参数,滤镜将不会作用。

现在一般在使用的方法有一下几种:

1. CSS 方法

.pngs {
  height: 90px;width: 90px;
  background-image:url(icon_home.png)!important;  /* FF IE7 */
  background-repeat: no-repeat; _filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='icon_home.png');  /* IE6 */
  _ background-image: none; /* IE6 */
}

在 HTML 如下调用:

<div class="pngs"></div>

这种方法的优点就是使用简单方便,但是不能作为背景,且只能用作单个png图片的使用。如果要作为背景,需要新增加一个div层,并设置其position:relative;

.png div{position:relative;}

<div class='png'>
  <div>CSS 背景PNG透明 及 链接失效问题解决</div>
</div>

这种方法可以使用在那些png图片不多,且不需要repeat的情况下。

2.JS 方法

<script language="JavaScript">
function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
    var arVersion = navigator.appVersion.split("MSIE")
    var version = parseFloat(arVersion[1])
    if ((version >= 5.5) && (document.body.filters))
    {
       for(var j=0; j<document.images.length; j++)
       {
          var img = document.images[j]
          var imgName = img.src.toUpperCase()
          if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
          {
             var imgID = (img.id) ? "id='" + img.id + "' " : ""
             var imgClass = (img.className) ? "class='" + img.className + "' " : ""
             var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
             var imgStyle = "display:inline-block;" + img.style.cssText
             if (img.align == "left") imgStyle = "float:left;" + imgStyle
             if (img.align == "right") imgStyle = "float:right;" + imgStyle
             if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
             var strNewHTML = "<span " + imgID + imgClass + imgTitle
             + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
             + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
             + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
             img.outerHTML = strNewHTML
             j = j-1
          }
       }
    }    
}
window.attachEvent("onload", correctPNG);
</script>
JS

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
  • 2022-02-13
  • 2021-05-29
  • 2021-10-30
  • 2021-12-31
  • 2021-11-17
猜你喜欢
  • 2022-01-24
  • 2021-06-28
  • 2021-12-24
  • 2021-08-28
  • 2021-08-25
相关资源
相似解决方案