Created
June 15, 2012 11:01
-
-
Save chambaz/2935873 to your computer and use it in GitHub Desktop.
Simple Fading Content Rotator
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 // slides are direct descendants of $featured | |
$featured = $('.holder'), | |
featuredExists = !!$featured, | |
featuredTimer = null, | |
// animation options | |
featuredOpts = { | |
delay: 1000, // initial delay before first slide fades in | |
interval: 4000, // time between slide animation | |
fade: 500 // fade duration fadeing out and in | |
}; | |
// if the holder is present | |
if(featuredExists) { | |
// intiate slider by fading in first and starting setInterval | |
$featured.children(':eq(0)').delay(1000).fadeIn(500, function() { | |
featuredTimer = setInterval(featuredSlider, featuredOpts.interval); | |
}); | |
// where the magic happens | |
var featuredSlider = function() { | |
var $current = $featured.children(':visible'), | |
$next; | |
// last one so $next is first one otherwise $next is index + 1 | |
if($current.index() === $featured.children().length - 1) { | |
$next = $featured.children(':first-child'); | |
} else { | |
$next = $featured.children(':eq('+($current.index() + 1)+')'); | |
} | |
// do the animation | |
$current.fadeOut(featuredOpts.fade); | |
$next.fadeIn(featuredOpts.fade); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment