jQuery event for images loaded

后端 未结 14 1196
离开以前
离开以前 2020-11-22 15:25

Is it possible to detect when all images are loaded via a jQuery event?

Ideally, there should be a

$(document).idle(function()
{
}

14条回答
  •  隐瞒了意图╮
    2020-11-22 15:44

    Waiting for all images to load...
    I found the anwser to my problem with jfriend00 here jquery: how to listen to the image loaded event of one container div? .. and "if (this.complete)"
    Waiting for the all thing to load and some possibly in cache !.. and i have added the "error" event... it's robust across all browsers

    $(function() { // Wait dom ready
        var $img = $('img'), // images collection
            totalImg = $img.length,
            waitImgDone = function() {
                totalImg--;
                if (!totalImg) {
                    console.log($img.length+" image(s) chargée(s) !");
                }
            };
        $img.each(function() {
            if (this.complete) waitImgDone(); // already here..
            else $(this).load(waitImgDone).error(waitImgDone); // completed...
        });
    });
    

    Demo : http://jsfiddle.net/molokoloco/NWjDb/

提交回复
热议问题