Skip to content

Instantly share code, notes, and snippets.

@felipelavinz
Created January 5, 2011 22:15
Show Gist options
  • Save felipelavinz/767128 to your computer and use it in GitHub Desktop.
Save felipelavinz/767128 to your computer and use it in GitHub Desktop.
Accordion Menu for jQuery
jQuery.fn.accordionMenu = function(settings){
settings = jQuery.extend({
delegate_selector : 'a', // {selector} selector that will receive the click
expandable_selector : 'dd', // {selector} selector that will expand/collapse
hide_onload : true, // {boolean} wheter to hide menus on page load
active_class : 'active', // {string} the name of the class that will be applied to the active element
fx : false, // {boolean} whether to use slide effect or simple hide/show
fx_speed : 'normal', // {string|number} speed keyword (slow, normal, fast) or milliseconds
dblclick_go : true // {boolean} whether to work as normal link on double click when menu is displayed
}, settings);
$menu_el = jQuery(this);
if ( settings.hide_onload ) $menu_el.find(settings.expandable_selector).hide();
$menu_el.delegate(settings.delegate_selector, 'click', function(){
var $e = jQuery(this),
target = $e.attr('rel'),
$target = jQuery('#'+target),
$current_active = $menu_el.find(settings.expandable_selector+'.'+settings.active_class);
if ( $target.length ) {
switch(settings.fx){
case true:
if ( settings.dblclick_go && $target.hasClass(settings.active_class) ) {
return true;
} else {
$current_active.slideUp(settings.fx_speed, function(){
jQuery(this).removeClass(settings.active_class);
});
$target.slideDown(settings.fx_speed, function(){ jQuery(this).addClass(settings.active_class) });
}
break;
default:
if ( settings.dblclick_go && $target.hasClass(settings.active_class) ) {
return true;
} else {
$current_active.hide(0, function(){
//jQuery(this).removeClass(settings.active_class);
$menu_el.find('.'+settings.active_class).removeClass(settings.active_class);
});
if ( $current_active.attr('id') !== target ) $target.show(0, function(){ jQuery(this).addClass(settings.active_class); /*$e.parent().addClass(settings.active_class);*/ });
}
}
return false;
} else {
/**
* Si el menu de navegacion correspondiente no existe,
* funciona como link normal
**/
return true;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment