9

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
  • 3
    body#home h1#siteH1 is a highly inefficient selector. Why not just use #siteH1 ? Commented Sep 3, 2009 at 22:36
  • For my information, is this also the case with jQuery selectors? Commented Sep 4, 2009 at 18:24

3 Answers 3

19

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);        
});
Sign up to request clarification or add additional context in comments.

5 Comments

your code execute inmediately. Does not wait anytime before changing the background and does not come to the initial background again.
Basically, I would like to loop beetween background images
Thanks, it works. Is there a way to fadeOut the background and fadeIn the next?
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"
Instead of using 2 times setTimeout, it would be easier to use setInterval.
3

checkout the queue functionality:

jQuery Queue

Comments

2

Late to the party, but here's what I just came up with for similar requirement.

<script type="text/javascript">

    // populate image set
    var imageArray = [
        "1.jpg",
        "2.jpg",
        "3.jpg",
        "4.jpg"
    ];

    // in milliseconds
    var fadeSpeed   = 1000;
    var timeout     = 3000;


    function fadeInFront (i) {
    
        $('#faderFront').css( {
            "background-image" : "url(" + imageArray[i] + ")"
        });
    
        $('#faderFront').fadeIn(fadeSpeed);
    
        i++;
    
        if ( i == imageArray.length ) {
            i=0;
        }

        setTimeout(function() {
            fadeOutFront(i);
        },timeout);
    
    }

    function fadeOutFront (i){
    
        $('#faderBack').css( {
            "background-image" : "url(" + imageArray[i] + ")"
        });
    
        $('#faderFront').fadeOut(fadeSpeed);
    
        i++;
    
        if ( i == imageArray.length ) {
            i=0;
        }
    
        setTimeout(function() {
            fadeInFront(i);
        },timeout);
    
    }
    
    function preload(arrayOfImages) {
    
        $(arrayOfImages).each(function() {
    
            $('<img/>')[0].src = this;
    
        });
    
    }

    $(document).ready(function(){

        preload(imageArray);

        setTimeout(function() {
            fadeOutFront(0);
        }, timeout);

    });

</script>

<style type="text/css">

    .imageContainer {
        width: 700px;
        height: 400px;
        position: relative;
    }
    
    #faderBack,
    #faderFront,
    #inFrontOfBoth {
        position: absolute;
        top: 0; left: 0;
        background-size: cover;
        background-position: center;
        background-repeat: no-repeat;
        width: 100%;
        height: 100%;
    }

</style>
   
<div id="container">

    <div class="imageContainer">
        
        <div id="faderBack"></div>
        <div id="faderFront" style="background-image: url('1.jpg');"></div>
        <div id="inFrontOfBoth">Hello World</div>

    </div>

</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.