Created
November 23, 2010 16:58
-
-
Save alexwelch/712107 to your computer and use it in GitHub Desktop.
a very simple jquery slideshow plugin
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($){ | |
$.slideshow = function(el, options){ | |
var base = this; | |
base.$el = $(el); | |
base.el = el; | |
base.slide = -1; | |
base.timer = false; | |
// Add a reverse reference to the DOM object | |
base.$el.data("slideshow", base); | |
base.init = function(){ | |
base.options = $.extend({},$.slideshow.defaultOptions, options); | |
base.generateJQuerySelectors(); | |
this.hideAll(); | |
this.play(); | |
base.bindButtons(); | |
}; | |
base.generateJQuerySelectors = function() { | |
// this goes through all the selectors and assigns them to their $ objects, so: | |
// options.selectors.options_panel has a base.$options_panel equivelent. | |
for (var key in base.options.selectors) { | |
base['$' + key] = base.$el.find(base.options.selectors[key]); | |
} | |
}; | |
base.$slide = function() { | |
return $(base.$slides[base.slide]); | |
}; | |
base.$control = function() { | |
return $(base.$controls[base.slide]); | |
}; | |
base.hideAll = function() { | |
// incase you forget to do this in your markup | |
base.$slides.addClass('hidden'); | |
}; | |
base.show = function() { | |
base.$slide().removeClass('hidden'); | |
base.$control().addClass('current'); | |
}; | |
base.hide = function() { | |
base.$slide().addClass('hidden'); | |
base.$control().removeClass('current'); | |
}; | |
base.stop = function() { | |
clearTimeout(base.timer); | |
}; | |
base.play = function() { | |
base.show(); | |
base.playNext(); | |
}; | |
base.playNext = function() { | |
base.next(); | |
base.timer = setTimeout(base.playNext, base.options.interval); | |
}; | |
base.goto = function(index) { | |
base.stop(); | |
base.hide(); | |
base.slide = parseInt(index); | |
base.show(); | |
}; | |
base.next = function() { | |
base.stop(); | |
base.hide(); | |
if (base.slide < base.$slides.length - 1) { | |
base.slide += 1; | |
} else { | |
base.slide = 0; | |
} | |
base.show(); | |
}; | |
base.previous = function() { | |
base.stop(); | |
base.hide(); | |
if (base.slide < 1) { | |
base.slide = base.$slides.length - 1; | |
} else { | |
base.slide -= 1; | |
} | |
base.show(); | |
}; | |
base.bindButtons = function() { | |
base.$next_btn.click(function(e) { | |
base.next(); | |
e.preventDefault(); | |
}); | |
base.$prev_btn.click(function(e) { | |
base.previous(); | |
e.preventDefault(); | |
}); | |
base.$controls.click(function(e) { | |
base.goto($(this).attr('href').split('_')[1]); | |
}); | |
}; | |
// Run initializer | |
base.init(); | |
}; | |
$.slideshow.defaultOptions = { | |
interval: 3000, | |
selectors: { | |
next_btn: 'a.next_btn', | |
prev_btn: 'a.prev_btn', | |
slides: '.slides li', | |
controls: '.ss_controls a' | |
} | |
}; | |
$.fn.slideshow = function(options) { | |
return this.each(function(){ | |
(new $.slideshow(this, options)); | |
}); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment