I wrote my own js image preloader before, what I'm going to post is pseudo-like to him, but it will need some work. In principle, to make your page load beautifully, the answer is not to hide the body in order to stop it, but rather to decorate what happens when the images load. Besides throwing your baby with bath water, sites usually load faster if they see something happening while they wait.
So what you need to do (for each image, or at least for each large or large image), the loading gif is displayed (check http://ajaxload.info ) while the image is loading, and then when it's finished, you can use jQuery to animate a container with the correct height and image attenuation. This stops your page jumping around (or rather makes it more beautiful while it jumps).
To achieve this effect, something like this should do the trick:
function imageLoader(elem) { //set all images in this elem to opacity 0 elem.children('img').css('opacity', '0'); var image = new Image(); //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 as follows:
imageLoader($('someElementWithAnImageTagInside'));
Again this is just psuedocode, but hopefully you should give you some ideas.
Hope this helps
source share