Home arrow JavaScript arrow Page 2 - Building a Dynamic Menu with CSS and JavaScript, part 1
JAVASCRIPT

Building a Dynamic Menu with CSS and JavaScript, part 1


Dynamic menus that highlight the link where a website visitor is located can make site navigation a much more pleasant experience. In this article, the first in a series, we will cover the construction process of a dynamic menu, extensively using the DOM methods within user-defined JavaScript objects to help us learn how to build more complex structures.

Author Info:
By: Alejandro Gervasio
Rating: 4 stars4 stars4 stars4 stars4 stars / 67
June 22, 2005
TABLE OF CONTENTS:
  1. · Building a Dynamic Menu with CSS and JavaScript, part 1
  2. · Coding the menu: CSS and HTML into action
  3. · The dynamic factor
  4. · The "menu" JavaScript object

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Building a Dynamic Menu with CSS and JavaScript, part 1 - Coding the menu: CSS and HTML into action
(Page 2 of 4 )

 

Let’s see the underlying code behind the sample menu. First, here are the CSS declarations: 

<style type="text/css">

a.regbutton:link,a.regbutton:visited {

    float: left;

    display: block;

    font: normal 11px "Verdana", Arial, Helvetica, sans-serif;

    color: #000;

    text-align: center;

    text-decoration: none;

    background: #fff url("bg.gif") repeat-x center left;

    width: 80px;

    padding: 10px 0px 9px 10px;

}

a.regbutton:hover {

    float: left;

    display: block;

    font: normal 11px "Verdana", Arial, Helvetica, sans-serif;

    color: #fff;

    text-align: center;

    text-decoration: none;

    background: #fff url("bg.gif") repeat-x center left;

    width: 80px;

    padding: 10px 0px 9px 10px;

}

.leftcorner {

    float: left;

    background: #fff url("lc.gif") no-repeat center center;

    width: 10px;

    padding-top: 8px;

    padding-bottom: 5px;

}

.rightcorner {

    float: left;

    background: #fff url("rc.gif") no-repeat center center;

    width: 10px;

    padding-top: 8px;

    padding-bottom: 5px;

}

#dynmenu {

    padding: 0px;

}

</style>

That’s a quite lengthy CSS listing to be analyzed. In fact, it’s not complicated at all, as you’ll see in a moment. I’ve just defined the classes for styling each link present in the menu, as well as the left and right corners respectively. Doing so, I’ve created the "regbutton" class, to give a button appearance to each one of the links. Please note that I’ve specified the tiny "bg.gif" background image to be horizontally tiled across the button itself, achieving the consistent look.

Then, I’ve declared the "leftcorner" and "rightcorner" classes, obviously for displaying the left and right menu corners. Finally, the "dynmenu" selector is defined, for styling the main menu container within the markup. I'm sure you’ll agree that all of this code is pretty self-explanatory. 

Now, it’s time for the HTML markup listing: 

<div id="dynmenu">

<span class="leftcorner">&nbsp;</span>

<a href="#" class="regbutton" title="About Us">About Us</a>

<a href="#" class="regbutton" title="Products">Products</a>

<a href="#" class="regbutton" title="Services">Services</a>

<a href="#" class="regbutton" title="Links">Links</a>

<a href="#" class="regbutton" title="Contact">Contact</a>

<span class="rightcorner">&nbsp;</span>

</div>

As you can see, the above HTML is very clear. The only tricky parts correspond to the left and right corners. I’ve used the powerful and versatile <span> tag to create the corner effects, with no structural value. There are lots of different ways to achieve this kind of visual effect, using <div> elements, but I’ve decided to keep it that way for simplicity. 

Let’s congratulate ourselves! With a few lines of code, we have a fully functional navigation menu that will work in most browsers. Here’s the full code for the recently developed static menu: 

<html>

<head>

<title>STATIC MENU</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<style type="text/css">

a.regbutton:link,a.regbutton:visited {

    float: left;

    display: block;

    font: normal 11px "Verdana", Arial, Helvetica, sans-serif;

    color: #000;

    text-align: center;

    text-decoration: none;

    background: #fff url("bg.gif") repeat-x center left;

    width: 80px;

    padding: 10px 0px 9px 10px;

}

a.regbutton:hover {

    float: left;

    display: block;

    font: normal 11px "Verdana", Arial, Helvetica, sans-serif;

    color: #fff;

    text-align: center;

    text-decoration: none;

    background: #fff url("bg.gif") repeat-x center left;

    width: 80px;

    padding: 10px 0px 9px 10px;

}

.leftcorner {

    float: left;

    background: #fff url("lc.gif") no-repeat center center;

    width: 10px;

    padding-top: 8px;

    padding-bottom: 5px;

}

.rightcorner {

    float: left;

    background: #fff url("rc.gif") no-repeat center center;

    width: 10px;

    padding-top: 8px;

    padding-bottom: 5px;

}

#dynmenu {

    padding: 0px;

}

</style>

</head>

<body>

<div id="dynmenu">

<span class="leftcorner">&nbsp;</span>

<a href="#" class="regbutton" title="About Us">About Us</a>

<a href="#" class="regbutton" title="Products">Products</a>

<a href="#" class="regbutton" title="Services">Services</a>

<a href="#" class="regbutton" title="Links">Links</a>

<a href="#" class="regbutton" title="Contact">Contact</a>

<span class="rightcorner">&nbsp;</span>

</div>

</body>

</html>

Note that I’ve coded some dead links for the menu. This seems to be a little obvious, but if you’re going to use the example, don’t forget to replace them with the corresponding URL values for your site. 

So far, so good, the menu is working and everything is fine. But did I mention the word "dynamic” in the above lines? Where is the dynamic menu? Keep reading. 


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks
- Dynamic jQuery Styling

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials