Official way to ask jQuery wait for all images to load before executing something

前端 未结 10 2369
攒了一身酷
攒了一身酷 2020-11-22 00:04

In jQuery when you do this:

$(function() {
   alert(\"DOM is loaded, but images not necessarily all loaded\");
});

It waits for the DOM to

10条回答
  •  别那么骄傲
    2020-11-22 00:16

    Use imagesLoaded PACKAGED v3.1.8 (6.8 Kb when minimized). It is relatively old (since 2010) but still active project.

    You can find it on github: https://github.com/desandro/imagesloaded

    Their official site: http://imagesloaded.desandro.com/

    Why it is better than using:

    $(window).load() 
    

    Because you may want to load images dynamically, like this: jsfiddle

    $('#button').click(function(){
        $('#image').attr('src', '...');
    });
    

提交回复
热议问题