admin管理员组

文章数量:1278859

I have a portfolio page filled with images that I would like to hide with a mask overlay until all the images have had a chance to finish loading. Is there a way to detect loading finished for all the content on a page?

I would like to find a way to do this, preferably using the jQuery library. Any ideas?

I have a portfolio page filled with images that I would like to hide with a mask overlay until all the images have had a chance to finish loading. Is there a way to detect loading finished for all the content on a page?

I would like to find a way to do this, preferably using the jQuery library. Any ideas?

Share Improve this question edited Aug 11, 2022 at 7:53 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 24, 2011 at 11:36 ThomasThomas 5,09921 gold badges71 silver badges101 bronze badges 2
  • 1 Don't. Seriously. People are used to pages loading incrementally. Showing them a loading screen until the entire page is ready is just going to increase the number of people who get bored and go away before the loading has finished. – Quentin Commented Mar 24, 2011 at 11:39
  • 1 Not my call, thanks for the opinion. – Thomas Commented Mar 24, 2011 at 11:51
Add a ment  | 

7 Answers 7

Reset to default 6

Assuming that the images you want to have finished loading before the overlay is removed all have the class .theImagesToLoad:

// count the number of images
var imageCount = $(".theImagesToLoad").length;

// init a load counter
var loadCounter = 0;
$(".theImagesToLoad").load(function() {

    // an image has loaded, increment load counter
    loadCounter++;

    // remove overlay once all images have loaded
    if(loadCounter == imageCount) {
        removeOverlay();
    }
}).each(function() {

    // make sure the `.load` event triggers
    // even when the image(s) have been cached
    // to ensure that the overlay is removed
    if(this.plete) $(this).trigger("load");
});

See: http://api.jquery./load-event/

You can use the window.onload event, it gets fired when all images have been loaded:

window.onload = function() {
  $('#maskoverlay').remove();
}

See here: https://developer.mozilla/en/DOM/window.onload

The jQuery ready event fires when the DOM is ready not when all the images are loaded. You can use the window.onload as suggested before or if you want to use jQuery you can use the load event like this:

$(window).load(function () {
  // run code
});

For document ready use:

$(function(){

   // Your code

});

However, you'll have to load the mask in CSS with the page and then use the above function to remove it. You won't be able to create the mask in javascript before all the images have loaded unless you again use javascript to supress the images loading, create your mask, load the images, then remove the mask

or you could use:

$(document).ready(function(){
    //your code
}

Edit

Sorry might have misunderstood you, this plugin might help you:

https://gist.github./268257

i think you can use the onload event for the <body> tag and attach to a function

or you can user the jquery

$(document).ready(function(){
});

Something like ?

$(function() {
    var img = [];
    var imgarr = ["image1.png","image2.png", ....];
    var imgcount = 0;

    var loadTimer;
    loadTimer = setInterval(function() {
        if(imgcount == imgarr.length){
           clearInterval(loadTimer);

           // ALL IMAGES ARE DONE LOADING HERE
           //   use : $(img) to have a JQuery object of the image elements
        }
    },100);

    $(imageContainer).hide();
    for(var i=0;i<imgarr.length;i++){
        img[i] = new Image();
        img.style.display = 'none';
        $(img[i]).appendTo(imageContainer);
        img[i].src = imgarr[i];
        img[i].onload = function() { imgcount++; }
    }
});

本文标签: htmlDetect page loaded with JavaScriptStack Overflow