Skip to content

Instantly share code, notes, and snippets.

@zackdever
Created October 18, 2012 20:52
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 zackdever/3914648 to your computer and use it in GitHub Desktop.
Save zackdever/3914648 to your computer and use it in GitHub Desktop.
simple JavaScript image fader
var bgImgs = [
'/images/foo.png'
, '/images/bar.png'
, '/images/foobar.png'
];
$(function () {
if (bgImgs.length > 1) {
var bgImage = $('#bgImage')
, index = 0
, interval = 5000
, slideshow = $('#slider');
setInterval(function () {
slideshow.css('background-image', 'url(' + bgImage.attr('src') + ')');
bgImage.fadeOut(function () {
index = index < bgImgs.length - 1 ? index + 1 : 0;
bgImage.attr('src', bgImgs[index]);
bgImage.fadeIn('slow');
});
}, interval);
}
});
<div id='slider'>
<img id='bgImage' src='/images/foo.png' />
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment