看了网上各种方法,选了个比较好的,
说明:网上说可以解决hover问题,但是我试过不知道为什么不行,(可以看之前的ie6解决hover的文章)但是用来解决网页直接引用的各个小图标还是很棒的,毕竟你用gif的小图标的话,锯齿很严重,png的要好太多。
方法如下:
1.下载DD_belatedPNG.js,代码如下:
PS:此代码已经在最后加入,整理好放入相应文件夹,不用多说
DD_belatedPNG.fix(".pngFix,.pngFix:hover");
1 var DD_belatedPNG={ns:"DD_belatedPNG",imgSize:{},delay:10,nodesFixed:0,createVmlNameSpace:function(){ if(document.namespaces&&!document.namespaces[this.ns]){document.namespaces.add(this.ns,"urn:schemas-microsoft-com:vml")}},createVmlStyleSheet:function(){ var b,a;b=document.createElement("style");b.setAttribute("media","screen");document.documentElement.firstChild.insertBefore(b,document.documentElement.firstChild.firstChild);if(b.styleSheet){b=b.styleSheet;b.addRule(this.ns+"\\:*","{behavior:url(#default#VML)}");b.addRule(this.ns+"\\:shape","position:absolute;");b.addRule("img."+this.ns+"_sizeFinder","behavior:none; border:none; position:absolute; z-index:-1; top:-10000px; visibility:hidden;");this.screenStyleSheet=b;a=document.createElement("style");a.setAttribute("media","print");document.documentElement.firstChild.insertBefore(a,document.documentElement.firstChild.firstChild);a=a.styleSheet;a.addRule(this.ns+"\\:*","{display: none !important;}");a.addRule("img."+this.ns+"_sizeFinder","{display: none !important;}")}},readPropertyChange:function(){ var b,c,a;b=event.srcElement;if(!b.vmlInitiated){ return}if(event.propertyName.search("background")!=-1||event.propertyName.search("border")!=-1){DD_belatedPNG.applyVML(b)}if(event.propertyName=="style.display"){c=(b.currentStyle.display=="none")?"none":"block";for(a in b.vml){ if(b.vml.hasOwnProperty(a)){b.vml[a].shape.style.display=c}}}if(event.propertyName.search("filter")!=-1){DD_belatedPNG.vmlOpacity(b)}},vmlOpacity:function(b){ if(b.currentStyle.filter.search("lpha")!=-1){ var a=b.currentStyle.filter;a=parseInt(a.substring(a.lastIndexOf("=")+1,a.lastIndexOf(")")),10)/100;b.vml.color.shape.style.filter=b.currentStyle.filter;b.vml.image.fill.opacity=a}},handlePseudoHover:function(a){setTimeout(function(){DD_belatedPNG.applyVML(a)},1)},fix:function(a){if(this.screenStyleSheet){var c,b;c=a.split(",");for(b=0;bn.H){i.B=n.H}d.vml.image.shape.style.clip="rect("+i.T+"px "+(i.R+a)+"px "+i.B+"px "+(i.L+a)+"px)"}else{d.vml.image.shape.style.clip="rect("+f.T+"px "+f.R+"px "+f.B+"px "+f.L+"px)"}},figurePercentage:function(d,c,f,a){var b,e;e=true;b=(f=="X");switch(a){case"left":case"top":d[f]=0;break;case"center":d[f]=0.5;break;case"right":case"bottom":d[f]=1;break;default:if(a.search("%")!=-1){d[f]=parseInt(a,10)/100}else{e=false}}d[f]=Math.ceil(e?((c[b?"W":"H"]*d[f])-(c[b?"w":"h"]*d[f])):parseInt(a,10));if(d[f]%2===0){d[f]++}return d[f]},fixPng:function(c){c.style.behavior="none";var g,b,f,a,d;if(c.nodeName=="BODY"||c.nodeName=="TD"||c.nodeName=="TR"){ return}c.isImg=false;if(c.nodeName=="IMG"){ if(c.src.toLowerCase().search(/\.png$/)!=-1){c.isImg=true;c.style.visibility="hidden"}else{ return}}else{ if(c.currentStyle.backgroundImage.toLowerCase().search(".png")==-1){ return}}g=DD_belatedPNG;c.vml={color:{},image:{}};b={shape:{},fill:{}};for(a in c.vml){ if(c.vml.hasOwnProperty(a)){ for(d in b){ if(b.hasOwnProperty(d)){f=g.ns+":"+d;c.vml[a][d]=document.createElement(f)}}c.vml[a].shape.stroked=false;c.vml[a].shape.appendChild(c.vml[a].fill);c.parentNode.insertBefore(c.vml[a].shape,c)}}c.vml.image.shape.fillcolor="none";c.vml.image.fill.type="tile";c.vml.color.fill.on=false;g.attachHandlers(c);g.giveLayout(c);g.giveLayout(c.offsetParent);c.vmlInitiated=true;g.applyVML(c)}};try{document.execCommand("BackgroundImageCache",false,true)}catch(r){}DD_belatedPNG.createVmlNameSpace();DD_belatedPNG.createVmlStyleSheet();window.οnlοad=function(){DD_belatedPNG.fix(".pngFix,.pngFix:hover");}
2.引入此JS,在需要透明的标签上加入class="pngFix"即可,简单 · 方便 · 快捷!
如下图:
注意:此处的js我注释掉了,因为前面写入到DD_belatedPNG.js里面了。
3.大功告成!
优点:
- CSS代码无需任何修改,按照平时的思路来写即可;
- 无需配置;
- 没有多余的gif图片;
- 支持img;
- 支持平铺;
- 支持CSS Sprite;
- 支持Hover等伪类;(你实现了,请务必评论给我怎么回事,我没有实现支持)
缺点:
- 额外加入了js文件(6.39k)和http请求,可以忽略不计;
- 当文件载入之前,会先暂时呈现灰底;
- js文件过多的时候,可能会报错,导致js无法正常运行(这种情况极少出现,可以忽略不计);
参考:http://www.cnblogs.com/yjzhu/archive/2012/11/09/2762059.html