mtWeb  Home > JavaScript > Text Fading with Color SelectionSitemap  Search

Text Fading with Color Selection

Posted by martin on 1 Aug 2001, last updated on 14 Sep 2002.

Example and explanation of JavaScript fading text in selectable color. Fade speed can also be customized.

Compatibility

  • This script requires a DHTML capable browser that means JavaScript and CSS support is required
  • It works in Opera 5, all Gecko based browsers (Netscape 6, Mozilla, Galeon, SkipStone), and IE 5
  • If you want to make this script NN/IE 4 compatible you'll have to change the div to a (these browsers do not support event handling for all elements)

Example of the script

Just point your mouse over the text to see it fading in and then back out when you move it away.

Some example text that will fade in and out

Select color to fade:

Explanation

Styles

First you may apply a simple style to the element like this one for example:

#fader {
	text-align : center;
	color : rgb(0, 0, 0);
}

The HTML code

<div id="fader" onmouseover="fadeIn(this)"
	onmouseout="fadeOut(this)">
Some example text that will fade in and out
</div>

<p>Select color to fade:</p>

<ul>
	<li><a href="#" title="select red"
		onclick="setColor(30, 0, 0);return false"
		style="color : red">red</a></li>
	<li><a href="#" title="select green"
		onclick="setColor(0, 30, 0);return false"
		style="color : green">green</a></li>
	<li><a href="#" title="select blue"
		onclick="setColor(0, 0, 30);return false"
		style="color : blue">blue</a></li>
	<li><a href="#" title="select yellow"
		onclick="setColor(30, 30, 0);return false"
		style="color : yellow">yellow</a></li>
	<li><a href="#" title="select aqua"
		onclick="setColor(0, 30, 30);return false"
		style="color : aqua">aqua</a></li>
	<li><a href="#" title="select fuchsia"
		onclick="setColor(30, 0, 30);return false"
		style="color : fuchsia">fuchsia</a></li>
</ul>

The id is used to identify the element which we want to change, and we use its onmouseover and onmouseout event handlers.

Then we create an unordered list which is used to set the color which will be faded in/out, I used the onclick handler to invoke a JavaScript function which does the trick.

The JavaScript itself

Define some global variables, r, g, b represent the current red, green and blue components of the element color. chR, chG, chB equal the step with which the colors will fade and fader is the object which we handle, timer a reference to a previous set timeout which we should cancel before invoking a new one.

var r = g = b = 0;
var chR = 30;
var chG = chB = 0;
var fader = null;
var timer = null;

This function handles the fading color change by the unordered list of choices. It sets the global variables to the values passed to the function, because you cannot set global variables within an event handler (well at least not in all browsers). The last line is needed because colors often get out of range and it might cause your script to stop reinvoking itself.

function setColor(R, G, B) {
	chR = R;
	chG = G;
	chB = B;
	r = g = b = 0;
}

A wrapper function that calls fadeReal with positive parameters to start fading in. The first thing to do is check whether the browser has created the style object for the element we want to handle( the only browser that supports CSS dynamic changing but does not create this object is NN 4 but anyway it does not support onmouseover and onmouseout events for the div element so it discard it).

The next thing we have to do is assign the value of the element reference to a global variable (so that our code can be easily reused in other scripts). We check if there is a timer already set and clear it if this is the case. Last but not least call the function that does the hard work.

function fadeIn(obj) {
	if ( obj.style ) {
		fader = obj;
		if (timer)
			clearTimeout(timer);
		fadeReal(chR, chG,chB)
	}
}

This is the other wrapper function that calls fadeReal but this time with negative parameters which causes fading out.

function fadeOut(obj) {
	if ( obj.style ) {
		fader = obj;
		if (timer)
			clearTimeout(timer);
		fadeReal(-chR,-chG,-chB)
	}
}

