tags:

views:

464

answers:

2

I would like to change a header css background image every few seconds, so its look like a slideshow.

For example first 2 seconds be:

body#home h1#siteH1 { background:url(../images/header1.jpg) no-repeat;}

Next 2 seconds be:

body#home h1#siteH1 { background:url(../images/header2.jpg) no-repeat;}

Next 2 seconds be:

body#home h1#siteH1 { background:url(../images/header3.jpg) no-repeat;}

And then loop again to header1.

If anyone knows how to do the transition with a fading effect, then it would be simply perfect.

+2  A: 

checkout the queue functionality:

jQuery Queue

Slee
+5  A: 

Now with fade

Try this:

var currentBackground = 0;
var backgrounds = [];
backgrounds[0] = '../images/header1.jpg';
backgrounds[1] = '../images/header2.jpg';
backgrounds[2] = '../images/header3.jpg';

function changeBackground() {
    currentBackground++;
    if(currentBackground > 2) currentBackground = 0;

    $('body#home h1#siteH1').fadeOut(100,function() {
        $('body#home h1#siteH1').css({
            'background-image' : "url('" + backgrounds[currentBackground] + "')"
        });
        $('body#home h1#siteH1').fadeIn(100);
    });


    setTimeout(changeBackground, 2000);
}

$(document).ready(function() {
    setTimeout(changeBackground, 2000);        
});
Ropstah
your code execute inmediately. Does not wait anytime before changing the background and does not come to the initial background again.
Sergio del Amo
Basically, I would like to loop beetween background images
Sergio del Amo
See the updated answer...
Ropstah
Thanks, it works. Is there a way to fadeOut the background and fadeIn the next?
Sergio del Amo
I did not wont to remove you up vote. But If I vote you up twice it remove the old vote and says "Vote too old to be change, unless this answer is edited"
Sergio del Amo
ok i added fading..
Ropstah