如何使网页支持PNG透明(IE6、IE7、firefox下测试通过)
分类:网页设计 Tags: png 透明 兼容浏览器 评论:0 浏览:4598 最近更新时间:2008/10/18 23:09:43
IE6不支持PNG半透明图片的缺陷为web设计带来了极大的不便,之前曾经用滤镜+hack的方法实现显示PNG,不过实现起来相当繁琐。还有一种网上比较流行的方法,更加简便,下面详细介绍这种方法:
把以下代码保存为correctpng.js
在网页的头部引用一下
使用的时候直接用img标签即可。
把以下代码保存为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; height:" + img.height + "px;" + imgStyle + ";"
- + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
- + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
- img.outerHTML = strNewHTML
- i = i-1
- };
- };
- };
- if(navigator.userAgent.indexOf("MSIE")>-1)
- {
- window.attachEvent("onload", correctPNG);
- };
在网页的头部引用一下
- <SCRIPT language=JavaScript src="correctpng.js" type=text/javascript></SCRIPT>
使用的时候直接用img标签即可。
本文地址:/383/
-
• 如何有效防止网站出现垃圾留言,垃圾评论?
• 不错的photoshop样式免费下载,asl格式,Web2.0风格导航条与按钮效果
• 点击一个超级链接,能同时打开两个网页/网站的代码
• EASEUS Partition Master 5.0.1 Unlimited Edition无损磁盘分区调整工具教程
• 解决国外空间Access错误Selected collating sequence not supported by the operating system
• 10个国外墙纸图片素材网站
• js实现图片等比缩略显示代码,支持IE,Firefox浏览器
• 关于网站paypal在线支付接口代码问题
• FCKEditor2.5编辑器设置及修改方法For ASP,支持UTF-8编码
• 目前唯一支持中国银行卡付款的LunarPages.com美国空间/虚拟主机