A Stylish Navigation Menu With jQuery

22 January 2010 Friday

This application was viewed 1037 times




A Stylish Navigation Menu With jQuery

Introduction

As you may know, the first 20 seconds of a new visitor’s interaction with a website determine whether they are going to stay or leave. This means you have to follow common design practices and put everything where the user would expect it to be. A crucial part of this process is developing an easy to use navigation menu.

In this tutorial we are going to make a stylish CSS + XHTML navigation menu with the help of the jQuery library.

So download the tutorial files above and keep on reading..

View The DEMO

Step 1 – the XHTML

The XHTML code is simple and search engine friendly.

index.html

01.<div id="menu-container">
02.<ul id="navigationMenu">
03.<li><a href="#" class="normalMenu">Home</a></li>
04.<li><a href="#" class="normalMenu">Services</a></li>
05.<li><a href="#" class="selectedMenu">Our clients</a></li>
06.<li><a href="#" class="normalMenu">The team</a></li>
07.<li><a href="#" class="normalMenu">About us</a></li>
08.<li><a href="#" class="normalMenu">Contact us</a></li>
09.</ul>
10.</div>

The entire navigation menu is basically one unordered list. You can introduce new menu items by just adding more LI elements and setting their respective text and target URLs.

An important thing to note here is how you can mark a menu item as selected by default (or active) – just assign it the selectedMenu class (line 5). For example, here we are highlighting the “Our clients” page. You can set it up manually for a static site, or use PHP to do ti dynamically.

Step 2 – jQuery

As you can see from the illustration above, we are going to clone the set of links defined in our XHTML (the dark ones) and assign them the hoverMenu CSS class, which floats them above the default ones.

This strategy has many advantages – firstly it keeps the XHTML cleaner, because you do not have to add those links manually to the page, and secondly it guarantees that you will have clean and working navigation regardless of the visitor’s javascript support – great for both usability and SEO.

Now lets see what happens in our script.js.

script.js

01.$(document).ready(function(){
02.// executed after the page has finished loading
03.  
04.    $(´#navigationMenu li .normalMenu´).each(function(){    // for each menu item
05.  
06.        // create a duplicate hyperlink and position it above the current one
07.        $(this).before($(this).clone().removeClass().addClass(´hoverMenu´));
08.  
09.    });
10.  
11.    $(´#navigationMenu li´).hover(function(){   // using the hover method..
12.  
13.        // we assign an action on mouse over
14.        $(this).find(´.hoverMenu´).stop().animate({marginTop:´0px´},200);
15.        // the animate method provides countless possibilities
16.  
17.    },
18.  
19.    function(){
20.        // and an action on mouse out
21.        $(this).find(´.hoverMenu´).stop().animate({marginTop:´-25px´},200);
22.  
23.    });
24.  
25.});

Great, but what have we just done? First we used the $(‘document’).ready method to insure that the code is executed after the page has finished loading.

Then we looped through all the links and cloned them, assigning a new CSS class  – hoverMenu.

After this we used the hover() method to easily add event handlers for both the mouseover and the mouseout event.

Later we used the animate method – a really powerful tool in the jQuery arsenal. In this case we provided it with a new marginTop value and the duration of the effect. jQuery will handle all the animation.

Note the use of the stop() method – it stops all currently active animations and thus prevents the stacking of different animation effects on top of each other guaranteeing a smooth user experience.

Step 3 – the CSS

Lets take a look at our CSS style sheet.

demo.css

01./* Page styles */
02.  
03.body,h1,h2,h3,p,td,quote,small,form,input,ul,li,ol,label{   /* resetting some elements for better browser compatibility */
04.    margin:0px;
05.    padding:0px;
06.}
07.  
08.body{   /* styling the body */
09.    margin-top:20px;
10.    font-family:Arial, Helvetica, sans-serif;
11.    color:#51555C;
12.    height:100%;
13.    font-size:12px;
14.}
15.  
16./* Navigation menu styles */
17.  
18.ul{ /* the unordered list */
19.    height:25px;
20.    font-family:Arial, Helvetica, sans-serif;
21.    font-size:12px;
22.}
23.  
24.ul li{
25.    border:1px solid #444444;
26.    display:inline-block;   /* changing the display property */
27.    float:left; /* floating the list items to the left */
28.    height:25px;
29.    list-style-type:none;   /* disabling the list icon */
30.    overflow:hidden;    /* hiding the overflowing content */
31.}
32.  
33.ul li a, ul li a:hover,
34.ul li a:visited{
35.    text-decoration:none;   /* removing the underline text-decoration */
36.}
37.  
38./* styling the links */
39..normalMenu, .normalMenu:visited,
40..hoverMenu, .hoverMenu:visited,
41..selectedMenu,.selectedMenu:visited {/* these properties are shared between the classes */
42.    outline:none;
43.    padding:5px 10px;
44.    display:block;
45.}
46.  
47./* styles that are assigned individually */
48.  
49..hoverMenu,.hoverMenu:visited,
50..selectedMenu,.selectedMenu:visited {
51.    margin-top:-25px;
52.    background:url(img/grey_bg.gif) repeat-x #eeeeee;
53.    color:#444444;
54.}
55.  
56..selectedMenu,.selectedMenu:visited {
57.    margin:0;
58.}
59.  
60..normalMenu, .normalMenu:visited{
61.    color:white;
62.    background:url(img/dark_bg.gif) repeat-x #444444;
63.}

As you can see, we have three main classes that define the looks of our navigation menu. normalMenu – for the normal state of the navigation links, hoverMenu – lighter link that slides down on hover, selectedMenu the active (selected) state.

With this our stylish navigation menu is complete!

Tags: menu, jquery, navigation,


A Stylish Navigation Menu With jQueryBookmark and Share

You write your thoughts

Please login to post comments here from the right corner.

your add
icon free icon icon set vector icon free catalog catalog template jquery news ticker widget tools tutorial php slider video youtube effects menu thumbnail forms facebook zoom image combo multiselect modal polaroid gallery expand code feedback contact navigation slider tagcloud Skateboard psd templates files badges subscribe panel login temlate file free office holland free template tabs prototype css scriptaculous opacity moo autocomplete wordpress themes design theme button images photoshop fonts font nav pdf logo style high javascript pretty photo jqery lightbox Themes Blog popular blog desing joomla xhtml brushes hair footer admin mootols slide show blackbox new download class first plugin upload browse Page effect modalbox web application urlitooltips sliding boxes captions flatchat chat ajax messaging browser hoverlightbox CSS Dev General Javascript PHP SQL search avatar vector Drupal Joomla Oscommerce javaScript phototype manager ajaxplorer flash xml rotator bookmarking pageflip slideshow poll text component script mediaplayer imagegallery pictograms car poster player mp3 radio onlineradio navigations drop-down collection brush tree textures paper pop-up bumpbox web2.0 iphone film frame twitter page fan mail delete dynamic calender form handbook city social paint timeline clock connect bar sharing accordion quotes ticker photo-effects background 3D media player graphic rss leaf green glossy bubble icon comic clean cool web 2.0 png actionscript ietter rotating Menu animated textured site masking pasword app layer sponge bob popup bubble tooltips horizontal prototip tooltip pack charachters comics blog action icon.psd flavour navi tango gradients istanbul mixed css3 sleek animations program html5 website Layer styles ultimate candy Media icons dropdown snow icon innerfade hover sub tag