DHTML
  Home arrow DHTML arrow Page 3 - Cross Fading Navigation with DHTML
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? 
DHTML

Cross Fading Navigation with DHTML
By: Justin Cook
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2004-03-10

    Table of Contents:
  • Cross Fading Navigation with DHTML
  • Putting our Items in Place
  • Into the Code
  • The Fading Function
  • In Retrospect...

  • 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

    Cross Fading Navigation with DHTML - Into the Code


    (Page 3 of 5 )

    Ok, so now let’s get into the code, we’ll take it step by step. To start off with, we need to make sure we’re using browsers capable of handling the transitions, and a generic function to find objects.


    //=========================================
    // browser detection
    var ie5   = ( document.getElementById && document.all ); 
    var moz   = ( document.getElementById &&! document.all );
    var opera    = ( navigator.userAgent.indexOf( 'Opera' ) != -1 );
    if ( opera ) ie5 = false;
     
    //=========================================
    // generic object finder, modify it however you want
    function getObject( obj ) {
      var strObj
      if ( document.all ) {
        strObj = document.all.item( obj );
      } else if ( document.getElementById ) {
        strObj = document.getElementById( obj );
      }
      return strObj;
    }

    Both Internet Explorer and Mozilla are good browser candidates. Opera however, will try to qualify at first pass by meeting the IE5 requirements. Therefore we have to do some string checking, and make sure Opera is identified correctly. For this browser and other non-DHTML friendly browsers, the menus will just pop in and out of existence, without fading.

    Visual Manipulation

    Now let’s get into the actual visual manipulation. The most efficient route to take, is to fade out the entire navRowSpan, make sure all sub-items are not displayed, display only the desired sub-items, then fade the whole navRowSpan back in. To do this, it’s best to use two functions: one to handle swapping the displayed items, one for the fading.

    Let’s start with the first function, and declare the global variables that will be shared between the two:


    //=======================================
    // instantiate global variables
    var wait, goIn, intRightNow, inUse;
     
    // first function, swaps the active sub items
    function swapNav( intNavSpan ) {
     

     
    if ( getObject'nav' intNavSpan ).style.display == 'inline' )
      
    return;

    The first thing we need to do is prevent the swapping of the same items simultaneously, or even trying to fade in an object that’s already active (full opacity).

    But what happens if a user moves their mouse over another item in the middle of the fade? The best way to handle it is to stop fading in, fade it out quickly, and start on the new one.


    if intNavSpan != intRightNow ) {
      
    // clear timeouts
      if ( typeof wait != 'undefined' ) { clearTimeout( wait ); }
      if ( typeof goIn != 'undefined' ) { clearTimeout( goIn ); }

     
      
    intRightNow intNavSpan;  // set ‘current item’ placeholder
      inUse = '';
      swapNav( intNavSpan );
      return;
    }

    Of course we do need to wait until the fader is completely finished it’s work before we start on the next one:


    if inUse != '' ) {
      wait 
    setTimeout'swapNav( ' intNavSpan ' )'20 );
      
    return;
    }

    Here’s where we make the call to fade the whole thing out.

    We wait until that process is finished, and then loop through the sub items, and make sure they’re all hidden. For efficiency’s sake, we define the maxInt, the total number of sub item containers, so that we can quickly locate them all.

    NOTE  The items do not start with an index of ‘0’ in my example, so maxInt truly reflects the total.


    if inUse != '' ) {
      wait 
    setTimeout'swapNav( ' intNavSpan ' )'20 );
      
    return;
    }
     
    var 
    maxInt 3;
    for 
    ( var 1<= maxInti++ ) {
      getObject
    'nav' ).style.display 'none';
    }

    Now we simply display the active sub-item container, and fade the block back in. 


      getObject'nav' intNavSpan ).style.display 'inline';
      
    fader'navRowSub''in' );
    }

    So now let’s get into the actual meat, what we’ve all come here to see: the fancy fading function.

    More DHTML Articles
    More By Justin Cook


     

    DHTML ARTICLES

    - Text-Justify, Volume, and Other Style Sheet ...
    - Ruby-Position, Size, and Other Style Sheet P...
    - Padding, Pages, and More Style Sheet Propert...
    - Marks, Orphans, and More Style Sheet Propert...
    - Layouts, Margins, and Other Style Sheet Prop...
    - Floats, Fonts, and Other Style Sheet Propert...
    - Color, Filters, and Other Style Sheet Proper...
    - Borders and More with Style Sheets
    - Learning Style Sheet Properties
    - Style Sheet Property Reference
    - Completing a Noisy Image Application
    - An Object-Based Approach to Building Noisy I...
    - A Basic Method for Building Noisy Images
    - Adding More Features to Sliders with the Scr...
    - Using Sliders with the Scriptaculous Framewo...







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