views:

419

answers:

2

I'm currently using a small jquery script to rotate through background images on a portfolio site I''m creating for a friend's portfolio site here's the code:

  counter = 1; 
    num_images = 9;
    dir = "IMAGE DIRECTORY URL";

    function rotateHeader() {


 var background_img = 'url(' + dir + '/image' + counter + '.gif)';

 jQuery('.category').css('background-image', background_img);  
 counter++; if (counter > num_images) counter = 1;

    }
    setInterval( "rotateHeader()", 5000 );

You can see it working here:

www.iamanatom.com/site/the-good

What I want to do is have the images and or colors fade in and out. I think I'll have to preload the images to do this and then add some sort of fade property when the pictures are loading. How do I do this?

A: 

Something like this ought to do it

var counter = 1, 
    num_images = 9,
    dir = "IMAGE DIRECTORY URL";

function rotateHeader() {

    var background_img = 'url(' + dir + '/image' + counter + '.gif)';

    jQuery('.category').fadeOut(function() {
        jQuery(this).css('background-image', background_img).fadeIn();
    });  
    counter++; if (counter > num_images) counter = 1;
}
setInterval(rotateHeader, 5000 );

First we fade out the current background image, then in the callback function, change the background image and then fade it back in. You might want to play with the fadeOut() and fadeIn() intervals to get the effect you want. I've also changed the setInterval() to use the function name as opposed to a string to evaluate.

Russ Cam
Okay amazing thanks you so much, thats created the fade effect BUT now I have another problem - if you look at my implementation:www.iamanatom.com/site/the-goodyou will see that the entire page fades in and out rather than just the backgroun images. Not sure what to do about that!
JamieD
Ahhh I think I know why that is - the element I'm trying to manipulate is the body (I'm using category as a class because I only want the effect on the blog pages). Unfortunately this element wraps around everything so it fades the whole page in.I think my solution will be to create an absolutely positioned div that dynamically picks up the classes I need and is outside the main page elements... we'll see if this works
JamieD
Yippee problem solved guys my solution works!!!Thanks to Russ Cam!!!
JamieD
A: 

Can you please provide a step by step tutorial for this? I am using WordPress and I would like fading / rotating background images.

VincentAlexander