A-A+
IE6中Png格式图片透明
png图片透明在不同浏览器都会有不兼容这个问题,下面我来总结了几种解决IE6中Png格式图片透明的具体方法。
js实现方法,代码如下:
- document.write('<script type="text/javascript" id="ct" defer="defer" src="javascript:void(0)"></script>');var ct=document.getElementById("ct");ct.onreadystatechange=function(){pngfix()};pngfix=function(){var els=document.getElementsByTagName('*'),ip=/.png/i,al="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='",i=els.length,uels=new Array(),c=0;while(i-->0){if(els[i].className.match(/unitPng/)){uels=els[i];c++;}}if(uels.length==0)pfx(els);else pfx(uels);function pfx(els){i=els.length;while(i-->0){var el=els[i],es=el.style,elc=el.currentStyle,elb=elc.backgroundImage;if(el.src&&el.src.match(ip)&&!es.filter){es.height=el.height;es.width=el.width;es.filter=al+el.src+"',sizingMethod='crop')";el.src=clear;}else{if(elb.match(ip)){var path=elb.split('"'),rep=(elc.backgroundRepeat=='no-repeat')?'crop':'scale',elkids=el.getElementsByTagName('*'),j=elkids.length;es.filter=al+path[1]+"',sizingMethod='"+rep+"')";es.height=el.clientHeight+'px';es.backgroundImage='none';if(j!=0){if(elc.position!="absolute")es.position='static';while(j-->0)if(!elkids[j].style.position)elkids[j].style.position="relative";}}}}};};
2.页面头部,引入此js文件,代码如下:
- <script type="text/javascript" src="你的命名文件.js"></script>
3.添加clear.gif到你的images 文件夹中.在js文件中,修改"var clear="images/clear.gif" 路径,为你存放clear.gif的文件路径.
这中间是【】clear.gif图片,1*1的非常小。
这样你的整个项目的png图片都实现了透明效果.的确非常简单吧?就3个步骤,就实现了整个站点所有png的透明效果.
纯css实现方法二
IE5.5+的 AlphaImageLoader滤镜为通向png提供了一个道路,如果他载入的是PNG(Portable Network Graphics)格式,则0%-100%的透明度也被提供。我们就利用这个滤镜和hack来设计一个半透明png背景图片的模型。
解决方法:
首先,为你的图片外套一个DIV层,像这样,代码如下:
- <body> <div class=”flower”></div> </body>
然后,在CSS文件里面写入如下代码:
- body {background-color:#000}
- div.flower {background:url(flower-transparent.png) no-repeat; height:100px; width:100px}
呃,这是指Mozilla Firefox,要让其在IE6下面正常显示,你应该在head和/head之间写入如下代码:
- <!–[if gte IE 5]>
- <style type="text/css">
- div.flower {
- background:none;
- filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=’flower.png’ ,sizingMethod=’crop’);
- }
- </style>
- <![endif]–>
这样,就OK了,下面是加了上面代码后的IE显示图.
方法三,我自己使用的方法,代码如下:
- correctpng.js
- // JavaScript Document
- function correctPNG()
- {
- for(var i=0; i<document.images.length; i++)
- {
- var img = document.images[i]
- var imgimgName = 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
- ii = 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方法不支持重复背景,如果背景重复的话就只能显示一个,别的都支持
学习收藏了,IE6也要兼顾!