JavaScript
  Home arrow JavaScript arrow Page 5 - Switching on Layers with Scripts
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  
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

Switching on Layers with Scripts
By: Dan Wellman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 6
    2004-06-08

    Table of Contents:
  • Switching on Layers with Scripts
  • Starting with a Basic HTML Page
  • VBScript Subroutines
  • Using an Array
  • JavaScript Version

  • 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


    Switching on Layers with Scripts - JavaScript Version


    (Page 5 of 5 )

    As promised, here is the JavaScript version of the code for those of you that want maximum compatibility. Again, first define the scripting language and add the wrapper:

    <script language="Javascript>
    <!- -  stealth mode on

    This time, you don't need to worry about subroutines, but you will need to create four functions to call when the various tabs are clicked. The first function can be defined using the following code:

    function taboneontop() {
     tab1.style.zIndex="3";
     content1.style.visibility="visible";
     tab2.style.zIndex="2";
     content2.style.visibility="hidden";
     tab3.style.zIndex="1";
     content3.style.visibility="hidden";
     tab4.style.zIndex="0";
     content4.style.visibility="hidden";
     startmessage.style.visibility="hidden";
    }

    It's fairly simple and should be easy to follow; it simply changes the current z-indices of the tabs and un-hides the relevant content div.  Create another three functions, one for each tab that can be clicked:

    function tabtwoontop() {
     tab1.style.zIndex="2";
     content1.style.visibility="hidden";
     tab2.style.zIndex="3";
     content2.style.visibility="visible";
     tab3.style.zIndex="1";
     content3.style.visibility="hidden";
     tab4.style.zIndex="0";
     content4.style.visibility="hidden";
     startmessage.style.visibility="hidden";
    }
    function tabthreeontop() {
     tab1.style.zIndex="2";
     content1.style.visibility="hidden";
     tab2.style.zIndex="1";
     content2.style.visibility="hidden";
     tab3.style.zIndex="3";
     content3.style.visibility="visible";
     tab4.style.zIndex="0";
     content4.style.visibility="hidden";
     startmessage.style.visibility="hidden";
    }
    function tabfourontop() {
     tab1.style.zIndex="2";
     content1.style.visibility="hidden";
     tab2.style.zIndex="1";
     content2.style.visibility="hidden";
     tab3.style.zIndex="0";
     content3.style.visibility="hidden";
     tab4.style.zIndex="3";
     content4.style.visibility="visible";
     startmessage.style.visibility="hidden";
    }

    Now just add the closing comment wrapper and end the script in the normal way:

    -->
    </script>

    All you need to do now is add an onClick event to each of your tab div's to call the function when the object is clicked:

    <div id=tab1 onClick="taboneontop()"  style="position:absolute;left:16;top:116;z-index:3">

    Add a similar handler to each of the other tab div's, but remember to change the function call, i.e. for the tab2 div, add onClick="tabtwoontop()", etc.

    Conclusion

    The goal of this article was to create a script that manipulated first the z-indices of the graphical tabs in order to pull the one that is clicked to the top layer. This is an excellent way of showing the visitor/user which tab they are currently on. The script secondly displays content relating to the tab number in the main body of the graphic. Using these scripts can add a quality of depth to your work that is impossible using pure HTML. Feel free to use these scripts in any way you see fit and to modify them in your own way. There are probably numerous ways in which they can be improved, certainly in the case of the JavaScript file, but I hope it has been a simple introduction to the div tag, the z-index property and DHTML scripting in general.


    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.

     

    JAVASCRIPT ARTICLES

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






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