Skip to content

Instantly share code, notes, and snippets.

@rpupkin77
Created July 8, 2011 17:17
Show Gist options
  • Save rpupkin77/1072301 to your computer and use it in GitHub Desktop.
Save rpupkin77/1072301 to your computer and use it in GitHub Desktop.
Simple jQuery Slideshow (tested on 1.5)
/** ROCSLIDE - SIMPLE JQUERY SLIDESHOW FADER
*
*
* settings are as follows:
* start - item to start at
* seconds - duration of each state
* pager_element - selector that you want the auto-generated pages and buttons to be applied to (via .html())
* mouse_pause - true means item will be paused on rollover of content
* current_class - css class that the current item in pager element will use (only if pager element is set)
* prev_text - text to be used in auto-generated prev link (on if pager element is set)
* next_text - text to be used in auto-generated prev link (on if pager element is set)
*
* $(function(){ $(selector).rocslide({"setting":"value"}) });
**/
(function( $ ){
var methods = {
init: function(options){
return this.each(function() {
$obj = this;
//set user defined option, if they exist
if ( options )
{
$.extend( settings, options );
}
//set attrs based on options
attrs.nodes = $($obj).children(".slidenode")
attrs.count = attrs.nodes.length;
attrs.current = settings.start;
attrs.timeout = settings.seconds*1000
//build pager element if need be
if(settings.pager_element){
methods.build_pager(settings.pager_element);
}
//hide each item except the one on which we are supposed to start.
attrs.nodes.each(function(index)
{
if(index !== settings.start)
{
$(this).hide();
}
//setup mouseover pausing if it is set to true (default)
if(settings.mouse_pause)
{
$(this).mouseover(function()
{
methods.pause(true);
});
$(this).mouseout(function()
{
methods.pause(false);
});
}
});
//if paging is turned on, set the first item to be selected
if(settings.pager_element)
{
$("#rocslide-jump_"+attrs.current).addClass(settings.current_class);
}
//start the show
methods.slide();
});
},
slide:function(){
attrs.timer = setTimeout(function(){
methods.next(true);
}, attrs.timeout);
},
build_pager:function(elem){
$('.rocslide-jump').live('click', function(){
methods.transition(($(this).text()-1));
});
$('#rocslide-prev').live('click', function(){
methods.prev();
});
$('#rocslide-next').live('click', function(){
methods.next();
});
var html = "<ul id='rocslide-pager'><li><a href='#' id='rocslide-prev'>"+settings.prev_text+"</a></li>";
attrs.nodes.each(function(index){
html += "<li><a href='#' id='rocslide-jump_"+index+"' class='rocslide-jump'>"+(index+1)+"</a></li>";
});
html += "<li><a href='#' id='rocslide-next'>"+settings.next_text+"</a></li></ul>";
$(elem).append(html);
},
pause:function(action){
settings.paused = action;
if(settings.paused)
{
clearTimeout(attrs.timer)
}
else
{
methods.slide();
}
},
ouroboros:function(to){ //ensures that the snake eats its own tail.
//on last, go to first
if(to >= attrs.count)
{
to = 0;
}
//make sure that the previous item relative to the first item is the last item.
if(to < 0)
{
to = (attrs.count-1);
}
return to;
},
transition:function(to, auto){
clearTimeout(attrs.timer);
var trans = "fast";
if(auto){
trans = "slow";
}
to = this.ouroboros(to);
$(attrs.nodes[attrs.current]).fadeOut(trans, function(){
$(attrs.nodes[to]).fadeIn('fast', function(){
attrs.current = to;
});
})
//make the pager item selected
if(settings.pager_element){
$(".rocslide-jump").removeClass(settings.current_class);
$("#rocslide-jump_"+to).addClass(settings.current_class);
}
if(!settings.paused)
{
this.slide();
}
},
next:function(auto){
methods.transition((attrs.current+1), auto)
},
prev:function(auto){
methods.transition((attrs.current-1), auto)
}
}
//attrs - need to be accessible only to the app, user can't override
var attrs = {
'current':0,
'count':0,
'timeout':0,
'nodes':null,
'timer':null
}
//settings - users can override
var settings = {
'start' : 0,
'seconds' : 8,
'paused' : false,
'mouse_pause':true,
'pager_element':false,
'current_class':"rocslide-current",
'prev_text':"&lt;",
'next_text':"&gt"
};
$.fn.rocslide = function( method ) {
if ( methods[method] )
{
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
}
else if ( typeof method === 'object' || ! method )
{
return methods.init.apply( this, arguments );
}
else
{
$.error( ' The method ' + method + ' is not a rocslide method' );
}
};
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment