How to Create Accordion Menu (CSS3+jQuery)


How to Create a Accordion Menu (jQuery Version) [Tutorial]
Topic: jQuery / CSS3
Difficulty: Intermediate
Estimated Completion Time: 30 mins

As you may noticed a few days ago we released a tutorial about How to Create an Accordion Menu in Pure CSS3. As this pure CSS3 Accordion Menu will not work on all browsers because the :target selector in not supported in the oldest browsers we decided to create a jQuery version of this menu. Also jQuery offers more stability and cross browser compatibility support so if you want to provide to your visitors/client a better product you may consider use this jQuery version.

Before you start reading this tutorial I recommend you to read the CSS3 Version as we will not cover all the steps and will focus only on the changes that need to be done for the jQuery version.

Step 1 – HTML Markup

The HTML is the same as the CSS3 version, a nested unordered list which will contain all of our links. We have added a class to each one to be able to add the images and the id is needed for the CSS3 only version. We will keep the CSS3 only version working so if the JavaScript will be disabled on the client browser it will continue working with just CSS.


<ul class="accordion">
<li id="one" class="files">
		<a href="#one">My Files<span>10</span></a>
<ul class="sub-menu">
<li><a href="#"><em>01</em>Sub Menu<span>1</span></a></li>
		</ul>
	</li>
<li id="two" class="mail">
		<a href="#two">Mail<span>20</span></a>
<ul class="sub-menu">
<li><a href="#"><em>01</em>Sub Menu<span>2</span></a></li>
		</ul>
	</li>
<li id="three" class="cloud">
		<a href="#three">Cloud<span>30</span></a>
<ul class="sub-menu">
<li><a href="#"><em>01</em>Sub Menu<span>3</span></a></li>
		</ul>
	</li>
<li id="four" class="sign">
		<a href="#four">Sign Out</a>
<ul class="sub-menu">
<li><a href="#"><em>01</em>Sub Menu</a></li>
		</ul>
	</li>
</ul>

Step 2 – CSS Styling

The changes in the CSS code are very basic. The only thing that we would do is changing and adding some lines of code.

In the next three parts of code the only think that we will do is adding some lines of code to add the “active” class. This class will be used in the jQuery code.


