DHTML
  Home arrow DHTML arrow Page 4 - 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  
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? 
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


    Cross Fading Navigation with DHTML - The Fading Function


    (Page 4 of 5 )


    //=============================
    function fader( objID, dir ) {
      var fade_index;

    The first thing we need to do is make sure we’re not trying to fading in two directions at once. This is because the fader works by adding or subtracting from the objects opacity index until it reaches the limit. If we were going in two directions at once, we’d be adding and subtracting, which is basically like trying to walk forward with one leg, and backward with the other!


    if inUse != '' && inUse != dir ) {
      
    return;
    }

    Next we just make sure they’ve passed the DHTML test, and let them see the fades. As browsers progress, we could add them to the list here. But until then, they just see the active navigation pop into view.


    if ie5 || moz ) {

    Let’s just set the direction placeholder to prevent the aforementioned situation.


    inUse dir;

    Now let’s get the current opacity we’re working with. Notice for Explorer we get the opacity directly. But with Mozilla we must multiply by 100, as the index is a floating point number between 0 and 1.


    ifie5 ) {
      fade_index 
    getObjectobjID ).filters.alpha.opacity;
    }
    if
    moz ) {
      fade_index 
    getObjectobjID ).style.MozOpacity 100
    }

    Now we set the next opacity index, and the limit we want it to go to, based on the direction we’re fading in. We will use JavaScript’s ternary operator because it’s so nice and short. You could also use an if/else statement.

    Currently I go up and down by 5’s, but you can modify this to get faster or slower fades.


    fade_index  dir == 'in' fade_index fade_index 5;
    index_limit 
    dir == 'in' 100 0;

    Now we do the actual fade. Remember that for Mozilla we have to return to the floating point number format, so divide by 100.


    ifie5 ) {
      getObject
    objID ).filters.alpha.opacity fade_index;
    }
    if
    moz ) {
      getObject
    objID ).style.MozOpacity fade_index 100
    }

    Now we set the time in milliseconds that we want to wait to fade more. You could also change this number to get longer/shorter fades. Right now it’s 20 milliseconds.

    code

    Last, but of course not least, we clear the placeholder and timer once we’ve reached the fade limit. At this point, we have a wonderfully faded object, so we can get on with our life.

    NOTE There should be no carriage returns in your code for this portion.


      if( ( dir == 'in' && fade_index >= index_limit ) || ( dir == 'out' && fade_index <= index_limit ) ) {
        clearTimeout
    goIn );
        inUse 
    '';
        
    }
      
    }
    }

    That’s it, really not too complicated; you just have to build in fail-safes so that we don’t end up trying to fade 3 objects in two different directions at once while patting your head and rubbing your stomach. This could create infinite recursion, and cause your CPU to run away screaming. Trust me, I learned the hard way.

    If you copy and paste the code into your pages, you could get away with just adding your own navigation items, and changing the maxInt variable in swapNav().

    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 3 hosted by Hostway