Skip to content

Instantly share code, notes, and snippets.

@coverslide
Created July 18, 2011 22:51
Show Gist options
  • Save coverslide/1090897 to your computer and use it in GitHub Desktop.
Save coverslide/1090897 to your computer and use it in GitHub Desktop.
An implementation of a jQuery carousel.
jQuery.fn.newScroller = function(_options) {
if (typeof _options == "string") {
var args = Array.prototype.slice.call(arguments, 1)
return this.each(function() {
var $this = $(this)
data = $this.data("newScroller")
if (_options in data.functions)
data.functions[_options](args)
})
}
return this.each(function() {
var $this = $(this)
, options = $.extend({ itemSelector: "ul li", prevContent: "prev", nextContent: "next", items: 1, interval: 5000, speed: 500, width: 0, height: 0, wrap: false, autoPlay: false }, ((_options && (typeof _options == 'object')) ? _options : {}))
, $elements = $this.find(options.itemSelector)
, $controls = $("<div class='controls'></div>")
, $nextEl = $("<a class='next' href='javascript:void()'></a>").append(options.nextContent)
, $prevEl = $("<a class='prev' href='javascript:void()'></a>").append(options.prevContent)
, data = {}
$this.data("newScroller", data)
data.properties = {
current: 0
, interval: 0
, paused: !!options.paused
}
function option(key, value) {
options[key] = value
}
function play() {
data.properties.paused = false;
if (data.properties.interval) {
clearInterval(data.properties.interval)
data.properties.interval = 0
}
data.properties.interval = setInterval(next, options.interval)
}
function toggle() {
if (data.properties.paused)
play()
else
pause()
}
function pause() {
data.properties.paused = true;
if (data.properties.interval) {
clearInterval(data.properties.interval)
data.properties.interval = 0
}
}
function next() {
data.properties.current++
if (data.properties.current > $elements.length - options.items) {
if (options.wrap) {
data.properties.current = 0
}
else {
data.properties.current = $elements.length - options.items
}
}
else {
update()
}
if (!data.properties.paused)
play()
return false;
}
function prev() {
data.properties.current--
if (data.properties.current < 0) {
if (options.wrap) {
data.properties.current = $elements.length - options.items
}
else {
data.properties.current = 0
}
}
else {
update()
}
if (!data.properties.paused)
play()
return false;
}
function update() {
$elements = $this.find(options.itemSelector)
animate($this.find(options.itemSelector + ".active"), $elements.eq(data.properties.current).addClass("active"));
if (!options.wrap && data.properties.current <= 0)
$prevEl.addClass("disabled").attr("disabled", "disabled")
else
$prevEl.removeClass("disabled").removeAttr("disabled")
if (!options.wrap && data.properties.current >= $elements.length - options.items)
$nextEl.addClass("disabled").attr("disabled", "disabled")
else
$nextEl.removeClass("disabled").removeAttr("disabled")
}
function animate(oldEl, newEl) {
switch (options.fx) {
case "fade":
oldEl.stop(true, true)
newEl.stop(true, true)
oldEl.fadeOut(options.speed)
newEl.fadeIn(options.speed)
break;
case "shift":
$elements.parent().eq(0).stop(true, true)
$elements.parent().eq(0).animate({ left: -newEl.position().left }, options.speed)
break;
default:
oldEl.hide()
newEl.show()
}
}
data.functions = {
animate: animate
, next: next
, prev: prev
, pause: pause
, play: play
, update: update
, toggle: toggle
, option: option
}
$prevEl.click(prev)
$nextEl.click(next)
$this.append($controls.append($prevEl, $nextEl))
update()
if (options.onLoad) {
options.onLoad.call(this)
}
if (options.autoPlay)
play()
return this
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment