LINK
Download
http://demo.tutorialzine.com/2009/09/stylish-navigation-menu-jquery/demo.zip
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..
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 > |
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.
The navigation menu explained
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 (){ |
04 | $( '#navigationMenu li .normalMenu' ).each( function (){ |
07 | $( this ).before($( this ).clone().removeClass().addClass( 'hoverMenu' )); |
11 | $( '#navigationMenu li' ).hover( function (){ |
14 | $( this ).find( '.hoverMenu' ).stop().animate({marginTop: '0px' },200); |
21 | $( this ).find( '.hoverMenu' ).stop().animate({marginTop: '-25px' },200); |
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
03 | body,h 1 ,h 2 ,h 3 ,p,td,quote, small ,form,input,ul,li,ol,label{ |
10 | font-family : Arial , Helvetica , sans-serif ; |
20 | font-family : Arial , Helvetica , sans-serif ; |
25 | border : 1px solid #444444 ; |
33 | ul li a, ul li a:hover, |
39 | .normalMenu, .normalMenu:visited, |
40 | .hoverMenu, .hoverMenu:visited, |
41 | .selectedMenu,.selectedMenu:visited { |
49 | .hoverMenu,.hoverMenu:visited, |
50 | .selectedMenu,.selectedMenu:visited { |
52 | background : url (img/grey_bg.gif) repeat-x #eeeeee ; |
56 | .selectedMenu,.selectedMenu:visited { |
60 | .normalMenu, .normalMenu:visited{ |
62 | background : url (img/dark_bg.gif) repeat-x #444444 ; |
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!
No comments:
Post a Comment