通常在首屏资源尚未准备好之前,需要做首屏预加载。

1
2
3
4
5
6
@keyframes rotate{0%{transform:rotate(360deg);}100%{transform:rotate(0deg);}}
.loader{position:absolute;z-index:200;overflow:hidden;left:0;top:0;right:0;bottom:0;width:100%;height:100%;color:#fff;background-color: #2b2250;}
.loader-graph{overflow:hidden;position:absolute;left:50%;top:50%;width:50px;height:50px;margin:-25px 0 0 -25px;color:transparent;text-align:center;border-top:5px solid rgba(255,255,255,.2);border-right:5px solid rgba(255,255,255,.2);border-bottom:5px solid rgba(255,255,255,.2);border-left:5px solid #fff;border-radius:30px;animation:rotate .72s linear infinite}
.loader-progress{width:60px;height:60px;line-height:60px;font-weight:bold;font-size:14px;text-align:center;position: absolute;left: 50%;top: 50%;margin: -25px 0 0 -25px;}
.loader-text{position:absolute;left:0;top:50%;margin:40px 0 0 8px;width:100%;line-height:24px;font-size:16px;text-align:center;}
.scaleOut{opacity:0;transform:scale(1.25);transition:all 0.8s;}
1
2
3
4
5
<div id="loader" ontouchmove="return !1" class="loader">
<div class="loader-graph"></div>
<div id="progress" class="loader-progress">2%</div>
<div class="loader-text">LOADING...</div>
</div>
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() {
// loading层消失后的回调
})
}

new PreLoad(tasks, {onload: loading})
}();