Skip to content

Instantly share code, notes, and snippets.

@adibhanna
Created November 29, 2012 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adibhanna/4168248 to your computer and use it in GitHub Desktop.
Save adibhanna/4168248 to your computer and use it in GitHub Desktop.
jquery: multiple background
jQuery(window).load(function(){
var images = ['blaa.jpg','sdsd.jpg'];
var i = 0;
function changeBackground() {
jQuery('#absolute-c').css('background-image', function() {
if (i >= images.length) {
i=0;
}
return 'url(' + images[i++] + ')';
});
}
// Call it on the first time
changeBackground();
// Set an interval to continue
setInterval(changeBackground, 3000);
});
//.......................................................
jQuery(window).load(function(){
var images = ['picture1.jpg','picture2.jpg'];
var i = 0;
var timeoutVar;
function changeBackground() {
clearTimeout(timeoutVar); // just to be sure it will run only once at a time
jQuery('#absolute-c').css('background-image', function() {
if (i >= images.length) {
i=0;
}
return 'url(' + images[i++] + ')';
});
// call the setTimeout every time to repeat the function
timeoutVar = setTimeout(changeBackground, 3000);
}
// Call it on the first time and it will repeat
changeBackground();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment