`
helloyesyes
  • 浏览: 1295393 次
  • 性别: Icon_minigender_2
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

ie png图片显示不正常

阅读更多

png图片有很好的品质。阴影效果也不会有杂边,很流畅。如果插入网页的话可以给网站内容增色不少!更重要的是在不增加图片容量大小的情况下提高了页面的图片的质量。对于有复杂背景,如:在有颜色过度背景上插入不规则边框的图片带来极大很便利!

  但目前IE中对于插入的透明背景的.png的图片是不能正常显示的。IE会自动给".png"格式的图片加个灰色背景。

  解决这个的方法是增加javascript。具体方法如下:

  把下面的代码放在head区就可以解决问题了。

我测试发现IE7.0可以正常显示出来,IE6及IE6以下不可以。

以下是引用片段:
<script language="javascript">
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")

//这里不是很好,因为图片的格式不是由扩展名决定的,这里可以由server的方法去得到image真正的format传过来检查
{
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);
</script>


  也可以把这段代码单独加在一张图片上:


<!--<span
style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='testpng.png',sizingMethod='scale');"></span> -->

<IMG alt="" src="testpng.png">

------------------------------------------------------------------------------------------------------

PNG.JS代码:
// PNGHandler: Object-Oriented Javascript-based PNG wrapper
// --------------------------------------------------------
// Version 1.1.20031218
// Code by Scott Schiller - www.schillmania.com
// --------------------------------------------------------
// Description:
// Provides gracefully-degrading PNG functionality where
// PNG is supported natively or via filters (Damn you, IE!)
// Should work with PNGs as images and DIV background images.

function PNGHandler() {
var self = this;

this.na = navigator.appName.toLowerCase();
this.nv = navigator.appVersion.toLowerCase();
this.isIE = this.na.indexOf('internet explorer')+1?1:0;
this.isWin = this.nv.indexOf('windows')+1?1:0;
this.ver = this.isIE?parseFloat(this.nv.split('msie ')[1]):parseFloat(this.nv);
this.isMac = this.nv.indexOf('mac')+1?1:0;
this.isOpera = (navigator.userAgent.toLowerCase().indexOf('opera ')+1 || navigator.userAgent.toLowerCase().indexOf('opera/')+1);
if (this.isOpera) this.isIE = false; // Opera filter catch (which is sneaky, pretending to be IE by default)

this.transform = null;

this.getElementsByClassName = function(className,oParent) {
doc = (oParent||document);
matches = [];
nodes = doc.all||doc.getElementsByTagName('*');
for (i=0; i<nodes.length; i++) {
if (nodes[i].className == className || nodes[i].className.indexOf(className)+1 || nodes[i].className.indexOf(className+' ')+1 || nodes[i].className.indexOf(' '+className)+1) {
matches[matches.length] = nodes[i];
}
}
return matches; // kids, don't play with fire. ;)
}

this.filterMethod = function(oOld) {
// IE 5.5+ proprietary filter garbage (boo!)
// Create new element based on old one. Doesn't seem to render properly otherwise (due to filter?)
// use proprietary "currentStyle" object, so rules inherited via CSS are picked up.

var o = document.createElement('div'); // oOld.nodeName
var filterID = 'DXImageTransform.Microsoft.AlphaImageLoader';
// o.style.width = oOld.currentStyle.width;
// o.style.height = oOld.currentStyle.height;

if (oOld.nodeName == 'DIV') {
var b = oOld.currentStyle.backgroundImage.toString(); // parse out background image URL
oOld.style.backgroundImage = 'none';
// Parse out background image URL from currentStyle object.
var i1 = b.indexOf('url("')+5;
var newSrc = b.substr(i1,b.length-i1-2).replace('.gif','.png'); // find first instance of ") after (", chop from string
o = oOld;
o.style.writingMode = 'lr-tb'; // Has to be applied so filter "has layout" and is displayed. Seriously. Refer to http://msdn.microsoft.com/workshop/author/filter/reference/filters/alphaimageloader.asp?frame=true
o.style.filter = "progid:"+filterID+"(src='"+newSrc+"',sizingMethod='crop')";
// Replace the old (existing) with the new (just created) element.
// oOld.parentNode.replaceChild(o,oOld);
} else if (oOld.nodeName == 'IMG') {
var newSrc = oOld.getAttribute('src').replace('.gif','.png');
// apply filter
oOld.src = 'none.gif'; // get rid of image
oOld.style.filter = "progid:"+filterID+"(src='"+newSrc+"',sizingMethod='crop')";
oOld.style.writingMode = 'lr-tb'; // Has to be applied so filter "has layout" and is displayed. Seriously. Refer to http://msdn.microsoft.com/workshop/author/filter/reference/filters/alphaimageloader.asp?frame=true
}
}

this.pngMethod = function(o) {
// Native transparency support. Easy to implement. (woo!)
bgImage = this.getBackgroundImage(o);
if (bgImage) {
// set background image, replacing .gif
o.style.backgroundImage = 'url('+bgImage.replace('.gif','.png')+')';
} else if (o.nodeName == 'IMG') {
o.src = o.src.replace('.gif','.png');
} else if (!this.isMac) {
// window.status = 'PNGHandler.applyPNG(): Node is not a DIV or IMG.';
}
}

this.getBackgroundImage = function(o) {
var b, i1; // background-related variables
var bgUrl = null;

if (o.nodeName == 'DIV' && !(this.isIE && this.isMac)) { // ie:mac PNG support broken for DIVs with PNG backgrounds
if (document.defaultView) {
if (document.defaultView.getComputedStyle) {
b = document.defaultView.getComputedStyle(o,'').getPropertyValue('background-image');
i1 = b.indexOf('url(')+4;
bgUrl = b.substr(i1,b.length-i1-1);
} else {
// no computed style
}
} else {
// no default view
}
}
return bgUrl;
}

this.supportTest = function() {
// Determine method to use.
// IE 5.5+/win32: filter

if (this.isIE && this.isWin && this.ver >= 5.5) {
// IE proprietary filter method (via DXFilter)
self.transform = self.filterMethod;
} else if (!this.isIE && this.ver < 5) {
self.transform = null;
// No PNG support or broken support
// Leave existing content as-is
} else if (!this.isIE && this.ver >= 5 || (this.isIE && this.isMac && this.ver >= 5)) { // version 5+ browser (not IE), or IE:mac 5+
self.transform = self.pngMethod;
} else {
// Presumably no PNG support. GIF used instead.
self.transform = null;
return false;
}
return true;
}

this.init = function() {
if (this.supportTest()) {
this.elements = this.getElementsByClassName('png');
for (var i=0; i<this.elements.length; i++) {
this.transform(this.elements[i]);
}
}
}

}

// Instantiate and initialize PNG Handler

var pngHandler = new PNGHandler();

DEMO页HTML代码:
<html>
<head>
<title>Schillmania! | png.js demo</title>
<script type="text/javascript" src="png.js"></script>
</head>

<body>

<!--

Don't copy this part here, this is just for your information.

// Required under the <head> tag:

<script type="text/javascript" src="png.js"></script>

// Required in the <body> tag:

<img src="your-image.gif" class="png" style="width:XXXpx;height:YYYpx" />

// ..Where XXX and YYY are the width/height of your image. Without width/height the PNG won't work under IE:win32.

// Required before the </body> tag (but after all of your content):

<script type="text/javascript">
pngHandler.init();
</script>

// This javascript block should be put at the bottom of your page, inside the <body> and before </body>.
// It calls the library and replaces the GIF images (if applicable) before the page has loaded (and before the GIF has loaded, So the user doesn't load 2 images for each PNG you want to replace.)
// If you don't put it after all of your content, it may do strange things and miss some images.

-->

<h1>PNG (img)</h1>

<img src="foo.gif" alt="" class="png" style="width:220px;height:100px" />

<h1>PNG (div with background image)</h1>

<div class="png" style="width:220px;height:100px;background-image:url(foo.gif);background-repeat:no-repeat">

</div>

<script type="text/javascript">
pngHandler.init();
</script>

</body>
</html>

源码及DEMO打包下载:
详细出处参考:http://www.jb51.net/article/11196.htm

分享到:
评论

相关推荐

    IE无法显示png图片 IE部分图片显示为叉叉的解决办法

    莫名其妙的问题,IE 看网页中的部分图片是红叉,同样的网址,Firefox 中正常。...【IE无法显示png图片 IE部分图片显示为叉叉的解决办法】 http://www.cnblogs.com/oskycar/archive/2009/06/24/1510054.html

    ie6图片png完美支持iepngfix

    在网页中引入iepngfix.js文件,并调用其方法,可以实现在IE6中展示带有透明效果的PNG图片,从而提高了用户体验,特别是在那些需要保持一致视觉效果的跨浏览器项目中。 使用iepngfix库的基本步骤如下: 1. **引入...

    js修复IE不能显示PNG图片透明背景的方法

    ### js修复IE不能显示PNG图片透明背景的方法 #### PNG图片及其特性 PNG(Portable Network Graphics)是一种常用的位图图形格式,以其高效的压缩算法和对透明度的支持而受到广泛青睐。相较于传统的GIF格式,PNG...

    IE浏览器png图片显示补丁

    2. **透明处理**:修复了PNG图片的Alpha通道透明度问题,使得半透明和完全透明的区域能够正确显示。 3. **性能优化**:尽可能地减少对浏览器性能的影响,提供流畅的用户体验。 4. **易用性**:只需要将idpngfix.htc...

    ie6显示png图片

    4. **应用滤镜**:利用IE6特有的CSS滤镜属性,例如`filter: progid:DXImageTransform.Microsoft.AlphaImageLoader`,iepngfix将PNG图片的透明部分加载到新创建的`&lt;div&gt;`上,从而使透明效果得以实现。 5. **更新样式...

    处理IE6浏览器显示.png图片透明度文件

    在互联网的早期,Internet Explorer 6 (IE6) 是广泛使用的浏览器之一,但它存在许多兼容性问题,其中就包括PNG图片透明度的问题。PNG(Portable Network Graphics)是一种支持透明度的图像格式,但在IE6中,它无法...

    VB 透明PNG图片显示控件

    "VB 透明PNG图片显示控件"正是为了解决这一需求而设计的。 首先,我们要理解PNG图像的透明性是通过Alpha通道实现的。Alpha通道不仅包含红色、绿色和蓝色信息,还包含一个透明度级别,范围从0(完全透明)到255...

    ie6下png透明图片显示不正常之最好的3种解决方案

    在互联网早期,IE6(Internet Explorer 6)作为当时最常用的浏览器,存在许多兼容性问题,其中PNG(Portable Network Graphics)格式的透明图片在IE6下显示不正常就是其中之一。PNG格式,特别是PNG-24格式,支持半...

    ie 下png 透明图片 兼容解决方案

    这些版本的浏览器无法正确显示24位PNG图像的Alpha透明效果,导致网页设计者面临挑战。本篇文章将深入探讨这个问题,并提供多种解决方法。 首先,我们需要理解PNG图像格式。PNG是一种无损压缩的图像格式,支持透明度...

    ie浏览器不能显示png格式图片的问题解决

    - **在线工具**:例如TinyPNG、Convertio等,这些工具可以快速将PNG图片转换为其他格式。 - **桌面软件**:Adobe Photoshop、GIMP等图像编辑软件也支持格式转换功能。 #### 四、注意事项 - 在进行任何涉及注册表...

    PNG图片在IE6中非正常显示的处理方法

    PNG图片在Internet Explorer 6(简称IE6)中可能会遇到显示问题,这主要是由于IE6对PNG格式图像的不完全支持。PNG是一种无损压缩的位图格式,它提供了透明度的支持,但在IE6上,尤其是对于24位PNG图片,透明效果可能...

    PNG图片IE6下面解决最好的方法

    PNG图片在Internet Explorer 6(简称IE6)下的显示问题是一个历史遗留的挑战,由于IE6对PNG格式的不完全支持,导致透明效果和部分渲染出现异常。PNG是一种优秀的无损压缩图像格式,广泛用于网页设计,特别是需要半...

    DD_belatedPNG.js解决IE6浏览器下的PNG透明图片显示问题

    这主要源于IE6不支持PNG8以上的Alpha透明度,导致透明PNG图片在该浏览器下显示为黑色背景或者完全不透明。为了解决这个问题,开发者们创造了一种名为“DD_belatedPNG”的JavaScript库,它通过JavaScript模拟了PNG...

    IE显示不了PNG的解决办法

    然而,有时在使用Internet Explorer(IE)浏览器时,可能会遇到PNG图片无法正常显示的问题。这通常是由于浏览器的配置问题或与MIME类型相关的注册表设置不正确所导致的。 在Windows系统中,MIME类型是一种标准,...

    旧浏览器PNG图片透明效果脚本 IEPNGFix v2

    然而,早期版本的Internet Explorer(尤其是IE5.5至IE7)对PNG8和PNG24格式的透明度支持不足,导致这些浏览器中的PNG图片无法正确显示透明效果。 "IEPNGFix v2" 是一个专门针对这个问题的JavaScript库,它通过动态...

    使IE6正常显示PNG-24格式图片代码

    为了解决这个问题,开发人员通常会采用以下两种方法来使IE6能够正常显示PNG-24格式的图片。 方法一: 这种方法是通过JavaScript来解决的。这段代码首先检查当前浏览器是否为IE6,如果是,则执行一段JavaScript函数`...

    PNG图片透明兼容IE6和火狐示例

    通过在CSS中引入`behavior:url(iepngfix.htc)`,可以使得IE6正确显示PNG的透明部分。 5. **CSS使用**:在CSS中,可以使用`background-image`属性设置PNG图片作为背景,同时通过`opacity`或`filter`属性控制透明度,...

    IE6中觉得PNG格式图片阴影的方法

    通过在CSS中引入这个文件,并将其应用到需要修复的元素上,我们可以使IE6正确显示带阴影的PNG图片。尽管这种方法在当时是一个有效的解决方案,但随着浏览器的更新换代,现在已经推荐使用更现代的浏览器和适应性更强...

    png透明显示IE6

    标题“png透明显示IE6”涉及的是一个古老但仍然具有一定现实意义的问题,即如何在Internet Explorer 6(简称IE6)浏览器中正确显示PNG格式图片的透明效果。PNG是一种流行的图像格式,支持丰富的颜色和透明度,但在IE...

    IE6 png背景图片透明

    只需引入库并调用相应函数,即可使PNG图片在IE6下变得透明。 3. **CSS Hack**:开发者还可以利用CSS选择器的特定性来为IE6单独设置样式。例如,可以使用`*html`前缀或者`_height`这样的非标准属性,将透明滤镜应用...

Global site tag (gtag.js) - Google Analytics