[英]Using jQuery, how can I check when all images have loaded within a dynamic dom structure?
I'm adding dynamic content (including images) to the body. 我正在向身体添加动态内容(包括图像)。 I want to be able to tell when the images within the new dynamic dom structure are all finished loading. 我想知道新动态dom结构中的图像何时全部加载完毕。
HTML: HTML:
<button>
Add dynamic content
</button>
JS: JS:
$(document).ready(function() {
// Append new .dynamic-container divs to the body
$('button').click(function() {
var newElement = $('<div class="dynamic-container"><div class="images-container"><img src="https://s3-us-west-2.amazonaws.com/cdn.simplyrets.com/properties/trial/home4.jpg" /><img src="https://s3-us-west-2.amazonaws.com/cdn.simplyrets.com/properties/trial/home-inside-4.jpg" /><img src="https://s3-us-west-2.amazonaws.com/cdn.simplyrets.com/properties/trial/home15.jpg" /><img src="https://s3-us-west-2.amazonaws.com/cdn.simplyrets.com/properties/trial/home12.jpg" /><img src="https://s3-us-west-2.amazonaws.com/cdn.simplyrets.com/properties/trial/home-inside-15.jpg" /></div></div>');
$('body').append(newElement);
// Perform an operation whenever all images are loaded within a newly created div.dynamic-container element
newElement.on('load', 'img', function(){
alert('all images have loaded');
});
});
});
Unfortunately this is not working. 不幸的是,这不起作用。 What am I missing? 我想念什么?
I have created a JsFiddle to show what I'm doing: https://jsfiddle.net/cseckler/y3mpo37d/ 我创建了一个JsFiddle来显示我在做什么: https ://jsfiddle.net/cseckler/y3mpo37d/
You can't rely on the load
event if you append the images to the document (or even create them with a src
) prior to hooking load
, so what you want to do is check for completed images; 如果在挂接load
之前将图像添加到文档(甚至使用src
创建图像),则不能依赖load
事件,因此,您要做的就是检查完整的图像; see comments: 看评论:
// Perform an operation whenever all images are loaded within a newly created div.dynamic-container element
var imgs = newElement.find("img[src]"); // [src] = skip ones with no source
imgs.on("load", checkImages);
checkImages();
function checkImages() {
var counter = 0;
imgs.each(function() {
if (this.complete) {
++counter;
}
});
if (counter === imgs.length) {
imgs.off("load", checkImages);
// They're all loaded or failed; perform your operation
}
}
Live Example: 现场示例:
$(document).ready(function() { // Append new .dynamic-container divs to the body $('button').click(function() { var newElement = $('<div class="dynamic-container"><div class="images-container"><img src="https://s3-us-west-2.amazonaws.com/cdn.simplyrets.com/properties/trial/home4.jpg" /><img src="https://s3-us-west-2.amazonaws.com/cdn.simplyrets.com/properties/trial/home-inside-4.jpg" /><img src="https://s3-us-west-2.amazonaws.com/cdn.simplyrets.com/properties/trial/home15.jpg" /><img src="https://s3-us-west-2.amazonaws.com/cdn.simplyrets.com/properties/trial/home12.jpg" /><img src="https://s3-us-west-2.amazonaws.com/cdn.simplyrets.com/properties/trial/home-inside-15.jpg" /></div></div>'); $('body').append(newElement); // Perform an operation whenever all images are loaded within a newly created div.dynamic-container element var imgs = newElement.find("img[src]"); // [src] = skip ones with no source imgs.on("load", checkImages); checkImages(); function checkImages() { var counter = 0; imgs.each(function() { if (this.complete) { ++counter; } }); if (counter === imgs.length) { imgs.off("load", checkImages); // They're all loaded or failed; perform your operation console.log("All done!"); } } }); });
img { max-width: 300px; }
<button> Add dynamic content </button> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
That uses the complete
flag on img
elements: 在img
元素上使用complete
标志 :
The IDL attribute complete must return true if any of the following conditions is true: 如果满足以下任一条件,则IDL属性complete必须返回true:
- The
src
attribute is omitted. 省略src
属性。- The final task that is queued by the networking task source once the resource has been fetched has been queued. 一旦资源被获取,联网任务源将要排队的最终任务已排队。
- The
img
element is completely available.img
元素完全可用。- The
img
element is broken.img
元素已损坏。Otherwise, the attribute must return false. 否则,该属性必须返回false。
(Note: "attribute" in the above doesn't mean "attribute" in the HTML sense, just the IDL sense; complete
is a property .) (注意:上面的“属性”在HTML含义上并不意味着“属性”,而只是IDL含义; complete
是一个属性 。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.