Skip to content Skip to sidebar Skip to footer

How Can I Hide A Page Contents Until All The Images Are Fully Loaded?

I am trying to optimize a site which loads terribly. I already reordered, compressed and minified js and css, but the biggest problem are the images. This site has some really heav

Solution 1:

It's extremely easy with JQuery. I'd recommend using that framework anyway, even if it wasn't for this particular solution:

functionpreload(arrayOfImages) {
    $(arrayOfImages).each(function(){
        $('<img/>')[0].src = this;
    });
}

Use it like this:

preload([
    'img/apple.jpg',
    'img/banana.jpg',
    'img/pear.jpg'
]);

Hiding your site at this point is easy with CSS and JQuery:

CSS:

body {
    display:none;
}

JQuery:

$(function(){
    $('body').show();
    // or fade it in: $('body').fadeIn();
});

Solution 2:

Just after the Body tag, add a Div Element to cover the whole length and breadth of the page, make it white background or add a "Loading" image, set its z-index to Max (9999 or something) so its on top of everything, and give it some ID/Name

and Use a Javascript with Body onLoad Event to hide or remove that Div Element.

Something like

<bodyonload='document.body.removeChild(document.getElementById("bigDiv"));'><divstyle="width:100%;height:100%;background:white;"id=bigDiv>Loading...</div>

Solution 3:

First important thing to check - make sure the images are cacheable. Sometimes caching of images is disabled. The way to check this is a direct call to the webserver:

 telnet localhost 8080
 GET /images/flibble.gif HTTP/1.0

And see what is returned. It should return something like Expires: or Cache-Control.

This page seems to describe it well: http://www.web-caching.com/mnot_tutorial/how.html

The other trick is to specify the size of the images in the tag. This vastly improves performance because the browser doesn't need to wait until the page is loaded.

Solution 4:

I've written my own js image preloader before, what I'm about to post is pseudo-similar to it but will need some work. Basically in order to make your page load nicely, the answer isn't to hide the body to stop it jumping around, rather beautify what happens when the images do load. Besides throwing out the baby with the bath water, generally sites appear to the user to load faster if they can see something happening while they wait.

So what you need to do (for each image, or atleast each major or large image) is display a loading gif (check out http://ajaxload.info) while the image loads, then when it is finished you can use jQuery to animate it's container to the correct height and fade in the image. This stops your page jumping around (or rather makes it look prettier while it jumps).

To acheive this effect, something like this should do the trick:

functionimageLoader(elem)
{
  //set all images in this elem to opacity 0
  elem.children('img').css('opacity', '0');
  var image = newImage();
  //show loading image
  elem.append('html for loading image');

  //when the image loads, animate the height of the elem and fade in image
  image.onload=function()
  { 
       var h = image.height;
       elem.animate({height:h+'px'}, function(){
         elem.children('img').fadeIn();
         elem.children('html for loading image').remove(); 
       });
  };

  image.src=elem.children('img').attr('src');
}

Use it like this:

imageLoader($('someElementWithAnImageTagInside'));

Again this is just psuedocode really but hopefully should give you some ideas.

Hope this helps

Solution 5:

U can use block plugin to block the page. Link

Post a Comment for "How Can I Hide A Page Contents Until All The Images Are Fully Loaded?"