This is the function that does all the work. First three lines update the global variables used to store the current red, green and blue components of the color of the element. Then a simple check for out of range color values if all goes well we set the element color to the new one and set a timer to invoke the same function with the same parameters in 100ms (that's 1/10 of a second).

function fadeReal(chR, chG, chB) {
    r += chR;
    g += chG;
    b += chB;

    if ( ( r >= 0 ) && ( r < 256 ) &&
    ( g >= 0 ) && ( g < 256 ) &&
    ( b >= 0 ) && ( b < 256 ) ) {
        fader.style.color =
            "rgb(" + r + "," + g + "," + b + ")";
        timer = setTimeout
            ("fadeReal(" + chR + "," + chG + "," + chB + ")", 100);
    }
}

If you want to copy the script from the HTML output you may have to concatenate some lines which were separated for better visual look( you don't like to scroll to be able to read the text and script do you) but you can save yourself lots of time and efforts by downloading all of it directly.

Comments

help
by (DLB6834@aol.com) on 23 Sep 2002 12:53am GMT

do you download text fader how do you download it?

The script only
by martin on 23 Sep 2002 2:48pm GMT

You can download the JavaScript only, the HTML and CSS code needed is not much so you can just copy & paste it from the page.

yo
by () on 30 Jan 2003 9:46pm GMT

hi ahslye i hopethis text fader is working!!!

Get real
by () on 7 Apr 2003 7:17pm GMT

Ha ha ha .... real funny joke

help me
by BrIt () on 7 Apr 2003 10:20pm GMT

yeah i need help on how to download the fadin color but i only want blue purple and red to fade

help me...
by Mel (summergrl315@aol.com) on 22 Apr 2003 5:03pm GMT

i dont get this at all

Nothing works
by Daz () on 25 Apr 2003 3:37am GMT

Yeap the script does not work in IE6, Opera7, NS7 and NS4.75 and the download gives me a binary file.

Re: Nothing works
by martin on 4 May 2003 3:46pm GMT

The download gives you a binary because your browser lies that it supports gzip encoding, just save the file as something.gz and extract it.

ever heard of getelmementbyid?
by neo () on 26 May 2003 11:53pm GMT

nothing works :(

nice site design and content though, too bad I'm leaving now

Doesnt work
by () on 10 Jun 2003 7:42pm GMT

ummm none of wut u said would work i have tryed and tryed it jus wont do wut it is suppose to do an di am getting fustrated

Yep
by aom () on 14 Aug 2003 9:35pm GMT

Yep, it's not working on me either. I am also frusrated trying to seek a working code for whole afternoon.

THIS SUCKS!
by THIS SUCKS! () on 29 Aug 2003 4:03am GMT

THIS SUCKS! AND NOTHING ON HERE MAKES SENSE, OR BETTER YET...WORKS!

The intelligent posts to this page I find very ins
by Tan Benason (tann@blowhole.com) on 30 Aug 2003 6:19am GMT

Like the above post. He is yelling and he has pins in his eyes.

Hi
by Aaron () on 30 Aug 2003 3:02pm GMT

If anyone knows where i can find pictures of naked male bodybuilders please post the site here thanks

RE: Hi Aaron!
by berserk () on 1 Sep 2003 7:40am GMT

Aaron, you just have to undress and find a mirror! :)

Post a shot here, so we all can enjoy!

Code Fix
by Ryan () on 17 Nov 2003 3:34am GMT

This code does infact work just fine with a minor fix. In the fadeIn and fadeOut routines, capitalize the R in fadeReal. Try figuring out why it doesn't work rather than complaining that it sucks.

wat evere
by () on 5 Jan 2004 11:32pm GMT

i had it didnt work

Re: Code Fix
by martin on 7 Jan 2004 11:19am GMT

But it's already with a capital R, or do these guys actually write the code themselves rather than copy/paste?

:-)
by Kayla (KaylaBelle06@aol.com) on 20 Feb 2004 4:39am GMT

I just wanna know how to fade text! In english! Not everyone unerstands computer language.

Yeah ...your all stupid
by Ryan (ryanday_4@msn.com) on 19 Mar 2004 10:24pm GMT

I just wanna know how to fade text! In english! Not everyone unerstands computer language.

yea...your an idoit computer language is how you do every thing and thats *HTML for short it works for me but i had to twick it ok retards?