Skip to content

Instantly share code, notes, and snippets.

@tylersmalley
Last active December 12, 2015 22:29
Show Gist options
  • Select an option

  • Save tylersmalley/4833177 to your computer and use it in GitHub Desktop.

Select an option

Save tylersmalley/4833177 to your computer and use it in GitHub Desktop.
/*!
* Wizard: Easy paging through DOM objects
*
* @constructor
* @param {String} jQuery selector for page
*/
var Wizard = (function(selector){
/**
* Public Methods
*/
this.goto = goto;
this.previous = previous;
this.next = next;
this.current = function(){ return currentPage; };
this.count = function(){ return pages.length; };
/**
* Private Variables/Methods
*/
var pages = []
, currentPage = 0;
/**
* Initializer
*
* @see {Page()}
*/
function initialize(){
pages = $(selector);
next();
$('.js-next-page').on('click', next);
$('.js-previous-page').on('click', previous);
}
/**
* Navigates to the previous page if possible
*
* @returns {Boolean}
*/
function previous(){
return goto(currentPage - 1);
}
/**
* Navigates to the next page if possible
*
* @returns {Boolean}
*/
function next(){
return goto(currentPage + 1);
}
/**
* Navigates to a specified page
*
* @returns {Boolean}
*/
function goto(page){
var selector = pages[page - 1];
if (selector) {
pages.hide();
$(selector).show();
currentPage = page;
return true;
}
return false;
}
initialize.apply(this, arguments);
});
$(function(){
new Wizard('.page');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment