做东西遇到显示页面百分比的加载进度,不过里面的图片较多,看了别人的代码,才想到可以通过图片的加载显示加载的百分比,做一下笔记:
html:
<span id="percent"></span>
js:
function perFun(){ var imgs = document.getElementsByTagName("img"); var len = 0; var percent = document.getElementById("percent"); for(var i=0; i<imgs.length; i++){ (function(i){ imgs[i].onload = function(){ len ++; percent.innerText = Math.round(len*100/imgs.length); } })(i); } } perFun();