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  
Mobile Linux 
App Generation ROI 
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, concluded
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 19
    2005-06-29

    Table of Contents:
  • Building a Dynamic Menu with CSS and Javascript, concluded
  • Adventures with the Menu Class and Button Class
  • The Style Object
  • Another Class on the Road: Using the Menu Class

  • 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, concluded - The Style Object


    (Page 3 of 4 )

    The style object is a proprietary object introduced in Internet Explorer 4.0 and can be used to retrieve any in-line styles applied to an element object by using the " style" attribute. However it doesn't retrieve any styling set in embedded or externally linked style sheets. Also, it’s possible to dynamically assign style properties to any page element. This last ability is what I’m using here to style the menu buttons. In fact, the object’s extreme popularity made it widely available for most of 6th generation browsers onward. So, users still using old browsers won’t be able to see anything referencing this object. The section that styles the button <div> element is the following:

    // style button <div>
    button.style.position='absolute';
    button.style.left=x+'px';
    button.style.top=y+'px';
    button.style.width=w+'px';
    button.style.height=h+'px';
    button.style.padding='3px 0px 3px 0px';
    button.style.textAlign='center';
    button.style.borderColor='#000';
    button.style.borderStyle='solid';
    button.style.borderWidth='1px';

    Now, with that familiar feeling about the style object, it should be clear enough how the class applies styles to either the button containing <div> and the link itself. For the button element, I’ve decided to specify a background color and a solid border; as well as padding values and text align properties. As you can appreciate, it’s not very different from styling with regular CSS rules.

    In order to display the link in a different way from the others, the class checks out if the link value passed as a parameter matches the current page location. If this is true, the corresponding button is displayed with an orange background color, this way highlighting it from the rest. The process to simply resolve the current page location and accordingly highlight the proper button is done with the lines listed below:

    // resolve active page and display button differently
    loc.indexOf(url)!=-1?button.style.backgroundColor=
      '#fc0':button.style.backgroundColor='#9cf';

    Now, it’s time to give a style the <a> element, and assign the "href" and "title" properties to it:

    // style link
    lnk.style.display='block';
    lnk.style.color='#000';
    lnk.style.fontFamily='Verdana';
    lnk.style.fontSize='11px';
    lnk.style.textDecoration='none';
    // assign attributes to link
    lnk.href=url;
    lnk.title=text;

    For achieving a similar effect to the "hover" state in links, the class assigns different colors for the link text by calling different functions for the "onmouseover" and "onmouseout" event handlers respectively. As you can see, this is done with the following code:

    // change link color on mouseover
    lnk.onmouseover=function(){
              this.style.color='#fff';
    }
    // reset link color on mouseout
    lnk.onmouseout=function(){
              this.style.color='#000';
    }

    The final task to be performed by the class is to add the text to be displayed in the link and insert this element inside the button container, finally returning the button object itself. If you think about it, this is extremely handy, since the constructor returns a completely styled and functional button, ready to be appended as an element of the general menu. That looks pretty good for adding any number of different buttons. Here’s where the class shows up its real power.

    Well, at this point I guess you’re feeling like everything in the world is an object to be programmed (perhaps including wives and girlfriends). But please, let’s not get our noses into weird affairs and come back to the dynamic menu. Let’s take a good look at the "Menu" class.

    More JavaScript Articles
    More By Alejandro Gervasio


       · This second part of the series uses JavaScript user-defined objects and the DOM to...
       · Hi all,I just wanted suggest that it could be very attractive for readers to see...
       · Hello Jordi,Thank you for the comments about the article. Unfortunately, I...
       · Hi Alejandro,I found your articles very useful. It would be very nice if you could...
       · Hi Jorge,Thanks for the kinds comments on the articles. Below it's the list for...
     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - 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






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