Simple JQuery Accordion Plugin - Version 3.1

This is a simple menu done in JQuery. It's an evolved version of the menu first presented in this weblog entry.

I've got information man! New shit has come to light!

--Jeff Bridges as 'The Dude' in The Big Lebowski

There is no cookie stuff. Please control state through your page (generation) code by adding the expand class to items that need to be expanded t page load time.

If this plugin is useful for you feel free to download it and use it on your website.

This plugin 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 expand to the item we want to be expanded.

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

  1. jQuery.fn.initMenu = function() {
  2. return this.each(function(){
  3. var theMenu = $(this).get(0);
  4. $('.acitem', this).hide();
  5. $('li.expand > .acitem', this).show();
  6. $('li.expand > .acitem', this).prev().addClass('active');
  7. $('li a', this).click(
  8. function(e) {
  9. e.stopImmediatePropagation();
  10. var theElement = $(this).next();
  11. var parent = this.parentNode.parentNode;
  12. if($(parent).hasClass('noaccordion')) {
  13. if(theElement[0] === undefined) {
  14. window.location.href = this.href;
  15. }
  16. $(theElement).slideToggle('normal', function() {
  17. if ($(this).is(':visible')) {
  18. $(this).prev().addClass('active');
  19. }
  20. else {
  21. $(this).prev().removeClass('active');
  22. }
  23. });
  24. return false;
  25. }
  26. else {
  27. if(theElement.hasClass('acitem') && theElement.is(':visible')) {
  28. if($(parent).hasClass('collapsible')) {
  29. $('.acitem:visible', parent).first().slideUp('normal',
  30. function() {
  31. $(this).prev().removeClass('active');
  32. }
  33. );
  34. return false;
  35. }
  36. return false;
  37. }
  38. if(theElement.hasClass('acitem') && !theElement.is(':visible')) {
  39. $('.acitem:visible', parent).first().slideUp('normal', function() {
  40. $(this).prev().removeClass('active');
  41. });
  42. theElement.slideDown('normal', function() {
  43. $(this).prev().addClass('active');
  44. });
  45. return false;
  46. }
  47. }
  48. }
  49. );
  50. });
  51. };
  52. $(document).ready(function() {$('.menu').initMenu();});

Download

Download everything in a zip file