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

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

    For those who want to be notified of download completion of a single image that gets requested after $(window).load fires, you can use the image element's load event.

    e.g.:

    // create a dialog box with an embedded image
    var $dialog = $("
    "); // get the image element (as a jQuery object) var $imgElement = $dialog.find("img"); // wait for the image to load $imgElement.load(function() { alert("The image has loaded; width: " + $imgElement.width() + "px"); });

提交回复
热议问题