Created
October 18, 2012 20:52
-
-
Save zackdever/3914648 to your computer and use it in GitHub Desktop.
simple JavaScript image fader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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