3

enter image description hereI'm new to javascript and was just wondering the best way to go about changing the background image (only 2 images) of a DIV say like every 10 seconds. Thanks :)

3
  • 2
    have a look into the function setTimeout() in javascript (w3schools.com/jsref/met_win_settimeout.asp) Commented Jul 26, 2011 at 16:12
  • Even better: try setInterval() Commented Jul 26, 2011 at 16:13
  • is the green and red image the same all the way through or specific to each div? Commented Jul 26, 2011 at 16:52

6 Answers 6

2

This should do it for you : http://jsfiddle.net/U9GCM/

$(document).ready(function(){
var setFirst = $('.set').first();    
setFirst.addClass('active');
setTimeout(function(){change_background();},1000);
});

function change_background(){
    var setFirst = $('.set').first(),
        setActive = $('.active'),
        setNext = setActive.next(),
        setCheckActive = setActive.attr('id'),
        setCheckLast = $('.set').last().attr('id');

    if(setCheckActive == setCheckLast){
        setNext = setFirst;
        }

    $('.active').removeClass('active');
    setNext.addClass('active');
    setTimeout(function(){change_background()},1000);
    }

You can add as many divs as you want, just change 1000 to 10000 for ten seconds.

Hope it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks. how would i change a div inside the div background image too at the same time?
1

Try something like this:

$(document).ready(function(){
   setTimeout('setbackground()',10000);
});

function setbackground(){ 
  // change you image here
  setTimeout('setbackground()',10000); // to change the image in 10 seconds
}

2 Comments

i need the image to swap back too
The function call it self back. You would need to test the current display, then change it for the following one.
0

jsfiddle demo

alternate CSS classes (ie bg1 <--> bg2) with a setInterval function

var body = document.getElementsByTagName('body')[0],
    switchBG = function() {
        var bgTimer = setInterval(function() {
            body.className = (body.className === "bg2" ? "bg1" : "bg2");
        }, 10000 /* 10000 ms = 10 sec */);
    };
switchBG();

this is straight js

Comments

0
$(document).ready(function(){
   var i = 0;
   var images = newArray("image1.jpg","image2.png","image3.jpg");
   setTimeout('changeBack(i, images)',10000);
});

function changeBack(i, images) {
i++;
if(i > images.length) {
    i = 0;
}
$('#changeBackInThisId').css('background-image', images[i]);
setTimeout(changeBack(i, images), 10000);
}

I hope it works :-)

Or you can have any number of divs in your code with style="display: none" (besides the first of them) and each 10 seconds sets display: block; to one of them.

Comments

0

Here's a plugin type answer: http://jsfiddle.net/EpGsc/

Comments

-1
var time = 10000; // Ten seconds 
var timer = setInterval(function() {

    $('#myDiv, #myOtherDiv').css('background-image', '/new/img/path.jpg');

}, time);

Not tested but should work. I'll work on an example.

1 Comment

basically i have 3 divs in a row. i need the background to change on each div in order every 10 seconds in a loop.

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.