jQuery event for images loaded

后端 未结 14 1208
离开以前
离开以前 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:38

    maybe this plugin may be useful: http://www.farinspace.com/jquery-image-preload-plugin/

    0 讨论(0)
  • 2020-11-22 15:39
    $( "img.photo" ).load(function() {
    
        $(".parrentDiv").css('height',$("img.photo").height());
        // very simple
    
    }); 
    
    0 讨论(0)
  • window.load = function(){}

    Its fully supported by all browsers, and it will fire an event when all images are fully loaded.

    https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload

    0 讨论(0)
  • 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/

    0 讨论(0)
  • 2020-11-22 15:46

    There's a note on the ahpi.imgload.js plugin at the moment saying that it is currently broken, and to try this gist instead: https://gist.github.com/797120/7176db676f1e0e20d7c23933f9fc655c2f120c58

    0 讨论(0)
  • 2020-11-22 15:48

    I created my own script, because I found many plugins to be quite bloated, and I just wanted it to work the way I wanted. Mine checks to see if each image has a height (native image height). I've combined that with the $(window).load() function to get around the issues of caching.

    I've code commented it quite heavily, so it should be interesting to look at, even if you don't use it. It works perfectly for me.

    Without further ado, here it is:

    imgLoad.js script

    0 讨论(0)
提交回复
热议问题