png格式的图片在IE6 下透明解决方案

FF和IE7已经直接支持透明的png图了,下面这个主要是解决IE6下透明PNG图片有灰底的

style="FILTER: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=images/fl.png‘)"

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

特性:
Enabled : 可读写。布尔值(Boolean)。参阅 enabled 属性。
sizingMethod : 可读写。字符串(String)。参阅 sizingMethod 属性。
src : 可读写。字符串(String)。参阅 src 属性。

说明:
在对象容器边界内,在对象的背景和内容之间显示一张图片。并提供对此图片的剪切和改变尺寸的操作。如果载入的是PNG(Portable Network Graphics)格式,则0%-100%的透明度也被提供。
PNG(Portable Network Graphics)格式的图片的透明度不妨碍你选择文本。也就是说,你可以选择显示在PNG(Portable Network Graphics)格式的图片完全透明区域后面的内容。

实例:解决IE6下png透明失效的问题。

CSS样式:

.png{
_background: url(http://www.snowangel.cn/images/angel.png) no-repeat !important; 
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=noscale, src="http://www.snowangel.cn/images/angel.png");
background:none;
width:118px;height:133px;
}

.png div{position:relative;}

HTML代码:

<div class="png">
<div>
CSS 背景PNG透明 及 链接失效问题解决
</div>
</div>

/*
兼容IE6.0、IE7.0、FF,
IE7.0和新版的FF其实是可以不加filter滤镜直接透明的。
*/

=============================================================================

第 1 种方法:定义一个样式,给某个div应用这个样式后,div的透明png背景图片自动透明了。(注意两处图片的路径写法不一样,本例中,icon_home.png图片与html文件在相同目录)

<!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=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--
.qq {
height: 90px;
width: 90px;
background-image: url(icon_home.png)!important;/* FF IE7 */
background-repeat: no-repeat;

_filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=‘icon_home.png‘); /* IE6 */
_ background-image: none; /* IE6 */
}
-->
</style>
</head>

<body>

<div class="qq"></div>

</body>
</html>

第 2 种方法: 给img定义样式,页面上所有透明png即自动透明了。(这方法只对直接插入的图片有效,对背景图无效)注意,要准备一个透明的小图片transparent.gif,大小不限。必须放在和html相同的目录
请勿大量使用,否则会导致页面打开很慢!!!)

<!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=gb2312" />
<title>无标题文档</title>
<style type="text/css">
.mypng img {
azimuth: expression(
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 = "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);
}

</style>
</head>

<body>
换成你的png图片
<div class="mypng">
<img src="icon_face_07.png" width="30" height="30" />
<img src="icon_face_10.png" width="30" height="30" />
<img src="icon_face_08.png" width="30" height="30" />
</div>
</body>
</html>

第 3 种方法:用JS实现,加上一段js代码后,所有插入的透明png自动透明了.(注意,这方法也是只对直接插入的图片有效,对背景图无效

<!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=gb2312" />
<title>无标题文档</title>
<script language="JavaScript"> 
function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6. 

    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 
          } 
       } 
    }     

window.attachEvent("onload", correctPNG); 
</script>
<style type="text/css">
<!--
body {

}
-->
</style></head>

<body>
把图片换成你自己的图片
<img src="img/icon_face_03.png" width="30" height="30" /><!--把图片换成你自己的图片 -->
<img src="img/icon_face_05.png" width="30" height="30" />
<img src="img/menu_title_over.png" width="130" height="36" />
</body>
</html>

方法四

<script language="javascript">
// 修复 IE 下 PNG 图片不能透明显示的问题
function fixPNG(myImage) {
var arVersion = navigator.appVersion.split("MSIE");
var version = parseFloat(arVersion[1]);
if ((version >= 5.5) && (version < 7) && (document.body.filters))
{
     var imgID = (myImage.id) ? "id=‘" + myImage.id + "‘ " : "";
     var imgClass = (myImage.className) ? "class=‘" + myImage.className + "‘ " : "";
     var imgTitle = (myImage.title) ? "title=‘" + myImage.title   + "‘ " : "title=‘" + myImage.alt + "‘ ";
     var imgStyle = "display:inline-block;" + myImage.style.cssText;
     var strNewHTML = "<span " + imgID + imgClass + imgTitle

+ " style=\"" + "width:" + myImage.width

+ "px; height:" + myImage.height

+ "px;" + imgStyle + ";"

+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"

+ "(src=\‘" + myImage.src + "\‘, sizingMethod=‘scale‘);\"></span>";
     myImage.outerHTML = strNewHTML;
} }

