daily gadgets, computers, and electronic news
19/05
2005

Fading Element Effect using CSS

Sponsored Links


In the previous CSS article, we set up a transparent element using CSS through -moz-opacity property (Mozilla’s specific) and opacity property (CSS3). Now let’s try to move another step ahead and learn how to create a cool fade in/out effect using opacity property.

We won’t use -moz-opacity property in this article since the latest version of Firefox (v1.0.4) seems already support opacity property, just like Mozila. So only opacity property will be used.

To create a fading effect, we will need a little help from JavaScript. We’ll use JS’ setTimeout() function and clearTimeout() function to perform the fade animation. All we need to do, in each ‘animation‘ sequence, we either increase (fade-in) or decrease (fade-out) the element’s (image) opacity value until we hit 1 (fade-in) or 0 (fade-out). Each animation is processed through fadeIn() and fadeOut() function (source code below), and the setTimeout() function is used to set when the next fadeIn/fadeOut function will be called. So when those functions is called multiple times sequently, we’ll obtain the fading effect.

Before we get to the source code, let’s see how the effect will looks like:

Fade Out

 

Fade In

 

Move your mouse over the box and the fade effect will begin. To stop or to restore the box to its original condition, mouse your omouse away from the box.

Here’s the source code for the JavaScript portion (copy-paste and save it as fade.js)

[js]var fadeInObj = null;
var fadeOutObj = null;

function fadeIn(obyek,step,delay) {
clearTimeout(fadeOutObj);
var opacity = document.getElementById(obyek).style.opacity;
opacity = opacity * 100;
opacity = opacity + step;
if (opacity > 100) {
opacity = 100;
}
document.getElementById(obyek).style.opacity = opacity / 100;
if (opacity < 100) {
fadeInObj = setTimeout('fadeIn("' + obyek + '",' + step + ',' + delay + ');',delay);
} else {
clearTimeout(fadeInObj);
}
}

function fadeOut(obyek,step,delay) {
clearTimeout(fadeInObj);
var opacity = document.getElementById(obyek).style.opacity;
opacity = opacity * 100;
opacity = opacity - step;
if (opacity < 0) {
opacity = 0;
}
document.getElementById(obyek).style.opacity = opacity / 100;
if (opacity > 0) {
fadeOutObj = setTimeout(’fadeOut(”‘ + obyek + ‘”,’ + step + ‘,’ + delay + ‘);’,delay);
} else {
clearTimeout(fadeOutObj);
}
}[/js]

and this is the HTML portion for the sample above:
[html]

 
 

[/html]

To stop the animation, we call the JS’ clearTimeout() function. However, this function need to know which timeout object should be stop. So we declare the object everytime we call the setTimeout() function, and then pass this object as parameter for the clearTimeout() function (fadeInObj is the object for fade-in effect and fadeOutObj is for the fade-out).

Have fun! :d

Fading Element Effect using CSS is written by cosa and posted under Web & Graphic Design , , , , , . If you like it, you might consider subscribing to our feed, follows us on Twitter, or receive our latest posts via email. Or else, you could also or store it to your favourite social bookmark sites. Further information about this article can be found.
And while you're here, why don't you check out our other articles:

2 Comments »

  1. 1
    Mike says:

    Your script int working for me, perhaps its the reams of spammy banners you have messing everything up.
    Clean up your site man, for gods sake.

  2. 2
    AltaGid says:

    Hello! Help solve the problem.
    Very often try to enter the forum, but says that the password is not correct.
    Regrettably use of remembering. Give like to be?
    Thank you!

Leave a comment