js 或者 jq怎么判断所有图片加载完成?

js 或者 jq怎么判断所有图片加载完成?

动漫人物
浏览 454回答 5
5回答

幕布斯6054654

用document的onload事件,或者如果只关心图片,就挂接img的load事件。$('img').on('load', function() {    // xxx});

交互式爱情

&nbsp;/**&nbsp;*加载图片&nbsp;* @param arr ['图片链接']&nbsp;* @returns {Array}&nbsp;*/function loadImg(arr) {&nbsp; &nbsp; var newimages = [];&nbsp; &nbsp; var arr = (typeof arr != "object") ? [arr] : arr&nbsp; //确保参数总是数组&nbsp; &nbsp; for (var i = 0; i < arr.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; newimages[i] = new Promise(function (resolve, reject) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var image = new Image();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image.addEventListener('load', function listener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resolve(image);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.removeEventListener('load', listener);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image.src = arr[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image.addEventListener('error', reject);&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }&nbsp; &nbsp; return newimages}Promise.all(loadImg(listPics)).then(function () {&nbsp; &nbsp; console.log('图片加载完成')});之前项目使用过的加载全部图片的处理方案,希望能帮助你

牧羊人nacy

用到js图片的onload事件,jq的load

料青山看我应如是

那得预先知道图片的数量了,加一个计数器,在load回调函数中var i = 0;$('img').load(function(){&nbsp; &nbsp; i++;&nbsp; &nbsp; if(i==x){&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; }});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript