Created
August 6, 2012 10:07
-
-
Save stephband/3273140 to your computer and use it in GitHub Desktop.
Automatic slides cycling for Bolt
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
(function(jQuery, undefined){ | |
// Cycle news slides | |
jQuery(document).ready(function(){ | |
var slides = jQuery('.header_slide'), | |
length = slides.length, | |
i = slides.index(slides.filter('.active')[0]), | |
durationShort = 8000, | |
durationLong = 48000, | |
timer, fn; | |
function activateItem(i) { | |
slides.eq(i).trigger('activate'); | |
} | |
function activate() { | |
fn((i+1)%length); | |
} | |
function clearTimer() { | |
clearTimeout(timer); | |
timer = null; | |
} | |
slides | |
.bind('activate', function(e) { | |
// Keep index up to date. | |
i = slides.index(this); | |
clearTimer(); | |
// Use e.relatedTarget to determine if this activate is the result | |
// of a user action or not. If it is, the slide pauses longer. | |
timer = setTimeout(activate, e.relatedTarget ? durationLong : durationShort); | |
}) | |
.hover(function(e) { | |
fn = clearTimer; | |
}, function() { | |
fn = activateItem; | |
// If the timer has stopped, kick it off again | |
if (!timer) { activate(); } | |
}); | |
// Set the ball rolling | |
fn = activateItem; | |
timer = setTimeout(activate, durationShort); | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment