1. 程式人生 > >資源百分比預加載

資源百分比預加載

如果 func hid html () continue span length pic

JS代碼 :

var loadNum = 0,
    resource = [
        ‘aaa.gif‘,
        ‘1.gif‘,
        ‘2.gif‘,
        ‘https://gss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/d439b6003af33a872e9fa4dac35c10385243b504.jpg‘,
        ‘images/layer1.jpg‘,
        ‘images/layer2.jpg‘,
        ‘images/layer3.jpg‘
    ],
    resLen 
= resource.length; for (var i = 0; i < resLen; i++) { var Images = new Image(); Images.src = resource[i]; if (Images.complete) { //如果緩存裏有這張圖片,就是觸發complete loadNum++; var percent = Math.floor(loadNum / resLen * 100); setNum(percent); continue; } Images.onload
= function() { //圖片加載 loadNum++; var percent = Math.floor(loadNum / resLen * 100); setNum(percent); }; } function setNum(percent) { $("#bfb").text(percent); //設置百分數到DOM上 if (percent == 100) { setTimeout(function() { // do something; }, 300); } }

html:

<div id="bfb"></div>

資源百分比預加載