views:

5724

answers:

5

I have a site with a rotating header image (you've all seen them). I want to do the following:

  1. Load the entire page plus the first header image
  2. 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.

A: 

If you pass jQuery a function, it will not run until the page has loaded:

<script type="text/javascript">
$(function() {
    //your header rotation code goes here
});
</script>
Ryan Doherty
Does the "page has loaded" mean that all of the other images will be loaded as well?
DevDevDev
No, it only means that the dom has been created.
stimms
If you really need to wait for the page to finish loading, you could do:$(window).load(function() {//your code });
Ryan Doherty
This notation is just a shortcut for document.ready. It has nothing to do with the page load event.
James Westgate
+1  A: 

Well the first can be achieved with the document.ready function in jquery

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

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.

stimms
I don't think any of these will wait to transition until the next iamge has loaded though right?
DevDevDev
A good point, answer refined
stimms
A: 

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:

window.onload = function()
{
  //your code here
};

Now, this is obviously slower than the jQuery approach. However, you can compromise somewhere in between:

$(document).ready
(
  function()
  {
    var img = document.getElementById("myImage");

    var intervalId = setInterval(
                        function()
                        {
                          if(img.complete)
                          {
                            clearInterval(intervalId);
                            //now we can start rotating the header
                          }
                        },
                        50);
  }
);

To explain a bit:

  1. we grab the DOM element of the image whose image we want completely loaded

  2. we then set an interval to fire every 50 milliseconds.

  3. 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.

David Andres
window.onload = function() is a bad idea, it prevents you from setting onload from anything else or will erase any other "onload".
e-satis
@e-satis: To avoid overwriting the hooks that are already attached to the `window.onload`, attach your function with jQuery: `$(window).load(function () {});`
Andreas Grech
@e-satis: I mentioned window.onload specifically because the event handler code assigned to it is guaranteed to execute after images have loaded, which is different from the behavior of the jQuery ready function and somewhere at the other extreme of what the OP is looking for. You're right, there are better ways to attach events, but I didn't want to lose sight of the key points I was trying to convey.
David Andres
@Andreas: Good to know about the Load function.
David Andres
+2  A: 

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.

http://flesler.blogspot.com/2008/01/jquerypreload.html

Jacob
The only real anwser : there is no easy way to do it. Use a plugin.
e-satis
e-satis: I'm not sure why you believe the use of a plugin is the only "real" answer.
David Andres
+1  A: 

you can try

$(function()
{

$(window).bind('load', function()
{

// INSERT YOUR CODE THAT WILL BE EXECUTED AFTER THE PAGE COMPLETELY LOADED...

});
});

i had the same problem and this code worked for me. how it works for you too!

Website-Chef