Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop a css background image with Jquery every few seconds?

Tags:

jquery

css

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.

like image 203
Sergio del Amo Avatar asked Sep 03 '09 17:09

Sergio del Amo


2 Answers

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>
like image 55
Yevgeniy Avatar answered Sep 17 '22 13:09

Yevgeniy


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);        
});
like image 22
Ropstah Avatar answered Sep 17 '22 13:09

Ropstah