JavaScript
  Home arrow JavaScript arrow Page 4 - Controllable Navigation Bars with JavaScri...
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  
Actuate Whitepapers 
Moblin 
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

Controllable Navigation Bars with JavaScript, Part 1
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2005-03-21

    Table of Contents:
  • Controllable Navigation Bars with JavaScript, Part 1
  • Building core functionality
  • The JavaScript"switchBar()"function
  • Complete code listing and background images

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Controllable Navigation Bars with JavaScript, Part 1 - Complete code listing and background images


    (Page 4 of 4 )

     

    As I promised in the previous lines, here’s the complete code for the switcher bar:

     

    <html>

    <head>

    <title>SWITCHER BAR</title>

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

    <script language="javascript">

    switchBar=function(){

        var navbar,content,swbar;

        navbar=document.getElementById('navbar');

        if(!navbar){return;}

        content=document.getElementById('content');

        if(!content){return;}

        swbar=document.createElement('div');

        swbar.id='switchbar';

        swbar.title='hide navbar';

        content.parentNode.insertBefore(swbar,content);

        swbar.onclick=function(){

              if(navbar.style&&swbar.style){

                   if(!navbar.style.display||
                          navbar.style.display=='block'){

                        navbar.style.display='none';

                        swbar.style.backgroundImage=
                         'url(tab_right.gif)';

                        swbar.title='show navbar';

                   }

                   else{

                        navbar.style.display='block';

                        swbar.style.backgroundImage=
                         'url(tab_left.gif)';

                        swbar.title='hide navbar';

                   }

              }

        }

    }

    window.onload=function(){

        if(document.getElementById&&document.
    createElement){

              switchBar();

        }

    }

    </script>

    <style type="text/css">

    body {

        margin: 0;

    }

    #navbar {

        float: left;

        width: 15%;

        height: 600px;

        background: #ccf;

        padding: 10px;

        border-left: 1px solid #000;

        border-bottom: 1px solid #000;

    }

    #switchbar {

        float: left;

        width: 18px;

        height: 600px;

        background: #fff url("tab_left.gif") repeat-y center center;

        padding-top: 10px;

        padding-bottom: 10px;

    }

    #content {

        float: left;

        width: auto;

        padding: 10px;

        border-bottom: 1px solid #000;

    }

    #headlines {

        float: right;

        width: 15%;

        height: 600px;

        background: #ccf;

        padding: 10px;

        border-left: 1px solid #000;

        border-right: 1px solid #000;

        border-bottom: 1px solid #000;

    }

    </style>

    </head>

    <body>

    <div id="navbar">

    <h1>Navbar Section</h1>

    <p>Content goes here...</p>

    </div>

    <div id="headlines">

    <h1>Headline Section</h1>

    <p>Content goes here...</p>

    </div>

    <div id="content">

    <h1>Page Name</h1>

    <h2>Section title</h2>

    <h3>Article title</h3>

    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec molestie.

    Sed aliquam sem ut arcu. Phasellus sollicitudin. Vestibulum condimentum

    facilisis nulla. In hac habitasse platea dictumst. Nulla nonummy. Cras quis

    libero. Cras venenatis. Aliquam posuere lobortis pede. Nullam fringilla

    urna id leo.Praesent aliquet pretium erat. Praesent non odio.Pellentesque

    a magna a mauris vulputate lacinia. Aenean viverra. Class aptent taciti

    sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos.

    Aliquam lacus. Mauris magna eros, semper a, tempor et, rutrum et, tortor.</p>

    </div>

    </body>

    </html>

     

    Since the full code is rather incomplete without showing the background images "tab_left.gif" and "tab_right.gif," respectively, here they are.

     

     

     

     

     

    The images are titled vertically to make the entire switcher bar look like a tabbed panel. As you can easily deduce, the first image is displayed initially when the page is loaded and the sidebar is visible.When the user clicks on the switcher bar, the "navbar" element is completely hidden from view and the second background image takes its place, while the main "content" element will expand itself to the left, giving users a larger visible area for reading page contents more easily. Similarly, if the user clicks again, the "navbar" element is redisplayed  and the main container will be stretched to its original size. Remember that the value "auto" is assigned to the "content" element’s width.

     

    The visual representation of the effect is depicted below:

     

     

     

     

     

    That’s pretty nice, right? With a few lines of JavaScript code and a couple of common images, we have a fully functional, controllable navigation bar. Maybe it’s not going to change your life as a Web designer, but it’s worthy to keep it in the toolbox as a useful resource.

     

    Wrapping up

     

    Hopefully, we’re not done yet. The function in its current form needs some additional touches to really pull it together. We must completely remove the fixed heights initially assigned to the containers in the CSS declarations and still keep the script working for us. In the second part of this series, we’ll be addressing these issues and adding more functionality to the original script. So, until the next time, stay tuned!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       ·  The approach explained in the article might be a good add-on for sites looking...
       · Sorry about that; we had to do that to get the article to fit properly.
       · Hi Terri,No problem about that :-). Thank you for the aclaration.
       · of using that technique for almost 3 years on my site now (onlinetools.org), but I...
       · Hello friend,Thank you for the comment. Definiltively, setting a cookie for...
       · Yes, how would we be able to call the onclick function from another function? Or am...
       · According to your comnents, I thik the best way to keep user preferences is using a...
     

    JAVASCRIPT ARTICLES

    - 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...
    - Bulleted Menu of Links
    - Creating Click Loggers and Basic Markers wit...
    - Adding Pan Controls to Yahoo! Maps


    IBM developerWorks





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