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

前端 未结 10 2374
攒了一身酷
攒了一身酷 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 = $("<div><img src='" + img_url + "' /></div>");
    
    // 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");
    });
    
    0 讨论(0)
  • 2020-11-22 00:27

    With jQuery i come with this...

    $(function() {
        var $img = $('img'),
            totalImg = $img.length;
    
        var waitImgDone = function() {
            totalImg--;
            if (!totalImg) alert("Images loaded!");
        };
    
        $('img').each(function() {
            $(this)
                .load(waitImgDone)
                .error(waitImgDone);
        });
    });
    

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

    0 讨论(0)
  • 2020-11-22 00:34

    $(window).load() will work only the first time the page is loaded. If you are doing dynamic stuff (example: click button, wait for some new images to load), this won't work. To achieve that, you can use my plugin:

    Demo

    Download

    /**
     *  Plugin which is applied on a list of img objects and calls
     *  the specified callback function, only when all of them are loaded (or errored).
     *  @author:  H. Yankov (hristo.yankov at gmail dot com)
     *  @version: 1.0.0 (Feb/22/2010)
     *  http://yankov.us
     */
    
    (function($) {
    $.fn.batchImageLoad = function(options) {
        var images = $(this);
        var originalTotalImagesCount = images.size();
        var totalImagesCount = originalTotalImagesCount;
        var elementsLoaded = 0;
    
        // Init
        $.fn.batchImageLoad.defaults = {
            loadingCompleteCallback: null, 
            imageLoadedCallback: null
        }
        var opts = $.extend({}, $.fn.batchImageLoad.defaults, options);
    
        // Start
        images.each(function() {
            // The image has already been loaded (cached)
            if ($(this)[0].complete) {
                totalImagesCount--;
                if (opts.imageLoadedCallback) opts.imageLoadedCallback(elementsLoaded, originalTotalImagesCount);
            // The image is loading, so attach the listener
            } else {
                $(this).load(function() {
                    elementsLoaded++;
    
                    if (opts.imageLoadedCallback) opts.imageLoadedCallback(elementsLoaded, originalTotalImagesCount);
    
                    // An image has been loaded
                    if (elementsLoaded >= totalImagesCount)
                        if (opts.loadingCompleteCallback) opts.loadingCompleteCallback();
                });
                $(this).error(function() {
                    elementsLoaded++;
    
                    if (opts.imageLoadedCallback) opts.imageLoadedCallback(elementsLoaded, originalTotalImagesCount);
    
                    // The image has errored
                    if (elementsLoaded >= totalImagesCount)
                        if (opts.loadingCompleteCallback) opts.loadingCompleteCallback();
                });
            }
        });
    
        // There are no unloaded images
        if (totalImagesCount <= 0)
            if (opts.loadingCompleteCallback) opts.loadingCompleteCallback();
    };
    })(jQuery);
    
    0 讨论(0)
  • None of the answers so far have given what seems to be the simplest solution.

    $('#image_id').load(
      function () {
        //code here
    });
    
    0 讨论(0)
提交回复
热议问题