[英]How can i know if all <img> loaded in div using jQuery
How can i know if all loaded in div using jQuery 我怎么知道是否全部使用jQuery加载到div中
i want to do this after load all img in #slider div 我想在#slider div中加载所有img后执行此操作
var imgHeight = $("#slider img").height();
alert(imgHeight);
You can use the load
event 您可以使用
load
事件
$('#slider img').load(function(){
var imgHeight = $(this).height();
alert(imgHeight);
});
if there are more than one image, and you only want to obtain the height after they have all loaded, try this code 如果有多个图像,并且只想在它们全部加载后获得高度,请尝试以下代码
var img = $('#slider img');
var length = img.length;
img.load(function(){
length--;
if(length === 0){
alert("All images loaded");
};
});
Well, I've tested the code, and it appears that the problem hasn't got anything to do with the code. 好吧,我已经测试了代码,看来问题与代码无关。 When loading the page with the images already in the cache, this is what I get:
当将页面中已包含图像的图像加载到缓存中时,这是我得到的:
Strangely, this does not occur when I force the browser not to use the cache. 奇怪的是,当我强制浏览器不使用缓存时,不会发生这种情况。
Try this: 尝试这个:
onload
event listener to each image in the slider. onload
事件侦听器附加到滑块中的每个图像。 alert(imageHeight)
) alert(imageHeight)
) untested: 未经测试:
(function(){
var slider=document.getElementById('slider'),
images=slider.getElementsByTagName('img'), image, i=-1;
while (image=images[i]) {
image.onload=function(){
this['data-ready']=true;
var image, i=-1;
while (image=images[i]) if (!image['data-ready']) return;
// all images are ready; do your thing here
// ...
}
}
}());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.