.accordion > li:hover > a,
.accordion > li:target > a,
.accordion > li > a.active {
	color: #3e5706;
	text-shadow: 1px 1px 1px rgba(255,255,255, .2);

	/*background: url(../img/active.png) repeat-x;*/
	background: #a5cd4e;
	background: -moz-linear-gradient(top,  #a5cd4e 0%, #6b8f1a 100%);
	background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#a5cd4e), color-stop(100%,#6b8f1a));
	background: -webkit-linear-gradient(top,  #a5cd4e 0%,#6b8f1a 100%);
	background: -o-linear-gradient(top,  #a5cd4e 0%,#6b8f1a 100%);
	background: -ms-linear-gradient(top,  #a5cd4e 0%,#6b8f1a 100%);
	background: linear-gradient(top,  #a5cd4e 0%,#6b8f1a 100%);
}

.accordion > li:hover > a span,
.accordion > li:target > a span,
.accordion > li > a.active span {
	color: #fdfdfd;
	text-shadow: 0px 1px 0px rgba(0,0,0, .35);
	background: #3e5706;
}

.accordion li.files > a:before { background-position: 0px 0px; }
.accordion li.files:hover > a:before,
.accordion li.files:target > a:before,
.accordion li.files > a.active:before { background-position: 0px -24px; }

.accordion li.mail > a:before { background-position: -24px 0px; }
.accordion li.mail:hover > a:before,
.accordion li.mail:target > a:before,
.accordion li.mail > a.active:before { background-position: -24px -24px; }

.accordion li.cloud > a:before { background-position: -48px 0px; }
.accordion li.cloud:hover > a:before,
.accordion li.cloud:target > a:before,
.accordion li.cloud > a.active:before { background-position: -48px -24px; }

.accordion li.sign > a:before { background-position: -72px 0px; }
.accordion li.sign:hover > a:before,
.accordion li.sign:target > a:before,
.accordion li.sign > a.active:before { background-position: -72px -24px; }

Now we will remove the previous styles (height, overflow, etc.) and we will use the “display: none;” property to hide the sub menus and the “display: block” property to show the sub menus.



.accordion li > .sub-menu {
	display: none;
}

.accordion li:target > .sub-menu {
	display: block;
}

Step 3 – jQuery

First we will link to jQuery library using the last version hosted by Google, if you want you can host it on your own server, it’s your choice. Next add the following code to the bottom of you HTML file just before the closing tag.


<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">

	$(document).ready(function() {

		// Store variables

		var accordion_head = $('.accordion > li > a'),
			accordion_body = $('.accordion li > .sub-menu');

		// Open the first tab on load

accordion_head.first().addClass('active').next().slideDown('normal');

		// Click function

		accordion_head.on('click', function(event) {

			// Disable header links

			event.preventDefault();

			// Show and hide the tabs on click

			if ($(this).attr('class') != 'active'){
				accordion_body.slideUp('normal');
				$(this).next().stop(true,true).slideToggle('normal');
				accordion_head.removeClass('active');
				$(this).addClass('active');
			}

		});

	});

</script>

We will start by storing this two variables. This will help to refer them and avoid to make jQuery jumping in the DOM to many times.



var accordion_head = $('.accordion > li > a'),
			accordion_body = $('.accordion li > .sub-menu');

This line of code will open the first accordion tab when the menu is loaded.


accordion_head.first().addClass('active').next().slideDown('normal');

Now we will disable the links of the accordion and only the sub menu links will be working.


event.preventDefault();

The next code will check if one of the accordion tabs is open and have the active class. So when you click on another tab it will remove and slide up the tab and add the active class and slide down the tab that you have clicked.


if ($(this).attr('class') != 'active'){
				accordion_body.slideUp('normal');
				$(this).next().stop(true,true).slideToggle('normal');
				accordion_head.removeClass('active');
				$(this).addClass('active');
			}

Conclusion

That’s it! I hope you have enjoy it and be useful for you. Don’t forget to let us know your feedback and subscribe.

Preview

Download Accordion Menu




90 Comments

  1. Darrell Mar 12, 9:47 am
  2. Valeriu Mar 12, 7:04 pm
  3. Anatoly Mar 19, 7:38 pm
  4. Caileen Mar 21, 8:57 am
  5. Heidi Mar 26, 2:12 pm
  6. Valeriu Mar 26, 11:08 pm
  7. Heidi Mar 27, 12:43 pm
  8. Valeriu Mar 27, 3:38 pm
  9. tsorya Apr 8, 4:39 pm
  10. Valeriu Apr 12, 4:31 pm
  11. Valeriu Apr 12, 4:35 pm
  12. Matthew Apr 15, 3:47 am
  13. Matthew Apr 15, 3:51 am
  14. TRex Apr 18, 6:10 pm
  15. Valeriu May 15, 11:40 pm
  16. Dave Apr 19, 4:46 am
  17. ali Apr 19, 9:49 am
  18. Omar Apr 21, 2:25 am
  19. Valeriu May 15, 11:42 pm
  20. Max Nov 22, 10:34 pm
  21. Marcel van Est Apr 25, 8:06 pm
  22. Marcel van Est Apr 25, 8:08 pm
  23. Marcel van Est Apr 25, 8:09 pm
  24. Dhananjay Apr 26, 1:45 pm
  25. Aless Mc Apr 27, 5:31 pm
  26. Aless Mc Apr 27, 5:54 pm
  27. Valeriu May 15, 11:46 pm
  28. Brooks Apr 27, 8:12 pm
  29. Valeriu May 15, 11:55 pm
  30. Max Nov 22, 10:45 pm
  31. Batman May 4, 6:52 pm
  32. Valeriu May 15, 11:48 pm
  33. Enrique Aug 21, 6:48 pm
  34. Shannon May 11, 11:44 pm
  35. Leonie May 14, 11:42 pm
  36. Valeriu May 15, 11:49 pm
  37. efi May 24, 4:32 pm
  38. Valeriu May 27, 2:28 pm
  39. Anjana Feb 5, 6:14 am
  40. Tom May 24, 7:18 pm
  41. Nelson May 24, 10:54 pm
  42. Todd May 26, 6:52 am
  43. mspace Jun 12, 4:36 pm
  44. Valeriu Jun 19, 3:52 am
  45. mspace Jun 20, 1:58 pm
  46. Ruiwei Jun 30, 3:36 am
  47. David Jul 3, 3:57 pm
  48. Nick Jul 9, 3:58 pm
  49. Todd Aug 7, 1:59 am
  50. Kryss Sep 19, 1:24 am
  51. John Sep 27, 11:45 pm
  52. John Sep 28, 12:04 am
  53. shogunR Oct 1, 5:41 pm
  54. Larryhommes Oct 3, 4:41 pm
  55. googs Oct 23, 8:42 pm
  56. Itacipri Dec 12, 5:11 am
  57. Marcy Feb 2, 8:57 pm
  58. Marcy Feb 4, 6:10 am
  59. Marcy Feb 4, 6:12 am
  60. Valeriu Feb 5, 9:31 pm
  61. Marcy Feb 5, 11:22 pm
  62. Kostas Feb 21, 3:20 pm
  63. nizil Feb 21, 6:57 pm
  64. Kiran Feb 26, 1:40 pm
  65. Reini Mar 4, 8:14 pm
  66. Thanigai Arasu Mar 20, 7:46 am
  67. Guuz Apr 9, 9:11 am
  68. Carlos May 9, 1:26 pm
  69. Guuz May 11, 9:20 am
  70. Guuz May 11, 9:33 am
  71. radha krishna May 22, 10:02 am
  72. Jorge Carreira May 27, 4:37 pm
  73. Drew May 30, 9:45 pm
  74. Todd B Jun 24, 9:32 pm
  75. Adriano May 13, 9:57 pm
  76. iusebootstrap Jul 29, 8:44 pm
  77. Orlis Aug 12, 11:56 am
  78. David Aug 23, 3:51 pm
  79. sara Oct 3, 9:57 pm
  80. Jonathan Oct 16, 2:13 am
  81. Andrea Nov 12, 12:19 pm
  82. Adriano Martins Jan 28, 9:45 pm
  83. Zysman May 12, 5:34 am
  84. hue May 19, 6:05 pm
  85. tomdouble May 20, 3:16 am
  86. hueman May 20, 10:36 am
  87. Massood Jul 11, 9:02 am
  88. esiaz Aug 26, 12:46 pm
  89. peyman Sep 1, 1:44 am
  90. Erdy Marmito Sep 29, 2:20 pm