1: Standard accordion menu

By default, the menu will launch in collapsed state and function like an accordion.

  • Weblog Tools
  • Programming Languages
  • Marco's blog (no submenu)
  • Cool Stuff
  • Search Engines

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.

  • Weblog Tools
    • PivotX
    • WordPress
    • Textpattern
    • Typo
  • Programming Languages
  • Marco's blog (no submenu)
  • Cool Stuff
  • Search Engines

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. This menu 'remembers' state for the last opened menu, not all of them.

  • Weblog Tools
  • Programming Languages
  • Marco's blog (no submenu)
  • Cool Stuff
  • Search Engines

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.

  • Weblog Tools
  • Programming Languages
    • PHP
    • Ruby
    • Python
    • PERL
    • Java
    • C#
  • Marco's blog (no submenu)
  • Cool Stuff
  • Search Engines

Source Code

  1. function initMenus() {
  2. $('ul.menu ul').hide();
  3. $.each($('ul.menu'), function(){
  4. var cookie = $.cookie(this.id);
  5. if(cookie != null && String(cookie).length < 1) {
  6. $('#' + this.id + '.expandfirst ul:first').show();
  7. }
  8. else {
  9. $('#' + this.id + ' .' + cookie).next().show();
  10. }
  11. });
  12. $('ul.menu li a').click(
  13. function() {
  14.  
  15. var checkElement = $(this).next();
  16. var parent = this.parentNode.parentNode.id;
  17.  
  18. if($('#' + parent).hasClass('noaccordion')) {
  19. if((String(parent).length > 0) && (String(this.className).length > 0)) {
  20. if($(this).next().is(':visible')) {
  21. $.cookie(parent, null);
  22. }
  23. else {
  24. $.cookie(parent, this.className);
  25. }
  26. $(this).next().slideToggle('normal');
  27. }
  28. }
  29. if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
  30. if($('#' + parent).hasClass('collapsible')) {
  31. $('#' + parent + ' ul:visible').slideUp('normal');
  32. }
  33. return false;
  34. }
  35. if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
  36. $('#' + parent + ' ul:visible').slideUp('normal');
  37. if((String(parent).length > 0) && (String(this.className).length > 0)) {
  38. $.cookie(parent, this.className);
  39. }
  40. checkElement.slideDown('normal');
  41. return false;
  42. }
  43. }
  44. );
  45. }
  46. $(document).ready(function() {initMenus();});
Terug