js去除浏览器默认底图的方法
本文实例讲述了js去除浏览器默认底图的方法。分享给大家供大家参考。具体分析如下:
我们在设计一些图片比较多的网页时,为了增强用户体验,希望图片加载的时候有个loading动画效果,而不是由空白到一下子出来。
在zencart的二次开发过程中同样也遇到了这个问题,下面是我的解决方案。
首页给图片设置一个默认的loading动画,再分配一个id,
如<img id="loading1″ src="loading.gif">实际上加载过程通过一个函数来完成
functionaddListener(element,type,expression,bubbling){
bubbling=bubbling||false;
if(window.addEventListener){//Standard
element.addEventListener(type,expression,bubbling);
returntrue;
}
elseif(window.attachEvent){//IE
element.attachEvent('on'+type,expression);
returntrue;
}
elsereturnfalse;
}
varImageLoader=function(url){
this.url=url;
this.image=null;
this.loadEvent=null;
};
ImageLoader.prototype={
load:function(){
this.image=document.createElement_x('img');
varurl=this.url;
varimage=this.image;
varloadEvent=this.loadEvent;
addListener(this.image,'load',function(e){
if(loadEvent!=null){
loadEvent(url,image);
}
},false);
this.image.src=this.url;
},
getImage:function(){
returnthis.image;
}
};
functionloadImage(objId,urls){
varloader=newImageLoader(urls);
loader.loadEvent=function(url){
obj=document.getElementByIdx_x(objId);
obj.src=url;
}
loader.load();
}
通过loadImage函数就可以为指定的图片添加加载过程,其中通过addListener函数注册事件,
使得在图片加载完成的时候能够自动替换掉loading.gif这个动画过渡图片。使用代码很简单
<imgid="loading1″src="loading.gif"/>
<scriptlanguage="javascript">
loadImage("loading1″,"http://www.baidu.com/img/baidu_logo.gif");
</script>
希望本文所述对大家的javascript程序设计有所帮助。