让IE6显示透明PNG背景图片

核心代码CSS:

  1. {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 地址指定背景图像。假如忽略此参数,滤镜将不会作用。


已知问题:
滤镜会使图片覆盖在文本层之上,使超链接和按钮等失效。并没有设置为背景的选项。

尚不完美的解决方法:
将div、链接或按钮设置相对位置,使之浮动。

1.此法文字仍会显示为在半透明下的模糊效果。
2.div/label/gridview等长度的变化将不会引起容器长度的自动适应。

摘几段代码和方法:

把下面的代码放在head区就可以解决问题了。
<!--[if gte IE 5.5000]><script type="text/javascript" src="js/pngfix.js"></script><![endif]-->
pngfix.js文件的程序代码

  1. function correctPNG() 
  2. {
  3. for(var i=0; i<document.images.length; i++)
  4. {
  5.  var img = document.images[i]
  6.  var imgName = img.src.toUpperCase()
  7.  if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
  8.  {
  9.   var imgID = (img.id) ? "id='" + img.id + "' " : ""
  10.   var imgClass = (img.className) ? "class='" + img.className + "' " : ""
  11.   var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
  12.   var imgStyle = "display:inline-block;" + img.style.cssText 
  13.   if (img.align == "left") imgStyle = "float:left;" + imgStyle
  14.   if (img.align == "right") imgStyle = "float:right;" + imgStyle
  15.   if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle  
  16.   var strNewHTML = "<span " + imgID + imgClass + imgTitle
  17.   + " style=/"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
  18.  + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
  19.   + "(src=/'" + img.src + "/', sizingMethod='scale');/"></span>" 
  20.   img.outerHTML = strNewHTML
  21.   i = i-1
  22.  }
  23. }
  24. }
  25. window.attachEvent("onload", correctPNG);

另一个官方解决方案的核心函数

  1. /* 
  2. Correctly handle PNG transparency in Win IE 5.5 & 6. 
  3. Copyright 2007 Ignia, LLC 
  4. Based in part on code from from http://homepage.ntlworld.com/bobosola. 
  5. Use in  with DEFER keyword wrapped in conditional comments: 
  6. <script type="text/javascript" defer="true" src="pngfix.js"></script> 
  7. */ 
  8. function fixPng() { 
  9.   var arVersion = navigator.appVersion.split("MSIE"
  10.   var version = parseFloat(arVersion[1]) 
  11.   if ((version >= 5.5 && version < 7.0) && (document.body.filters)) { 
  12.     for(var i=0; i<document.images.length;></document.images.length;>      var img = document.images[i]; 
  13.       var imgName = img.src.toUpperCase(); 
  14.       if (imgName.indexOf(".PNG") > 0) { 
  15.         var width = img.width; 
  16.         var height = img.height; 
  17.         var sizingMethod = (img.className.toLowerCase().indexOf("scale") >= 0)? "scale" : "image"
  18.         img.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img.src.replace('%23', '%2523').replace("'", "%27") + "', sizingMethod='" + sizingMethod + "')"
  19.         img.src="images/blank.gif" mce_src="images/blank.gif"
  20.         img.width = width; 
  21.         img.height = height; 
  22.         } 
  23.       } 
  24.     } 
  25.   } 
辨别浏览器功能
Firefox、Opera等完全支持PNG透明图片的浏览器也支持子选择器(>),而IE不识别(包括IE7),所有我们可以通过这来定义Firefox、Opera等浏览器中PNG图片的样式。如下
html>body #png {background: url(images/bg.png) repeat;}
而对于IE,我们都通过滤镜的方法来定义,或许有人问,IE7不是支持PNG透明图片吗?是的,不错,IE7是支持PNG透明图片,但IE7也支持AlphaImageLoader滤镜,为了避免使用PNG图片和滤镜冲突造成的不便,我们统一在IE中使用AlphaImageLoader滤镜。我们通过只有IE才识别的通配符(*),来定义IE浏览器中的滤镜。如下:

  1. * html #png {
  2. filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src="images/bg.png");
  3. background:none;
  4. }
这样综合起来的决绝方法就是:

  1. html>body #png {backgroundurl(images/bg.png) repeat;}
  2. html #png {
  3. filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src="images/bg.png");
  4. background:none;
  5. }
此外,还可以参考参考
http://dean.edwards.name/IE7/
http://code.google.com/p/ie7-js/
http://dean.edwards.name/weblog/2008/01/ie7-2/

代码原理都是滤镜,都无法正确处理背景.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值