I have a site with a rotating header image (you've all seen them). I want to do the following:
- Load the entire page plus the first header image
- Start the header image slideshow transitioning every x seconds or when the next image has finished loading, whichever is later
I haven't really need an example that truly does this.
The $(document).ready mechanism is meant to fire after the DOM has been loaded successfully but makes no guarantees as to the state of the images referenced by the page.
When in doubt, fall back on the good ol' window.onload event:
Now, this is obviously slower than the jQuery approach. However, you can compromise somewhere in between:
To explain a bit:
we grab the DOM element of the image whose image we want completely loaded
we then set an interval to fire every 50 milliseconds.
if, during one of these intervals, the complete attribute of this image is set to true, the interval is cleared and the rotate operation is safe to start.
you can try
i had the same problem and this code worked for me. how it works for you too!
did you try this ?
Well the first can be achieved with the document.ready function in jquery
The changing image can be achieved with any number of plugins
If you wish you can check if images are loaded with the complete property. I know that at least the malsup jquery cycle slideshow makes use of this function internally.
If you pass jQuery a function, it will not run until the page has loaded:
You probably already know about $(document).ready(...). What you need is a preloading mechanism; something that fetches data (text or images or whatever) before showing it off. This can make a site feel much more professional.
Take a look at jQuery.Preload (there are others). jQuery.Preload has several ways of triggering preloading, and also provides callback functionality (when the image is preloaded, then show it). I have used it heavily, and it works great.
Here's how easy it is to get started with jQuery.Preload: