Skip to content

Instantly share code, notes, and snippets.

@kgn
Created October 19, 2010 07:24
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 kgn/633771 to your computer and use it in GitHub Desktop.
Save kgn/633771 to your computer and use it in GitHub Desktop.
This is the accordion plugin developed for inscopeapps.com
(function($){
$.fn.accordion = function(options){
options = $.extend({
'header': 'h1', //The header that will be clicked
'content': 'div', //The content, this must be a sibling of the header
'showFirst': false, //If true the first item will be expanded on page load
'easing': null, //The easing style of the animation
//Called when the content will hide
'willHide': null,
//Called when the content was hidden
'didHide': null,
//The content element is passed to these function
//Called when the content will be shown
'willShow': null,
//Called when the content was shown
'didShow': null
}, options);
return this.each(function(){
var root = this;
//set the width to help slideDown figure out the right height
var allContents = $(root).find(options.content).width($(root).width());
//find headers
$(root).find(options.header).each(function(i){
var header = this;
//find the content
var content = $(header).siblings(options.content)[0];
if(content){
if(i > 0 || i === 0 && options.showFirst === false){
$(content).hide();
}
//run the show function if the first item is visable on load
if(i === 0 && options.showFirst === true && (options.willShow || options.didShow)){
if(options.willShow){
options.willShow(content);
}else if(options.didShow){
options.didShow(content);
}
}
$(header).click(function(){
//slide up all content elements
allContents.slideUp(options.easing);
if(options.willHide){
options.willHide();
}
//if the content element is hidden show it
if($(content).is(':hidden') === true){
if(options.willShow){
options.willShow(content);
}
$(content).slideDown(options.easing, function(){
options.didShow(content);
});
}
});
}
});
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment