3种让PNG在IE6下透明的方案

时间:2010年02月28日作者:青雨被围观 688 次评论次数:1

现在做很多页面,比如阴影,或很多情况要用到透明图,GIF图一般会有很多锯齿,
用PNG图吧,效果到是还好。Firefox和IE7以上版本都支持PNG透明,但IE6缺会显示灰白。

第一种,适用于图片做背景的时候。代码:

.first { width:400px; margin:0 auto; height:300px; background:url(images/bg1.png); }
 
* html .first { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src="images/bg1.png"); background:none; }

第二种,适用于PNG图片放在页面上的时候

/*-------------IE6-PNG透明------------------------*/
* html img,
* html .png{
    behavior: e­xpression((this.runtimeStyle.behavior="none")&&(this.pngSet?this.pngSet=true:(this.nodeName == "IMG" && this.src.toLowerCase().indexOf('.png')>-1?(this.runtimeStyle.backgroundImage = "none",
        this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src + "', sizingMethod='image')",
        this.src = "images/transparent.gif"):(this.origBg = this.origBg? this.origBg :this.currentStyle.backgroundImage.toString().replace('url("','').replace('")',''),
        this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.origBg + "', sizingMethod='crop')",
        this.runtimeStyle.backgroundImage = "none")),this.pngSet=true)
 
    );
}
/*-------------------------------------------------------------------------*/

第三种,放在你网页的<head>里就可以了

<script type="text/javascript">
function correctPNG() 
   {
   for(var i=0; i<document.images.length; 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 = "<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
   i = i-1
      }
      }
   }
window.attachEvent("onload", correctPNG);
</script>
标签:,分类:Html/Css
1条评论
  1. 卡优留言于:2010年02月28日11:47

    有兴趣做友情链接吗?可以到邮件回复我。

发表评论

*

*