我正在尝试在加载预定义数组中的所有图像后调用函数。
像这样尝试smth
for (var i = 0; i < length; i++) {
d = $.Deferred();
promises.push(p);
img = new Image();
img.onload = function(){
p.resolve
}
img.src = srcs[i];
}
$.when.apply($, promises).done(function(){
defImages.resolve();
})
它不起作用。我做错了什么?
答案 0 :(得分:0)
//count of all page images
var img_count=$('img').length;
//images loaded
var img_loaded=0;
$('img').load(function(){
img_loaded++;
if(img_loaded==img_count){
console.log('all images loaded');
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f9/CC-Zero-badge.svg/100px-CC-Zero-badge.svg.png">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/16/CC-BY_icon.svg/100px-CC-BY_icon.svg.png">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d0/CC-BY-SA_icon.svg/100px-CC-BY-SA_icon.svg.png">
&#13;