Creating Stylish Sliding Menu With JQuery

Today, we are going to create a very stylish sliding menu as shown in the picture on the left with the help of JQuery. Before we move on, please have a look at the demo of this menu.

In order to create this menu what is usually known as accordion, we will be adding HTML of course, CSS to style the menu and finally JQuery to create the kind of effect this menu has. Of course, once you create this menu, you can style it anyway you want; for example you can create background, active or hover images which can make this menu even more attractive or simply use colors instead. Ok, so let’s move on to creating this menu.

.

HTML

We will be using unordered lists for our menu. Here is how it looks like:

   <ul class="parent">
    	<li>
            <a href="#" class="slide">Title One</a>

            <ul class="child">
            	<li><a href="#">Link One</a></li>
                <li><a href="#">Link Two</a></li>
                <li><a href="#">Link Three</a></li>
                <li><a href="#">Link Four</a></li>
                <li><a href="#">Link Five</a></li>
            <!--<span class="hiddenSpellError" pre=""-->ul>

         </li>
    <!--<span class="hiddenSpellError" pre=""-->ul>
<!-- and more -->

As can be seen, there are two lists, the one with the class parent and other with that of child. The first list item of the parent list contains a link which is actually a heading that when clicked will show sub-links which are there in the child list. So the same markup structure goes for the rest of the headings and links.

The CSS

Here is the CSS for the menu:

a
{
	outline:none;
}

ul
{
	list-style:none;
}

ul.parent
{
	margin:0px;
}

ul.parent a
{
	display:block;
	width:200px;
	border-bottom:1px solid #336699;
	background:#3399FF;
	color:#FFFFFF;
	font-weight:bold;
	text-decoration:none;
	padding:7px;
}

ul.parent a:hover
{
	color:#000000;
	background:#66CC66
}

ul.child a
{
	background:#333333;
	color:#FFFFFF;
	border-bottom:1px solid #f6f6f6;
	font-weight:normal;
	margin-left:-40px;
	padding:7px;
	width:200px;
	display:block;
	line-height:10px;
}

ul.child a:hover
{
	color:#3333333;
	background:#CCCCCC
}

In some browsers, when you click on a link, an outline (sort of dotted border) is created around it, so we disable that behavior by setting:

a
{
	outline:none;
}

Then we remove the list style so that there are no bullets for ul lists and also remove any margins around it by setting value to 0:

ul
{
	list-style:none;
}

ul.parent
{
	margin:0px;
}

After that we set links’ display style to block so that we could apply a width to them and also set some other styles such as background:

ul.parent a
{
	display:block;
	width:200px;
	border-bottom:1px solid #336699;
	background:#3399FF;
	color:#FFFFFF;
	font-weight:bold;
	text-decoration:none;
	padding:7px;
}

Finally we set styles when mouse is over the links using :hover pseudo selector:

ul.parent a:hover
{
	color:#000000;
	background:#66CC66
}

JQuery

We use JQuery’s ready event which fires when DOM (document object model) becomes ready, the short version of this event is:

	$(function(){
	// code here
	});

When the page has loaded, we want to make sure that only the first heading and links are shown which can be done as follows:

	// hide all links except for the first
	$('ul.child:not(:first)').hide();

For each selected heading we set its color to #FF9900, so we want to change the color of the first heading so as to make it feel like selected by default when the page has loaded:

	$("a.slide:first").css("background-color","#FF9900");

Then we write the code for each heading’s click event:

	$('ul.parent a.slide').click(function(){
	});

What it means is that a link with class slide which is inside ul with class parent gets clicked, run the code specified. First, we set background color and mouse over and mouse out color for the current clicked heading and other headings:

	$('ul.parent a.slide').css("background-color","#3399FF");

	$('ul.parent a.slide').mouseout(function(){
		$(this).css("background-color","#3399FF");
	});

	$('ul.parent a.slide').mouseover(function(){
		$(this).css("background-color","#66CC66");
	});

	$(this).mouseout(function(){
		$(this).css("background-color","#FF9900");
	});

We now slide up all child elements:

	// slide all up
	$('ul.child').slideUp('slow');

Then we show the links of the current clicked heading:

	// show the links of current heading
	$(this).next().find('a').show();

$(this) refers to current clicked heading, that is $(‘ul.parent a.slide’, the next() method finds the next element in the hierarchy with respect to clicked element which is ul with class child, we find links inside that using find() method and finally show them. Finally, we slide down the ul with class child and prevent the default action of clicked link or heading.

Finally, we slide down the current heading:

  // slide down current heading
  $(this).next().slideDown('fast');

And we prevent default action of clicked heading which is actually
a link using:

// prevent default action
return false;

Here is the entire JQuery code:

	$(function(){
		// hide all links except for the first
		$('ul.child:not(:first)').hide();
		$("a.slide:first").css("background-color","#FF9900");

		$('ul.parent a.slide').click(function(){
			$('ul.parent a.slide').css("background-color","#3399FF");

			$('ul.parent a.slide').mouseout(function(){
				$(this).css("background-color","#3399FF");
			});

			$('ul.parent a.slide').mouseover(function(){
				$(this).css("background-color","#66CC66");
			});

			$(this).mouseout(function(){
				$(this).css("background-color","#FF9900");
			});

			// slide all up
			$('ul.child').slideUp('slow');

			// show the links of current heading
			$(this).next().find('a').show();

			// slide down current heading
			$(this).next().slideDown('fast');

			// prevent default action
			return false;
		});
	});

Download Code.