Home arrow DHTML arrow Page 4 - Cross Fading Navigation with DHTML
DHTML

Cross Fading Navigation with DHTML


Need to build smooth and attractive fading techniques into your web site using nothing more than script? This article will explain a proven cross-browser method, with ready-to-use code. If you think you don’t need this, you will conclude otherwise after reading the article!

Author Info:
By: Justin Cook
Rating: 5 stars5 stars5 stars5 stars5 stars / 10
March 10, 2004
TABLE OF CONTENTS:
  1. · Cross Fading Navigation with DHTML
  2. · Putting our Items in Place
  3. · Into the Code
  4. · The Fading Function
  5. · In Retrospect...

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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().


blog comments powered by Disqus
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...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials