摘要: PNG 是可携式网络图像Portable Network Graphics这三个单词的第一个字母的缩写。她和 GIF 一样支持透明背景,但其对透明的处理质量远远大于 GIF,逐渐成为网页制作者非常喜欢的一种图片格式。令人无奈的是,IE 却不支持 PNG 图片的透明处理,本文介绍的方法就是针对这一问题的。
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) ? ” ” : “”;
var imgTitle = (myImage.title) ? “title='” + myImage.title + “‘ ” : “title='” + myImage.alt + “‘ “;
var imgStyle = “display:inline-block;” + myImage.style.cssText;
var strNewHTML = ” + ” style=”” + “width:” + myImage.width
+ “px; height:” + myImage.height
+ “px;” + imgStyle + “;”
+ “filter:progid:DXImageTransform.Microsoft.AlphaImageLoader”
+ “(src='” + myImage.src + “‘, sizingMethod=’scale’);”>”;
myImage.outerHTML = strNewHTML;
}
}
将上面的代码另存为 js 文件,然后在页面中插入:
Code:
因为 png 图片只有在 IE 中才会有不透明的问题,所以只需在用 IE 浏览时才加载代码,注意要在调用代码中加上 DEFER 关键词,这样会延迟代码执行时间。
关于 DEFER :
Quote:
它告诉浏览器Script段包含了无需立即执行的代码,并且,与SRC属性联合使用,它还可以使这些脚本在后台被下载,前台的内容则正常显示给用户。
请注意两点:
1、不要在defer型的脚本程序段中调用document.write命令,因为document.write将产生直接输出效果。
2、而且,不要在defer型脚本程序段中包括任何立即执行脚本要使用的全局变量或者函数。
在页面中需要用到 PNG 图片的地方,添加
Code:
onload=”fixPNG(this)”
如:
Code:
<img style=”border:1px” src=”apple.png;” alt=”PNG” />
We use cookies to improve your experience on our site. By using our site, you consent to cookies.
Manage your cookie preferences below:
Essential cookies enable basic functions and are necessary for the proper function of the website.
These cookies are needed for adding comments on this website.
Statistics cookies collect information anonymously. This information helps us understand how visitors use our website.
Google Analytics is a powerful tool that tracks and analyzes website traffic for informed marketing decisions.
Service URL: policies.google.com
You can find more information in our Cookie Policy and Cookie Policy.