为何透明图片IE6下有阴影

来源:互联网 发布:wechat mac download 编辑:程序博客网 时间:2024/04/30 01:35

1.涉及web前端网页开发,常常遇到浏览器兼容问题。对于IE不同版本的浏览器在css样式上会有所区别:下面举例说明png格式图片兼容问题。

下面是非IE6显示效果:

下图是IE6上显示效果:

登录框png格式透明的图片,如不加处理直接在IE6下会出现灰色阴影,效果非常难看。

解决方法

方法一、也是最笨的办法,将图片换成gif或jpg格式的。(但是这样效果可能不及png)

方法二、在该jsp页面的中加入下面js代码


<script type="text/javascript">

//correntPNG   alphaBackgrounds这两个方法用于解决ie6 对png格式的透明图片背景变灰的问题

function correctPNG()
{
    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 ;
          }
       }
    }   
}
function alphaBackgrounds(){
  var rslt = navigator.appVersion.match(/MSIE (d+.d+)/,'');
  var itsAllGood = (rslt != null && Number(rslt[1]) >=5.5);
 
  for(i=0; i<document.all.length; i++){
   var bg = document.all[i].currentStyle.backgroundImage;
   if (bg){
     if (bg.match(/.png/i) != null){
      var mypng = bg.substring(5,bg.length-2);
     
      document.all[i].style.filter ="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+ mypng + "', sizingMethod='crop')";
     
      document.all[i].style.backgroundImage = "url('')";
      }
    }
   }
  }
  if(navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent){
  
         window.attachEvent("onload",correctPNG);
   window.attachEvent("onload",alphaBackgrounds);

 window.attachEvent("onload", correctPNG);
 window.attachEvent("onload", alphaBackgrounds);
  }

 

</script>

0 0
原创粉丝点击