Skip to content

Instantly share code, notes, and snippets.

@replete
Created April 14, 2012 14:56
Show Gist options
  • Save replete/2384996 to your computer and use it in GitHub Desktop.
Save replete/2384996 to your computer and use it in GitHub Desktop.
Simple rotating content children
/* -------------------------------------- */
//Simple Rotator
var $rotatorContainers = $("[data-rotator]");
$rotatorContainers.each(function () {
var $container = $(this),
$items = $container.children($container.attr("data-rotator")),
itemTotal = $items.length - 1,
autoplay = $container.attr("data-rotator-autoplay");
if (autoplay != undefined) var cycleInterval = setInterval(cycleItems, 5000);
if (itemTotal === 0) return;
$container
.children(".nav")
.removeClass("js-hide")
.click(function (event, triggered) {
if (!triggered && autoplay != undefined) clearTimeout(cycleInterval);
var $currentItem = $items.filter(".current"),
currentIndex = $items.index($currentItem),
$this = $(this);
if ($this.hasClass("prev")) {
if (currentIndex === 0) changeItem(itemTotal);
if (currentIndex > 0) changeItem(currentIndex - 1);
} else if ($this.hasClass("next")) {
if (currentIndex === itemTotal) changeItem(0);
if (currentIndex < itemTotal) changeItem(currentIndex + 1);
}
});
function changeItem(index) {
$items
.removeClass("current")
.eq(index)
.addClass("current");
}
function cycleItems() {
$container
.children(".next")
.trigger("click", true);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment