jQuery: Navigation menu
In this tutorial i’m going to show you how to make a navigation menu with Photoshop, jQuery, xHTML, and CSS.
Photoshop
For the Photoshop part I’ve made a video:
HTML
Now we’ve made the button images in Photoshop we’re going to write the code. First the HTML.
Make a new HTML file and call it index.html.
Type (or copy paste) the doctype and the basic HTML tags:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>jQuery menu</title>
</head>
<body>
</body>
</html>
Inside the body tags create a div with a id of menu, this will be the container of our buttons. Inside the menu div we make an unordered list, and for each button a list item with the actual link inside it. Give each button its own class.
<div id="menu">
<ul>
<li><a href="#" class="home"></a></li>
<li><a href="#" class="about"></a></li>
<li><a href="#" class="links"></a></li>
<li><a href="#" class="contact"></a></li>
</ul>
</div><!--/MENU-->
CSS
Now we will style our menu with CSS inside the head tags include a stylesheet or insert the tags for a styling block.
First reset the padding and margin so you don’t get any problems with default margins you don’t want to have. Then we’re going to style our menu container.
<style type="text/css" media="screen">
* { margin:0; padding:0; }
#menu {
margin:50px auto 0 auto;
height:35px;
overflow:hidden;
position:relative;
width:404px;
}
</style>
The with of 404px is because we want 1px space between the buttons. Overflow:hidden will hide every object that gets outside the div menu. The position:relative is in this case a IE fix, IE will not recognize overflow without the object set to relative.
Then we give the list items some default styling and a left margin of 1px so there is a gap of one pixel between the buttons.
li {
display:inline;
float:left;
list-style-type:none;
margin-right:1px;
}
And style the links. And by selecting each class give every button his background.
a {
display:block;
height:70px;
top:-35px;
position:relative;
width:100px;
}
a.home {
background:url(images/home.jpg) no-repeat bottom center;
}
a.about {
background:url(images/about.jpg) no-repeat bottom center;
}
a.links {
background:url(images/links.jpg) no-repeat bottom center;
}
a.contact {
background:url(images/contact.jpg) no-repeat bottom center;
}
The position is set to relative so the links are positioned relative to their parent #menu in this case. Top:-35px will set the position of the link -35px relative to the top of #menu.
jQuery
First inlcude the jQuery library and make the script tags in the head tags. We don’t want the code to be processed before the whole site is loaded, else it would slow down the load time, so we check if the document is ready.
<script type="text/javascript" src="http://dimics.com/wp-content/themes/dimics/jquery-1.3.2.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function(){
});
</script>
Then we make the slide function, we want it only to be executed if you hover over the link. After it slides down we want it to slide in, so we make another function behind it:
$("a").hover(function(){ $(this).animate({top:"0px"}, "300"); }, function(){ $(this).animate({top:"-35px"}, "300"); });
The animate function will animate the link with the top set to -35px, to an top value of 0px, and after that animate it back to -35px, the 300 defines the time in milliseconds, you can also change it in slow, normal, or fast. And because the overflow of the menu div is set to hidden you only see 35px of the button.
But we have a problem, now it will stack the functions, so if we would hover 3 times over a button it will slide down and up 3 times. To solve this problem we put the stop function before the animation:
$("a").hover(function(){ $(this).stop().animate({top:"0px"}, "300"); }, function(){ $(this).stop().animate({top:"-35px"}, "300"); });
You can customize the menu by changing the background images and colors.
Greetz,
Durgé
Thanks for posting about this, I would love to read more about this topic.
niec thanks
hi
wat if we want to do a little randomizing trick and every time when a visitor refresh page or new visit comes to page the name of buttons change
like home can be at last button and contact can be at first button
can u plzz tell me how to do this
thnx
In Firefox doesn’t work!
Can you explain what doesn’t work. Because i have it working right now in Firefox 3.5
Make sure to have javascript enabled. To enable go to tools > options > content > check enable javascript
hey how can i make this in a vertical navigation menu???
i want to make something similar but cant get it to work in vertical position…
plz help!!!
To achieve this make sure every button has got a parent, with the overflow set to hidden (in the horizontal menu we use the menu div). So include every button in a div or another object you can even experiment with the li and a objects.
Hope this will help you further, just expirement with different settings and eventually you will figure it out.
Hey thnx 4 taking time & helping out.
I tried with li but it didnt work. The button image doesnt appear right if I use li tags.
But i will try it with div.
Is it possible for you to write the code for vertical navigation menu. You can also mail it to me
I would really appreciate that.
Thnx
Thank you very much for that nice entry.
u r god
this is excellent. I have one small problem, on page I’ve build I have an a href=”mailto:xxxx”. Now that scrolls up and down, and I don’t want it to. Any suggestion how I could stop that happening. Tried in Firefox, IE, Safari and Chrome, all seem to show same behaviour.
thanks again for superb menu.
Why this menu works is because it are only images. If you make an image (like in the beginning) of the video, with the text Mail me or something. And just follow the steps it will looke the same as the demo.
If you dont want it to be an image you should place a div on top of it (maybe with a negative margin) and tweek the css.
This is a great tutorial! However with I include the file on my existing page (I’m coding in PHP) it justifies the whole page left! (I have the page justified center)
Any suggestions will be appreciated!
I use the a, and li to style the menu. I assume you have more links and lists on your site if you have copied the css to your site all the li will float to the left.
The solution is to give the navigation menu an unique class,
so add for example: class=”navBar” to the li and a tags,
and style instead of a{} and li{}, a.navBar{} and li.navBar{}
(also in the javascript)
I hope this will solve your problem.
perfect! THANKS A TON!
sorry but i have a IE (i think 7.0) and doesn’t work.
in firefox works very well but in IE only shoy the motion (tween?). the complet image go up and down but doesn’t hide
Here it works fine in IE 7. Did you change the code? Please make sure that you have position:relative at the #menu css.
This is a IE bug. If you haven’t set the container to relative the overflow won’t be hidden.
Hope this will help.
you are a genius, my hero…..etc….. thanks, i change the #menu css to external file; only add the code in the index file….. now works well in FF and IE…..thanks….
it’s me again:
I would like when select from, at the time the page is displayed, the button selected option to stay down
If you use wordpress, style the .current_page_item.
And else ad on the page the class to your li item from the page.
Use the styles that are animated with javascript but then in the css.
thank you
[…] : visit live demo : […]
Thanks for ur brilliant tutorial xx
Thank you, very nice xD
[…] Visit Tutorial […]
en, for IE, we must add “position:relative” to the #menu css, or else it could not works fine in IE, that is because the overflow could not works fine in IE.
[…] 最终成果:下载链接 演示地址。 原文来自:http://dimics.com/photoshop/jquery-navigation 2010年5月28日 JQuery | 没有评论 | 标签: JQuery, 导航菜单 function […]
I forgot to thank you for your awesome tutorial…
I am having a conflict between your nav and the version of coda slider I am trying to run on the site. Can you help?
Thank you.
Really good
i got this working nicely with my own images.. even did another menu that is vertical with vertical animation.. looks sick! BUUUUUTTT.. i tried putting them into my site and they are affecting every link on the page.. hopefully by tonight i have this problem fixed but if you would be so kind to let me know what an easy fix would be i would be very grateful.. if not.. i friggin love this and you did a GREAT job!!
Jup i made a mistake.. ;p.. In the css ad the id of the container.. in my code it would be “#menu a” instead of just “a”..
Thanks for your positive comment, glad you like it :).
ahhhh! you are da man!! thanks bro.. i LOVE this thing..
Thanks for useful tutorial. Waiting for your new wordpress tutorials. keep up good work. 😉
[…] : visit live demo […]
really cutting-edge menue.expecting more in future.
What i need to do for make menu have a current selector image?
WOW! Looks really cool, i want this on my site…But I’ve got one problem. In the css it sais: * {margin:0; padding:0;}
So of course all of my other elements are now without padding and margin, which doesn’t look really good for me.
And when I copy this only to the menu it doesn’t work.
Pls reply and I know my english is shitty 😀
Quite possibly the most succinct and current info I came across about this subject
[…] 到这里,我们已经完成了使用Photoshop,jQuery,xHTML和CSS来制作导航菜单的。 最终成果:下载链接。 原文来自:http://dimics.com/photoshop/jquery-navigation […]
thanks awesome tutorial
[HELP] where to add “CLASS=”navBar”” on JAVASCRIPT?
to add “CLASS=”navBar”” oN
Thanks for the great tutorial!
amazing
it’s look like a jelly, i think but still amazing.
could you have idea for making menu diagonal,like a menu with surounding the cycle(lingkaran).
how to show active page link to this navigation
[…] jQuery Navigation menu […]
Hey! Quick question that’s completely off topic. Do you know how to make your site mobile friendly? My website looks weird when viewing from my iphone. I’m trying to find a theme
or plugin that might be able to correct this issue.
If you have any recommendations, please share. Many thanks!
Thanks!
You’re the only one that explained it easily! you helped me a lot!
Hey there would you mind stating which blog platform you’re working with? I’m looking to start my own blog in the near future but I’m having a hard time deciding between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I’m looking for something completely unique.
P.S Sorry for getting off-topic but I had to ask!
@kill There are many wordpress themes and plugins that will make your site mobile friendly, just search for “wordpress mobile” and you’ll find a lot of solutions.
@carport We use WordPress, and I would recommend it if you are going to blog. It’s fully customisable and very well documented. Plus there are a lot of tutorials, themes, and plugins out there that are awesome and free.
[…] http://dimics.com/photoshop/jquery-navigation […]
i use your Menu, which is excellent… thanks , i have only one question can you explain if i have to make the hover position as a active position as well so if some one click on it the hover position stay remain the same for that page until some one click on the other button
[…] coder 2 mins ago 0 View Share Tweet Pinit Google+ Email […]
The comments are closed.
The comments are closed.