Skip to content

Instantly share code, notes, and snippets.

@felquis
Created May 4, 2015 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felquis/264c866cc364f76d6093 to your computer and use it in GitHub Desktop.
Save felquis/264c866cc364f76d6093 to your computer and use it in GitHub Desktop.
var accordion = (function ($) {
'use strict'
var accordionMethods = {},
tabsList,
tabWrapper,
tabSelector,
tabWrapper,
tabActiveClass;
accordionMethods.init = function (config) {
// ES6 https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/default_parameters
tabSelector = config.tabSelector || '.accordion-tab';
tabWrapper = config.tabWrapper || '.accordion-wrapper';
tabActiveClass = config.tabActiveClass || 'accordion-expanded';
tabsList = $(tabSelector);
tabWrapper = $(tabWrapper);
accordionMethods.destroy();
tabsList
.first()
.addClass(tabActiveClass);
initEvents();
}
var initEvents = function () {
tabWrapper.on('click.accordion', tabSelector, function(){
tabsList.not(this).removeClass(tabActiveClass);
$(this).addClass(tabActiveClass);
});
}
accordionMethods.destroy = function () {
tabsList.removeClass(tabActiveClass)
tabWrapper.off('click.accordion');
}
return accordionMethods;
}(jQuery));
// Usage
accordion.init({
tabSelector: '.tab-2'
});
accordion.init();
accordion.destroy();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment