Simple JQuery Accordion menu - Redux
This is a simple menu done in JQuery. It's an evolved version of the menu first presented in this i-marco weblog entry. I made these changes after receiving all sorts of change requests and other feedback on the original version. This new version is rather flexible and caters to various needs people may have when building a website. It also allows multiple instances of the menu on the same webpage, unlike the original.
If this code snippet is useful for you feel free to download it and use it on your (NON COMMERCIAL) website.
This snipped is discussed on my blog in this post.
1: Standard accordion menu
By default, the menu will launch in collapsed state and function like an accordion.
2: Accordion with first submenu expanded at page load
Same as the first example but with the first submenu expanded when the page loads. This is achieved by adding a CSS class expandfirst to the unordered list.
3: Non-accordion (standard expandable menu)
This is a standard expandable menu without accordion functionality. This is achieved by adding a CSS class named noaccordion to the unordered list.
4: Collapsible accordion with first item expanded at page load
This menu works like an accordion when one menu item is already expanded but will collapse completely when the expanded item is clicked. Additionally it has the first item expanded at page load. This is achieved by adding a CSS class named collapsible and another CSS class named expandfirst like in example 2.
Source
-
function initMenus() {
-
$('ul.menu ul').hide();
-
$.each($('ul.menu'), function(){
-
$('#' + this.id + '.expandfirst ul:first').show();
-
});
-
$('ul.menu li a').click(
-
function() {
-
var checkElement = $(this).next();
-
var parent = this.parentNode.parentNode.id;
-
if($('#' + parent).hasClass('noaccordion')) {
-
$(this).next().slideToggle('normal');
-
return false;
-
}
-
if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
-
if($('#' + parent).hasClass('collapsible')) {
-
$('#' + parent + ' ul:visible').slideUp('normal');
-
}
-
return false;
-
}
-
if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
-
$('#' + parent + ' ul:visible').slideUp('normal');
-
checkElement.slideDown('normal');
-
return false;
-
}
-
}
-
);
-
}
-
$(document).ready(function() {initMenus();});
Download
Download everything in a zip file