Register now or login here to start promoting your blog and your favourite articles.
Create an Attractive jQuery Menu with Fadein and Fadeout Effect
24 Jul 2009 - 28 Comments
We will be learning how to create an attractive menu with more advanced CSS and simple jQuery script. It uses jQuery builtin fadein and fadeout effect.
Author: kevin | Source: queness
Demonstration Download

Introduction

I got this tutorial request from a reader, if you have any request at all, please contact me, give me some suggestions for the upcoming tutorial! :)

Apparently, I have seen this kind of menu before, in Dragon Interactive . During the implementation, I was having a bit of problem, I thought it's just a simple fadein and fadeout effect, but it actually required a lot of CSS works and the right images.

Fade in menu Preview

1. HTML

HTML is very simple, just a list of links. :)

<ul id="navMenu">
	<li><a href="#">Test 1</a></li>
	<li><a href="#">Test 2</a></li>
	<li><a href="#">Test 3</a></li>
	<li><a href="#">Test 4</a></li>
	<li><a href="#">Test 5</a></li>
	<li><a href="#">Test 6</a></li>
</ul>

2. CSS

CSS is little bit complicated this time, therefore, I have put a lot of comments. I hope it will explain everything you need to know.

body {
	background:#222;	
}

#navMenu {
	margin:0; 
	padding:0;
	list-style:none;	
	font-family:arial;
	text-align:center;
	line-height:60px;
}

	#navMenu li {
		float:left;	
		
		/* default background image	*/
		background:url(default.jpg) no-repeat center center;	
		
		/* width and height of the menu item */
		width:120px;							
		height:70px;
		
		/* simulate pixel perfect using border */
		border-left:1px solid #111;				
		border-right:1px solid #333;
		border-top:1px solid #555;
		border-bottom:1px solid #333;
		
		/* must set it as relative, because .hover class top and left with absolute position will be positioned according to li.	*/
		position:relative;			
	}

	#navMenu li a {
		/* z-index must be higher than .hover class */
		z-index:20;				
		
		/* display as block and set the height according to the height of the menu to make the whole LI clickable	*/
		display:block;	
		height:70px;
		position:relative;
		color:#777;
	}

 	#navMenu li .hover {
 		/* mouseover image	*/
		background:url(over.jpg) no-repeat center center;		

		/* must be postion absolute 	*/
		position:absolute;	
		
		/*	width, height, left and top to fill the whole LI item	*/
		width:120px;	
		height:70px;
		left:0; 
		top:0;	
		
		/* display under the Anchor tag	*/
		z-index:0;		
		
		/* hide it by default	*/
		display:none;	
	}	

	#navMenu li.selected {
		/* selected image	*/
		background:url(selected.jpg) no-repeat center center;	
	}

3. Javascript

We are using the jQuery built-in fade in and face out effect. First of all, we need to append the div.hover to the list. After that, just a normal mouse hover event with the fadein and fadeout transition.

	
$(document).ready(function () {

	//Append a div with hover class to all the LI
	$('#navMenu li').append('
<\\/div>'); $('#navMenu li').hover( //Mouseover, fadeIn the hidden hover class function() { $(this).children('div').fadeIn('1000'); }, //Mouseout, fadeOut the hover class function() { $(this).children('div').fadeOut('1000'); }).click (function () { //Add selected class if user clicked on it $(this).addClass('selected'); }); });

Conclusion

This is it, I haven't tested it on IE6 (I don't have one :()... if you having problem with that, please let me know, hopefully I will able to find a way to solve it, or if you can come out with a solution that'd be highly appreaciated! :) Thanks.

Last but not least, I need your support :) If you like this article, please help me to promote it by adding this post to your bookmark. Or you can subscribe to my RSS for more jQuery tutorial and design inspiration posts! Thanks!

Demonstration Download

Copyright & Usage

The effects and techniques demonstrated in tutorials on Queness can be used in whatever manner you wish without attribution. You cannot copy whole tutorials (unless permission is given), either in English or translated to another language.

Share This Post to Support Me! :)


Comments

piese auto ieftine on 8 Feb 2010 says:
It's slightly harder to use, but the menus are excellent. The final result counts.
Jan Kovařík on 15 Jan 2010 says:
Just great, thank you for the excellent tutorial. I have one problem ... Can I change the color of the link (as slowly as the background)?
Daniel on 30 Nov 2009 says:
Very nice tutorial.. ive started off with mootools, and i find i way to hard to get started with. So now im looking for nice beginner Jquery tuts.. and this looks awsome, Nice effect, short script :) nice.
WEbRISES on 24 Nov 2009 says:
Great tut!! Really!! But if you tried, you see that when you select a \"li\" element it change to class \"selected\", but when you click another \"li\" the first \"selected\" doesnt return as the original.

To adjust this, just add this where it calls
//Add selected class if user clicked on it

$(this).addClass(selected)
.siblings(#navMenuli).removeClass(selected);

Done!!
Thanks
Jonath Lee on 18 Nov 2009 says:
Thanks Kevin for your reply. But however, it seems didnt work for me...When I added the .stop() the button still glow based on how many time I hover it.
kevin on 17 Nov 2009 says:
hi, you call add stop() to it,

$(this).stop().children(div).fadeOut(1000);
Jonath Lee on 16 Nov 2009 says:
As Cinq mentioned earlier, it really need to add in the callback function. Can anyone show me how please?
Cinq on 22 Oct 2009 says:
You forgot a callback function !
When I hover back and forth a little fast, or just on and off again itll keep going on and off for a long time.

With the callback itll stop fadeing in as soon as the mouse is out again :)
Protomn on 20 Oct 2009 says:
Works great in IE6!
krupnikas on 24 Sep 2009 says:
How can i manade to control speed of animation?
Also is there a possibility to make like in this \Dragon inc\ when you mouse over and then try to select other button but there is no need for all fade in and out animation...

Leave a comment

Subscribe RSS Subscribe RSS, Keep yourself updated with queness latest posts!
Pixel Crayons Increase your traffic!

Buy wholesale computers directly from China at DHgate.com

Discover the tools to build your e-Commerce Site with Netfirms

  •  
  •  
  •  
  •  
  •  

Community News

Recent Comments

Random Posts



Digging in Wordpress
View all posts and news Back to top

About the Author

A web designer and developer who is passionate and keen on contributing to the web development industry. Feel free to say hi to me, or follow me on twitter.

Kevin Liew

Partner

  • Web and Designers
  • CSS Style
  • PV.M Garage Blogzine - (Italian)
  • TutsValley
  • Designrfix
  • CoolVibe
  • Web Developer Juice
  • Denbagus
  • Web Hosting Secret Revealed
  • PSD to HTML Conversion
  • BlueHost