`

IE6下PNG图片透明方法

    博客分类:
  • HTML
阅读更多

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 方法

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 */

}
 

用了 ie 的滤镜就是这样麻烦

只要在用到滤镜 filter 内的 a 链接样式上加上 position:relative; 链接就可以生效了

xhtml

<div class= pngs ></div>

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

css

 

.png div{position:relative;}
xhml:
<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 == 6.0) && (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 先判断是否 IE ,然后判断 ie 版本,版本在 6.0 下则判定函数,给 png 的图片添加滤镜。

使用起来的确方便,无论多少图片都可以解决,但是依然无法 repeat

3 htc 方法

htc 相当于完全通过插件的方法修复的 IE6 bug ,功能强大,支持 repeat ,背景等功能,使用起来也很方便。

使用一个 iepngfix.htc  文件,和一个透明的 gif 文件。

使用方法:

<! [if lte IE 6]>

<style>.png{behavior:url( jscss/iepngfix.htc );}</style>  // 在这里可以加入其他用到 png 图片的 id 或者 class

<script type= text/javascript ”  src= jscss/iepngfix_tilebg.js ></script>

<![endif]

ps :如果需要 repeat 背景,往往需要设置这个 div  宽度为 100%

使用见: http://qqgame.qq.com/act/a20091210prize4/index.htm  ,在其中有 iepngfix.htc 可下载。

总结这几种方法,第三种方法是最简单使用,且容易推广的方法,建议可以做个公共的地址,有产品需要,只需要应用这个公共地址就行了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics