JavaScript
  Home arrow JavaScript arrow Page 3 - Building a Dynamic Menu with CSS and JavaS...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Building a Dynamic Menu with CSS and JavaScript, part 1
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 39
    2005-06-22

    Table of Contents:
  • Building a Dynamic Menu with CSS and JavaScript, part 1
  • Coding the menu: CSS and HTML into action
  • The dynamic factor
  • The "menu" JavaScript object

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building a Dynamic Menu with CSS and JavaScript, part 1 - The dynamic factor


    (Page 3 of 4 )

      

    As I stated at the beginning of this article, the objective is to build a dynamic menu that identifies the active page, and highlights the corresponding link, visually indicating to the user what page he/she is currently visiting. To do that, I’ll use the above static menu as the base structure, adding the dynamic capabilities with JavaScript.

    What’s more, since the menu might be quickly implemented in several Web projects, I’m going to encapsulate the whole code as a new JavaScript object, sacrificing a bit more of computing client resources, but gaining in maintenance and reusability. Sounds good, right? Let’s get a little more dynamic and build up the menu. 

    First, let’s see the full code for the CSS rules, which strongly relies on the previous static example. The listing is the following: 

    <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;

    }

    a.activebutton:link,a.activebutton:visited {

        float: left;

        display: block;

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

        color: #000;

        text-align: center;

        text-decoration: none;

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

        width: 80px;

        padding: 10px 0px 9px 10px;

    }

    a.activebutton:hover {

        float: left;

        display: block;

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

        color: #fff;

        text-align: center;

        text-decoration: none;

        background: #fff url("bgact.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;

    }

    h1 {

        clear: left;

    }

    </style>

    If you take a look at the above CSS rules, you’ll probably realize that they're pretty similar to those used with the first example. As you can appreciate, I’ve kept the original "regbutton", "leftcorner" and "rightcorner" classes, to give the same look to the menu. The only significant difference resides in the additional "activebutton" pseudo class, which is defined to be applied to the link corresponding to the active page, displaying it differently from the other links.

    In order to highlight the active button, I’ve used the "bgact.gif" background image that is similarly tiled horizontally, changing the original button color to an orange version. In this way it indicates that the link corresponds to the active page. As usual, feel free to pick up any background image that you think is convenient for achieving the visual effect. It’s also perfectly possible to get rid of all of the images and vary only background and font colors. Just grasp that inspirational moment and choose the best model for your personal preferences. 

    Finally, I’ve styled a <h1> header tag, for clearing any element rendered after the menu itself. This is only to be used in the example. If you’re going to use the menu in your own applications, just add the styles that you really need. 

    At this point, I think that the CSS styles have been easily established to give you a good understanding. Now, let’s take a deep breath and remember the concept proposed before: building a dynamic menu that is completely defined as a new JavaScript object. What are we waiting for? Let’s make JavaScript work for us! 

    More JavaScript Articles
    More By Alejandro Gervasio


       · The article shows how to define a simple dynamic navigational menu going from a...
       · Hello Sir,First of all congrates that u built this useful menu .I created this...
       · Thank you for the kind comments on my article. With reference to your question, the...
     

    JAVASCRIPT ARTICLES

    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway