需求:检查页面上的图片是否加载完成,再执行其他方法
<div class="loadImg" style="display:none;"> <img src="https://cswmcs.teamsh.com/static_material/app/foca/1.jpg" alt="" /> <img src="https://cswmcs.teamsh.com/static_material/app/foca/2.jpg" alt="" /> <img src="https://cswmcs.teamsh.com/static_material/app/foca/3.jpg" alt="" /> <img src="https://cswmcs.teamsh.com/static_material/app/foca/4.jpg" alt="" /> <img src="https://cswmcs.teamsh.com/static_material/app/foca/5.jpg" alt="" /> <img src="https://cswmcs.teamsh.com/static_material/app/foca/6.jpg" alt="" /> </div>
我需要检查loadIng中的图片是否全部加载完成
var images = $('.loadImg img'); var loadedImages = 0; images.each(function() { var img = new Image(); console.log('img',img) img.onload = function() { loadedImages++; if(loadedImages === images.length) { console.log('所有图片加载完成'); //执行其他方法 } }; img.src = $(this).attr('src');//将页面上的图片地址遍历赋值给定义的图片src });