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

前端 未结 10 2472
攒了一身酷
攒了一身酷 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:20

    I wrote a plugin that can fire callbacks when images have loaded in elements, or fire once per image loaded.

    It is similar to $(window).load(function() { .. }), except it lets you define any selector to check. If you only want to know when all images in #content (for example) have loaded, this is the plugin for you.

    It also supports loading of images referenced in the CSS, such as background-image, list-style-image, etc.

    waitForImages jQuery plugin

    • GitHub repository.
    • Readme.
    • Production source.
    • Development source.

    Example Usage

    $('selector').waitForImages(function() {
        alert('All images are loaded.');
    });
    

    Example on jsFiddle.

    More documentation is available on the GitHub page.

提交回复
热议问题