当前位置: 首页 > 图文教程 > 网络编程 > Javascript > IE浏览器PNG图片透明效果代码

Javascript
用new Image()预先加载图片真的有用吗?
超级susymenu实现top菜单
新版本susymenu树形菜单,请大家指教、分享
javascript入门·图片对象(无刷新变换图片)\滚动图像
国外Lightbox v2.03.3 最新版 下载
javascript渐变显示的雅虎中国选项卡效果代码
用javascript来实现select标签的美化的代码
用javascript实现改善用户体验之alert提示效果
Auntion-TableSort javascript类文件
javascript 新闻切换技术
JS代码判断集锦大全
[原创]用javascript实现检测指定目录是否存在的方法
JavaScript中的其他对象
[原创]来自ImageSee官方 JavaScript图片浏览器
用js 让图片在 div或dl里 居中,底部对齐
网页代码常用小技巧总结
可以自动轮换的页签 tabs with auto play fucntion
js判断一点是否在一个三角形内
DHTML Slide Show script图片轮换
JS event使用方法详解

Javascript 中的 IE浏览器PNG图片透明效果代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-12   浏览: 131 ::
收藏到网摘: n/a

平常,我们经常使用Gif格式的图片以保持图片在浏览器中透明,以支持底色。但由于IE浏览器本身的原因,我们无法使透明的PNG图片透明起来。那么,如何将它在IE浏览器下变得透明呢?

首先看我们的<img>标签代码:
<img src="Example.png" border="0" alt="放大镜" />
我们将利用IE中特有的特效来满足这个要求,这就是AlphaImageLoader Filter (http://msdn.microsoft.com/library/default.asp?url=/workshop/author/filter/reference/filters/alphaimageloader.asp)
如何做?
将下面代码保存为correctPNG.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; margin:6px; 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);

然后在你需要透明的网页中的<head>....</head>区加入:<script type="text/javascript" src="correctPNG.js"></script>
在<body>区加入多个与<img src="Example.png" border="0" alt="放大镜" />类似的PNG图片,试试看?
另一种方法:
复制代码 代码如下:

<html>
<head>
<title>alpha image</title>
<style type="text/css">
.pngholder{
width:100px;
height:100px;
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://www.ruanchen.com/');
}
.pngalpha{
filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);
background:url(http://www.ruanchen.com/"#3399ff#">
<!- And this is your code to implement the image ->
<div>透明</div>
<div class="pngholder"><div class="pngalpha"></div></div>
<div>不透明</div>
<img src="http://www.ruanchen.com/"/>
</body>
</html>