资源百分比预加载
Posted 智取小师妹的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了资源百分比预加载相关的知识,希望对你有一定的参考价值。
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>
以上是关于资源百分比预加载的主要内容,如果未能解决你的问题,请参考以下文章