开发者

How can i find all the images are loaded in ajax using jquery

开发者 https://www.devze.com 2023-04-09 05:48 出处:网络
Is it possible to find whether all the images are loaded in ajax content? I have used var img = $(\'#slider img\');

Is it possible to find whether all the images are loaded in ajax content? I have used

var img = $('#slider img');
var length = img.开发者_JAVA技巧length;

img.load(function(){
    length--;

    if(length === 0){
        alert("All images loaded");
    };
});

but the load is not working, it is not goning in to the loop...


Try something like this.

$('#slider img').each(function() {
      //Write pre loading script
});


You need add event listener load before set up src attribute. Or you can scan images and check if every is loaded (by image size):

var img = $('#slider img');
var length = img.length;

img.each(function(){
    if ($(this).width() > 0 && $(this).height() > 0)
        length--;

    if(length === 0){
        alert("All images loaded");
    };
});

I not checked code.

0

精彩评论

暂无评论...
验证码 换一张
取 消