NO.2 IE6下png透明的问题

本文提供了解决IE6浏览器下PNG图片背景不透明问题的方法。通过JavaScript和CSS滤镜实现PNG图片及背景图片的正常显示。
最近的在工作中也有遇到png图片在ie6下面不透明,会出现灰色的一方块背景的问题。

 

以前我的解决方法是直接把png的图片换成gif,但是这样会使得有些图片出现锯齿,影响了美观。

于是去网上搜了解决这个问题的办法:

 

ContractedBlock.gifExpandedBlockStart.gifCode
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>无标题文档</title>  
<script type="text/javascript"><!--   
function fixPng() {    
  
var arVersion = navigator.appVersion.split("MSIE")    
  
var version = parseFloat(arVersion[1])    
    
  
if ((version &gt;= 5.5 &amp;&amp; version &lt; 7.0&amp;&amp; (document.body.filters)) {    
    
for(var i=0; i<document.images.length;></document.images.length;>      var img = document.images[i];    
      var imgimgName = img.src.toUpperCase();    
      
if (imgName.indexOf(".PNG"&gt; 0) {    
        
var width = img.width;    
        
var height = img.height;    
        
var sizingMethod = (img.className.toLowerCase().indexOf("scale"&gt;= 0)? "scale" : "image";    
        img.runtimeStyle.filter 
= "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img.src.replace('%23''%2523').replace("'""%27"+ "', sizingMethod='" + sizingMethod + "')";    
        img.src
="images/blank.gif" mce_src="images/blank.gif";    
        img.width 
= width;    
        img.height 
= height;    
        }    
      }    
    }    
  }    
// -->
</script>  
</head>  
  
<body>  
    
<img src="pic.png" mce_src="pic.png" onload="fixPNG(this)" />  
</body>  
</html>  

 

此方法解决了页面中直接插入png图片的背景透明问题,

接着就解决当png作为背景图片时的png图片背景透明问题:

 

ContractedBlock.gifExpandedBlockStart.gifCode
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>背景图为png时</title>  
  
<mce:style type="text/css"><!--   
.pic{   
height: 150px;   
width: 150px;   
background-image: url(pic.png)!important;/* FF IE7 */   
background-repeat: no-repeat;   
  
_filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='pic.png'); /* IE6 */   
_ background-image: none; /* IE6 */   
}   
--></mce:style><style type="text/css" mce_bogus="1">.pic{   
height
: 150px;   
width
: 150px;   
background-image
: url(pic.png)!important;/* FF IE7 */   
background-repeat
: no-repeat;   
  
_filter
: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='pic.png'); /* IE6 */   
_ background-image
: none; /* IE6 */   
}</style>  
</head>  
<body>  
<div class="pic"></div>  
</body>  
</html>  

 经过验证此方法也成功了!

转载于:https://www.cnblogs.com/00fairy00/archive/2009/11/10/1599923.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值