Created
May 4, 2015 14:45
-
-
Save felquis/264c866cc364f76d6093 to your computer and use it in GitHub Desktop.
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
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