window.onload=function(){
         document.getElementById("top").style.height=screen.height/5+"px";
        
}//
</script>

用法如下:
<img src="logo.png" width="328" height="325" border="0" />

时间: 2024-08-01 22:46:07

png格式的图片在IE6 下透明解决方案的相关文章

前端问题——png图片在IE6下透明失效,解决办法

今天,一位同事问我问题,png 图片在IE6下透明背景失效. 解决办法,在网上查了很多,最后还是采用两种方案来解决这个问题 1.把这个网页的png格式图片变更为gif格式的图片.问题解决 2.就是让这个网页引用一段JS代码,如下: 1 if (!window.XMLHttpRequest) { 2 window.attachEvent("onload", enableAlphaImages); 3 } 4 5 function enableAlphaImages(){ 6 for (v

2.png图片在ie6下不透明

写在前面的话: 下面这些方法也是找来的,原文链接也都附上了~ 1.  ie6png背景图 if(window.XMLHttpRequest){ //Mozilla,Safari,IE7 if(!window.ActiveXObject){ //Mozilla,Safari, alert('Mozilla,Safari'); }else{ alert('IE7'); } }else{ alert('IE6'); } ie6 png 背景图  可以用滤镜使其透明: _background:none;

web标准:img图片在ie6下显示空白的bug解决方案

在进行页面的DIV+CSS排版时,遇到IE6(当然有时Firefox下也会偶遇)浏览器中的图片元素img下出现多余空白的问题绝对是常见的对于该问题的解决方法也是“见机行事”. 1.将图片转换为块级对象 即,设置img为“display:block;”.在本例中添加一组CSS代码:“#sub img {display:block;}”. 2.设置图片的垂直对齐方式 即设置图片的vertical-align属性为“top,text-top,bottom,text-bottom”也可以解决.如本例中增

Img图片在IE6下显示空白的Bug解决方案

在进行页面的DIV+CSS排版时,遇到IE6(当然有时Firefox下也会偶遇)浏览器中的图片元素img下出现多余空白的问题绝对是常见的对于该问题的解决方法也是“见机行事”. 1.将图片转换为块级对象 即,设置img为“display:block;”.在本例中添加一组CSS代码:“#sub img {display:block;}”. 2.设置图片的垂直对齐方式 即设置图片的vertical-align属性为“top,text-top,bottom,text-bottom”也可以解决.如本例中增

IE6 PNG透明解决方案(国外)

<!--[if lte IE 6]>  <script src="Js/DD_belatedPNG_0.0.8a.js" type="text/javascript"></script>  <script type="text/javascript">  DD_belatedPNG.fix('div, ul, img, li, input , a, .png_bg');  /* 将 .png_bg 

使IE6下PNG背景透明的七种方法任你选

原文地址:http://blog.csdn.net/mosliang/article/details/6760028 相信如何解决png在ie6下透明的问题困扰了很多人.为了追求更好的页面效果,很多人喜欢使用png格式的图片,恰恰因为ie6下png图片的透 明问题,很多人不得不退而求其次地改用gif格式的图片.今天就来介绍一个ie6下png透明的解决办法,支持background(含 position.repeat.img src). 1. DD_belatedPNG简介: DD_belated

解决IE6下png-24使用滤镜(_filter)透明问题 效果出不来

今天说说IE6下使用滤镜(_filter)解决png-24透明效果遇到的一些问题: css代码:background:url(../images/button1.png) no-repeat;_background:none; _filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/button1.png"); 在使用上述代码解决IE6下透明效果时,有的人会出现明明使用了上述代码,为什么没有出现想要的

js动态改变图片src在ie6下的兼容问题

场景:验证码图片在ie6下出不来 这个BUG折腾了我近半个小时,最后GOOGLE成功,原因是IE6的底层BUG,我们使用<a>的href属性改变的时候会中断加载,导致我们看不到图片,这经常在验证码或动态改变图片的时候遇到. 所以我们在使用a 链接时通常href和onclick都会同时用到,链接的 onclick 事件被先执行,其次是 href 属性下的动作(页面跳转,或 javascript 伪链接),假设链接中同时存在 href 与 onclick,如果想让 href 属性下的动作不执行,o

IE6 png 透明 (三种解决方法)(转来的哦)

FF和IE7已经直接支持透明的png图了,下面这个主要是解决IE6下透明PNG图片有灰底的 ============================================================================= 此效果简单,....相当不错 推荐 style="FILTER: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=images/fl.png')" 语法:filter : prog