//PNGHandler:Object-OrientedJavascript-basedPNGwrapper
//--------------------------------------------------------
//Version1.1.20031218
//CodebyScottSchiller-www.schillmania.com
//--------------------------------------------------------
//Description:
//Providesgracefully-degradingPNGfunctionalitywhere
//PNGissupportednativelyorviafilters(Damnyou,IE!)
//ShouldworkwithPNGsasimagesandDIVbackgroundimages.
functionPNGHandler(){
varself=this;
this.na=navigator.appName.toLowerCase();
this.nv=navigator.appVersion.toLowerCase();
this.isIE=this.na.indexOf('internetexplorer')+1?1:0;
this.isWin=this.nv.indexOf('windows')+1?1:0;
this.ver=this.isIE?parseFloat(this.nv.split('msie')[1]):parseFloat(this.nv);
this.isMac=this.nv.indexOf('mac')+1?1:0;
this.isOpera=(navigator.userAgent.toLowerCase().indexOf('opera')+1||navigator.userAgent.toLowerCase().indexOf('opera/')+1);
if(this.isOpera)this.isIE=false;//Operafiltercatch(whichissneaky,pretendingtobeIEbydefault)
this.transform=null;
this.getElementsByClassName=function(className,oParent){
doc=(oParent||document);
matches=[];
nodes=doc.all||doc.getElementsByTagName('*');
for(i=0;i
matches[matches.length]=nodes[i];
}
}
returnmatches;//kids,don'tplaywithfire.;)
}
this.filterMethod=function(oOld){
//IE5.5+proprietaryfiltergarbage(boo!)
//Createnewelementbasedonoldone.Doesn'tseemtorenderproperlyotherwise(duetofilter?)
//useproprietary"currentStyle"object,sorulesinheritedviaCSSarepickedup.
varo=document.createElement('div');//oOld.nodeName
varfilterID='DXImageTransform.Microsoft.AlphaImageLoader';
//o.style.width=oOld.currentStyle.width;
//o.style.height=oOld.currentStyle.height;
if(oOld.nodeName=='DIV'){
varb=oOld.currentStyle.backgroundImage.toString();//parseoutbackgroundimageURL
oOld.style.backgroundImage='none';
//ParseoutbackgroundimageURLfromcurrentStyleobject.
vari1=b.indexOf('url("')+5;
varnewSrc=b.substr(i1,b.length-i1-2).replace('.gif','.png');//findfirstinstanceof")after(",chopfromstring
o=oOld;
o.style.writingMode='lr-tb';//Hastobeappliedsofilter"haslayout"andisdisplayed.Seriously.Refertohttp://msdn.microsoft.com/workshop/author/filter/reference/filters/alphaimageloader.asp?frame=true
o.style.filter="progid:"+filterID+"(src='"+newSrc+"',sizingMethod='crop')";
//Replacetheold(existing)withthenew(justcreated)element.
//oOld.parentNode.replaceChild(o,oOld);
}elseif(oOld.nodeName=='IMG'){
varnewSrc=oOld.getAttribute('src').replace('.gif','.png');
//applyfilter
oOld.src='none.gif';//getridofimage
oOld.style.filter="progid:"+filterID+"(src='"+newSrc+"',sizingMethod='crop')";
oOld.style.writingMode='lr-tb';//Hastobeappliedsofilter"haslayout"andisdisplayed.Seriously.Refertohttp://msdn.microsoft.com/workshop/author/filter/reference/filters/alphaimageloader.asp?frame=true
}
}
this.pngMethod=function(o){
//Nativetransparencysupport.Easytoimplement.(woo!)
bgImage=this.getBackgroundImage(o);
if(bgImage){
//setbackgroundimage,replacing.gif
o.style.backgroundImage='url('+bgImage.replace('.gif','.png')+')';
}elseif(o.nodeName=='IMG'){
o.src=o.src.replace('.gif','.png');
}elseif(!this.isMac){
//window.status='PNGHandler.applyPNG():NodeisnotaDIVorIMG.';
}
}
this.getBackgroundImage=function(o){
varb,i1;//background-relatedvariables
varbgUrl=null;
if(o.nodeName=='DIV'&&!(this.isIE&&this.isMac)){//ie:macPNGsupportbrokenforDIVswithPNGbackgrounds
if(document.defaultView){
if(document.defaultView.getComputedStyle){
b=document.defaultView.getComputedStyle(o,'').getPropertyValue('background-image');
i1=b.indexOf('url(')+4;
bgUrl=b.substr(i1,b.length-i1-1);
}else{
//nocomputedstyle
}
}else{
//nodefaultview
}
}
returnbgUrl;
}
this.supportTest=function(){
//Determinemethodtouse.
//IE5.5+/win32:filter
if(this.isIE&&this.isWin&&this.ver>=5.5){
//IEproprietaryfiltermethod(viaDXFilter)
self.transform=self.filterMethod;
}elseif(!this.isIE&&this.ver<5){
self.transform=null;
//NoPNGsupportorbrokensupport
//Leaveexistingcontentas-is
}elseif(!this.isIE&&this.ver>=5||(this.isIE&&this.isMac&&this.ver>=5)){//version5+browser(notIE),orIE:mac5+
self.transform=self.pngMethod;
}else{
//PresumablynoPNGsupport.GIFusedinstead.
self.transform=null;
returnfalse;
}
returntrue;
}
this.init=function(){
if(this.supportTest()){
this.elements=this.getElementsByClassName('png');
for(vari=0;i
}
}
}
}
//InstantiateandinitializePNGHandler
varpngHandler=newPNGHandler();
DEMO页HTML代码:
PNG(img)
PNG(divwithbackgroundimage)