png在ie下显示不正常

pNG半透明背景图片效果
PNG图片在网页设计中扮演着一个很重要的角色,利用PNG图片的特点可以制作出实用,绚丽的效果,可是对于PNG图片的支持却不是很理想,Firefox和Opera对PNG支持的比较好,特别是Firefox浏览器。可是IE却不理PNG,使得设计者无法很随意的使用png图片。

IE5.5+的 AlphaImageLoader滤镜为通向png提供了一个道路,如果他载入的是PNG(Portable Network Graphics)格式,则0%-100%的透明度也被提供。我们就利用这个滤镜和hack来设计一个半透明png背景图片的模型。

经过多方面的实验比较了2种方法:css控制和js控制。总结如下:

css中镶入如下代码控制:

#png{background-image:url(images/jzz_02.png) no-repeat !important;
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled="true", sizingMethod="scale", src="images/jzz_02.png");
background:none;}

比较累,需要一个个添加代码。而且用了滤镜后背景图片不支持background-position定位

具体代码:

//总css文件 style.css

#png{

background:url(image.png) no-repeat center !important;
}

//CSS代码中镶入ie6配置CSS配置文件 ie6.css

#png{

background:none;
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='image.png',enabled='true', sizingMethod='scale');

}

//html格式

<head>
...
<link href="style.css" rel="stylesheet" type="text/css" />
<!--[if IE 6]>
<link href="ie6.css" rel="stylesheet" type="text/css" />
<![endif]-->
</head>
...

注:style.css文件里的图片url是相对于css文件的路径,ie6.css文件里src是相对于html文件的路径

JS控制:

比较方便,适合懒人- -!,但bug比较多,效率相对于css要低

比较常用的js代码:

1.

correctpng.js

// JavaScript Document

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
}
}
}
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);
//alert(mypng);
document.all[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+mypng+"', sizingMethod='crop')";
document.all[i].style.backgroundImage = "url('')";
//alert(document.all[i].style.filter);
}
}
}
}

html

<head>
...
<script type="javascript" src="correctpng.js"></script>

<!--[if IE 6]>

<script>

if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
window.attachEvent("onload", correctPNG);
window.attachEvent("onload", alphaBackgrounds);
}


</scrpit>

<![endif]-->

</head>

注 这个js方法不支持重复背景,如果背景重复的话就只能显示一个,别的都支持。

2.

使用先人写好的iepngfix类

这个类里面有3个文件

blank.gif替换为空白的图片

iepngfix.htc核心文件

iepngfix_tilebg.js貌似是控制重复背景的文件,不过我用了以后直接页面都显示不出- -!

具体用法:

在css文件里加入

img,div{

behavior: url("iepngfix.htc") ;

}

在html里head之间调用iepngfix_tilebg.js

注意最好blank.gif和htc文件放一个文件夹里 习惯都放在image文件夹里

总结:我个人认为使用css文件会比较好点,这对程序美观度和效率都会有一定的提高。

总结完毕。

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

另外的方法

新建一个叫png_fix.css的文件,内容如下:

Css代码 复制代码 收藏代码
  1. *htmlimg,*html.png{
  2. behavior:expression((this.runtimeStyle.behavior="none")&&(
  3. this.pngSet?this.pngSet=true:(this.nodeName=="IMG"&&
  4. this.src.toLowerCase().indexOf('.png')>-1?(
  5. this.runtimeStyle.backgroundImage="none",this.runtimeStyle.filter
  6. ="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+
  7. this.src+"',sizingMethod='image')",this.src=
  8. "image/transparent.gif"):(this.origBg=this.origBg?this.origBg:
  9. this.currentStyle.backgroundImage.toString().replace('url("','')
  10. .replace('")',''),this.runtimeStyle.filter=
  11. "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+
  12. this.origBg+"',sizingMethod='crop')",
  13. this.runtimeStyle.backgroundImage="none")),this.pngSet=true)
  14. );
  15. }
* html img,* html .png {
	behavior: expression(( 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 =  
		"image/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 )
		);
}

然后在HTML文件的<LINK>部分加入<> 最后自己做一个1×1像素的透明gif图片,存为transparent.gif。 使用的时候是这样: 1、如果是在HTML里面直接插入的PNG图片,就直接会处理掉; 2、如果是在css里面写的用作背景的图片,需要在

里面加上class="png",最后就是
; 3、如果是链接,需要加上cursor: pointer;测试下来使用没有问题,非常完美。

=========================================================================
亲测成功的方法

把下面的代码放在head区就可以解决问题了。
<!--[if gte IE 5.5000]><script type="text/javascript" src="js/pngfix.js"></script><![endif]-->
pngfix.js文件的程序代码
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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值