Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (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);
Add Comment
Please, Sign In to add comment