一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

css/js实现png图片ie6下背景透明实现代码

时间:2012-06-18 编辑:简简单单 来源:一聚教程网

在IE6直接显示(包括内容中直接插入、作为背景图片)PNG-24格式的图片,是不能正确显示透明、半透明内容与其他内容的叠加呈现效果的。那些IE7+以及其他标准浏览器中漂亮的虚化、淡出、投影效果,在IE6-中很可能成了一坨难看的灰色。

  如果你无法忍受ie6中难看的灰色,如果你无法放弃半透明叠加的效果,你会有机会遇到这个问题的。解决途径有不少。这里列举一些,以供参考:

1,修改设计效果,使之可以整块透明区域被切片而不影响显示效果。或者去掉半透明效果。

2,htc文件
  这里的htc,和宏达手机是无关的。目前它仅被IE5.5+支持。
   点击这里下载演示文件。
  此方案既适用于内容中直接插入图片的情况,也适用于背景图片的情况。在网上可以搜索到更多的相关介绍,但非本文推荐的方法,不多细说。

3,Flash替换
  意思就是,把需要用到半透明效果的地方,用flash来表现……是不是听着就很头疼?
  只适用于直接插入图片的情况。同样,非本文推荐的方案,不多细说。

4,IE滤镜
  此方法适用于背景图片的情况。直接插入图片的情况亦可改为用背景图片,从而采用此方法。
  具体做法:
  比如现在有一个容器,样式名为.pngBox,在支持PNG-24的浏览器中,使用样式定义:

 代码如下 复制代码

.pngBox {
.background: url("translucent.png") no-repeat 0 0;
.}

  在IE5.5/IE6中,就不需要这个背景图了,而通过用滤镜来加载图片。即:

 代码如下 复制代码

.pngBox {
.filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='translucent.png',sizingMethod='scale');
}

  值得注意的是,使用滤镜时,图片的src应该是相对网页文件的路径,而非相对css文件的路径;或者使用绝对路径。
  综上,考虑兼容性的定义可为:
 

 代码如下 复制代码
.pngBox {
.background: url("translucent.png") no-repeat 0 0;
._filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='translucent.png',sizingMethod='scale');
._background: none;
}

  AlphaImageLoader的用法的详细介绍,请在网上搜索。

5,JS替换法
  此JS替换方法基于第4点中的IE滤镜办法,适用于一个页面需要批量地加入透明png图片。

  为了插入一张图片所用到的css定义就很多,而且还要去想图片路径的问题。如果是多张图片,那定义背景的代码就一大片了。这个JS可以只需要关注在哪使用png图片,其他css代码会自动构造

JS实现PNG图片IE6下背景透明代码收藏,直接拿去用吧,不多说什么!!

 代码如下 复制代码

function correctPNG()
   {
   for(var i=0; i       {
     var img = document.images[i]
     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 = "        + " style="" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
        + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
       + "(src='" + img.src + "', sizingMethod='scale');">
"
       img.outerHTML = strNewHTML
       i = i-1
        }
      }
   }
function alphaBackgrounds(){
   var rslt = navigator.appVersion.match(/MSIE (d+.d+)/, '');
   var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
   for (i=0; i       var bg = document.all[i].currentStyle.backgroundImage;
      if (bg){
         if (bg.match(/.png/i) != null){
            var mypng = bg.substring(5,bg.length-2);
   //alert(mypng);
            document.all[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+mypng+"', sizingMethod='crop')";
            document.all[i].style.backgroundImage = "url('')";
   //alert(document.all[i].style.filter);
         }                                             
      }
   }
}

if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
window.attachEvent("onload", correctPNG);
window.attachEvent("onload", alphaBackgrounds);
}

现在只要调用本js在要实现透明的页面,然后在页面中加入png图片就可以实现背景透明了。

热门栏目