Easy to Style jQuery Drop Down Menu Tutorial

Easy to Style jQuery Drop Down Menu Tutorial


Easy to Style jQuery Drop Down Menu Tutorial

Introduction

Alright, this time, we are going to make a drop down menu. The main objective is to make it as simple as possible, with some little jQuery effect and easy to customize/ apply different style on it.

To style it into your own design, you just have to modify the a tag CSS style. You can put background images, hover effect and also change the submenu popup animation.

Before we start, I have had made quite a few of tutorials about menu, the following is the list, I guess you might be interested with some of these :)

As usual, we will start with HTML, CSS and finally Javascript.

Advertisement

1. HTML

This would be the most common way to structure a list menu. It would make sense even if the CSS is disabled.

<ul id="nav">
	<li><a href="#">Parent 01</a></li>
	<li><a href="#" class="selected">Parent 02</a>
		<ul>
			<li><a href="#">Item 01</a></li>
			<li><a href="#" class="selected">Item 02</a></li>
			<li><a href="#">Item 03</a></li>
		</ul>
		<div class="clear"></div>
	</li>
	<li><a href="#">Parent 03</a>
	<ul>
		<li><a href="#">Item 04</a></li>
		<li><a href="#">Item 05</a></li>
		<li><a href="#">Item 06</a></li>
		<li><a href="#">Item 07</a></li>
	</ul>			
		<div class="clear"></div>
	</li>
	<li><a href="#">Parent 04</a></li>
</ul>

<div class="clear"></div>

2. CSS

CSS is quite simple, we have to style two UL List - parent menu and submenu. We put float left for the parent menu so that all the list item would display inline. For the submenu, it's hidden by default, and will only display on mouse over event.

body {font-family:arial; font-size:11px;}
.clear {clear:both}	
/* remove the list style */
#nav {
	margin:0; 
	padding:0; 
	list-style:none;
}	
	
	/* make the LI display inline */
	/* it's position relative so that position absolute */
	/* can be used in submenu */
	#nav li {
		float:left; 
		display:block; 
		width:100px; 
		background:#ccc; 
		position:relative;
		z-index:500; 
		margin:0 1px;
	}
		
	/* this is the parent menu */
	#nav li a {
		display:block; 
		padding:8px 5px 0 5px; 
		font-weight:700;  
		height:23px; 
		text-decoration:none; 
		color:#fff; 
		text-align:center; 
		color:#333;
	}

	#nav li a:hover {
		color:#fff;
	}
	
	/* you can make a different style for default selected value */
	#nav a.selected {
		color:#f00;
	}
	
		/* submenu, it's hidden by default */
		#nav ul {
			position:absolute; 
			left:0; 
			display:none; 
			margin:0 0 0 -1px; 
			padding:0; 
			list-style:none;
		}
		
		#nav ul li {
			width:100px; 
			float:left; 
			border-top:1px solid #fff;
		}
		
		/* display block will make the link fill the whole area of LI */
		#nav ul a {
			display:block;  
			height:15px;
			padding: 8px 5px; 
			color:#666;
		}
		
		#nav ul a:hover {
			text-decoration:underline;	
		}

/* fix ie6 small issue */
/* we should always avoid using hack like this */
/* should put it into separate file : ) */
*html #nav ul {
	margin:0 0 0 -2px;
}

3. Javascript

This could be one of the most simplest jQuery scripts. We are using hover mouse event and just the most basic jQuery animation slideUp and slideDown. :) I love jQuery

$(document).ready(function () {	
	
	$('#nav li').hover(
		function () {
			//show its submenu
			$('ul', this).stop().slideDown(100);

		}, 
		function () {
			//hide its submenu
			$('ul', this).stop().slideUp(100);			
		}
	);
	
});

Conclusion

Like this tutorials? You can express your gratitude by visiting my sponsors on the sidebar, bookmark it and help me to spread this tutorial to our friends! :) Thanks!

Author: Kevin Liew

Kevin Liew is a web designer and developer and keen on contributing to the web development industry. He loves frontend development and absolutely amazed by jQuery. Feel free to say hi to me, or follow @quenesswebblog on twitter.

Share the love

147 comments

Adam Gordon
Sun, 3rd February 2013
Tried this. Seemed simple but when you wave the cursor back and forth over two of the Parent links the animation sticks and doesn't fully slide down until you reload the browser window. :/ Reply
roee
Tue, 19th February 2013
seriesly ppl should stop support IE lower than 9.
its popularity is already 12% and dropping down very quick, i found my self sometimes spending hours for this usless browser Reply
anjali
Thu, 7th March 2013
adam is right when the cursor is moved around the links dorpdowns are not woriking out Reply
Brad
Fri, 8th March 2013
Great job. I added a click handler in an attempt to make it compatible with touch devices. Click works, but if you click the background of a dropped down ul the drop down starts flying up and down at warp speed. Any idea how to implement an onclick? http://www.hcca.org Reply
Kevin Liew Admin
Tue, 26th March 2013
You can change javascript line 3 to 13:


$('#nav li').hover(
function () {
//show its submenu
$('ul', this).stop(true, true).slideDown(100);
},
function () {
//hide its submenu
$('ul', this).stop(true, true).slideUp(100);
}
);
Reply
janewb
Wed, 1st May 2013
thanks so much! well laid out tut! will this work on a mobile site? I'm doing a RWD portfolio Reply
Longmatch
Mon, 19th August 2013

How to change menu or submenu's width based upon the contents?
This is nice easy tutorial. Just want to know how to change menu or submenu's width based upon the contents inside the menu. For example, [New products] menu should be long enough to avoid text wrapping. Thanks Reply
M
Mon, 26th August 2013
Hi, I have 2 drop downs (<select>) and a submit button on my form. They display on 3 lines. However, my requirement is they display adjacent to each other - on the same line. How would I do this? Thanks in advance. Reply
LONGMATCH
Sat, 31st August 2013
This plugin works fine at the first load. But the submenu does not slide down or the submenu item cannot show up when navigating to different pages back and forth. Temporary solution is to click Home button, but it does not always work. Please advice. Thanks Reply
Jenny Beaumont
Tue, 25th February 2014
Hey, thanks for sharing, this is very cool! The only thing I'm having trouble with is that it doesn't seem to work on the first rollover. It takes rolling over twice to get the animation to kick in. Any idea where that might come from?

best,
-jennyb Reply
Jennifer
Tue, 15th April 2014
I am using the code you gave with the stop(true,true) but the hover nav is showing on page load. Ideas? Reply

Leave a Comment

Please keep in mind that comments are moderated and rel="nofollow" is in use. You can use [code][/code] if you want to write codes. Don't spam us :) Thanks!

Advertisement