1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| !function(undefined){ var PreLoad=function(a,b){var c=b||{};this.source=a,this.count=0,this.total=a.length,this.onload=c.onload,this.prefix=c.prefix||"",this.init()};PreLoad.prototype.init=function(){var a=this;a.source.forEach(function(b){var c=b.substr(b.lastIndexOf(".")+1).toLowerCase(),d=a.prefix+b;switch(c){case"js":a.script.call(a,d);break;case"css":a.stylesheet.call(a,d);break;case"svg":case"jpg":case"gif":case"png":case"jpeg":a.image.call(a,d)}})},PreLoad.prototype.getProgress=function(){return Math.round(this.count/this.total*100)},PreLoad.prototype.image=function(a){var b=document.createElement("img");this.load(b,a),b.src=a},PreLoad.prototype.stylesheet=function(a){var b=document.createElement("link");this.load(b,a),b.rel="stylesheet",b.type="text/css",b.href=a,document.head.appendChild(b)},PreLoad.prototype.script=function(a){var b=document.createElement("script");this.load(b,a),b.type="text/javascript",b.src=a,document.head.appendChild(b)},PreLoad.prototype.load=function(a,b){var c=this;a.onload=a.onerror=a.onabort=function(a){c.onload&&c.onload({count:++c.count,total:c.total,item:b,type:a.type})}};
var tasks = [ 'http://h5.m.jd.com/active/4V4Gr3KMtGRxrLc3aktGGwa8hbHy/pages/5918/img/bg1.jpg', 'http://h5.m.jd.com/active/4V4Gr3KMtGRxrLc3aktGGwa8hbHy/pages/5918/img/bg2.jpg' ] var $progress = document.getElementById('progress')
function loading(load) { var count = load.count var total = load.total $progress && ($progress.innerHTML = Math.round(100*count/total) + '%') if(count === total) return complete() }
function next(el, fn) { el.className += ' scaleOut' setTimeout(function(){ el.parentNode.removeChild(el) fn && fn() }, 800) }
function complete() { var $loader = document.getElementById('loader');
next($loader, function() { }) }
new PreLoad(tasks, {onload: loading}) }();
|