;(function(root,factory){if(typeofdefine==='function'&&define.amd){//AMDdefine(factory);}elseif(typeofexports==='object'){//Node,CommonJS之类的module.exports=factory();}else{root.resLoader=factory(root);//root是windowwindow.resLoader=(function(){})(window)这里的window有特殊含义么?还是只是一个匿名立即执行函数}}(this,function(){varisFunc=function(f){returntypeoff==='function';}//构造器函数functionresLoader(config){this.option={resourceType:'image',//资源类型,默认为图片baseUrl:'./',//基准urlresources:[],//资源路径数组onStart:null,//加载开始回调函数,传入参数totalonProgress:null,//正在加载回调函数,传入参数currentIndex,totalonComplete:null//加载完毕回调函数,传入参数total}if(config){for(iinconfig){this.option[i]=config[i];}}else{alert('参数错误!');return;}this.status=0;//加载器的状态,0:未启动1:正在加载2:加载完毕this.total=this.option.resources.length||0;//资源总数this.currentIndex=0;//当前正在加载的资源索引};resLoader.prototype.start=function(){this.status=1;var_this=this;varbaseUrl=this.option.baseUrl;for(vari=0,l=this.option.resources.length;ivarr=this.option.resources[i],url=''; if(r.indexOf('http://')===0||r.indexOf('https://')===0){url=r;}else{url=baseUrl+r;}varimage=newImage();image.onload=function(){_this.loaded();};image.onerror=function(){_this.loaded();};image.src=url;}if(isFunc(this.option.onStart)){this.option.onStart(this.total);}}resLoader.prototype.loaded=function(){if(isFunc(this.option.onProgress)){this.option.onProgress(++this.currentIndex,this.total);console.log(this.currentIndex,this.total)}//加载完毕if(this.currentIndex===this.total){if(isFunc(this.option.onComplete)){this.option.onComplete(this.total);}}}//暴露公共方法returnresLoader;}));varloader=newresLoader({resources:['http://p2.qhimg.com/t01ed1438874f940dc0.jpg','http://p9.qhimg.com/t01b4ff03b72c7dc6c7.jpg','http://p2.qhimg.com/t01dd90dfbec92074d0.jpg','http://p7.qhimg.com/t01cfec6d87cde457c5.jpg','http://p9.qhimg.com/t01943ced462da67833.jpg','http://p0.qhimg.com/t01943ced462da67833.jpg','http://p6.qhimg.com/t01aa15a7ba7ccb49a7.jpg','http://p8.qhimg.com/t010f1e8badf1134376.jpg','http://p8.qhimg.com/t01cf37ea915533a032.jpg','http://p3.qhimg.com/t0193d8a3963e1803e9.jpg','http://p3.qhimg.com/t01cd6a4d4b4bd4457b.jpg'],onStart:function(total){console.log('start:'+total);},onProgress:function(current,total){console.log(current+'/'+total);varpercent=current/total*100;console.log(percent,current,total)},onComplete:function(total){alert('加载完毕:'+total+'个资源');}});loader.start();root.resLoader=factory(root);root是windowwindow.resLoader=(function(){})(window)这里的window有特殊含义么?还是只是一个匿名立即执行函数还有就是这种支持amdcommmon的写法有个模式名称么?
慕的地6264312
紫衣仙女
相关分类