RevealTrans Filter
First Things First: This is a DHTML event; thus you must be running MSIE4.0+ to see the effect. However, browsers that do not understand DHTML will happily ignore the commands without throwing errors, so feel free to use them at will.
Do you want a cool effect? Dig this! Figure 3.4 shows what this tutorial teaches you. Notice that the button says to click it for the image to go away. I clicked it and then captured the image as the image was disappearing. Inside of two seconds, it was gone. Really!
Figure 3.4 No eggs, thanks...
Better yet, you can also set it so that the image comes back.
What you're seeing there is the revealTrans() filter at work. Two separate JavaScripts are using the filter to make a SPAN disappear and then reappear. I'll show you the first one and then explain the second one, but only quickly. (It's just like the first one only backward.)
Make It Disappear
As I said previously, the image, "eggs.gif", itself doesn't disappear. The SPAN surrounding the image disappears. The image just goes with it. So, let's start with the SPAN and its image. The code looks like this:
<INPUT TYPE="button" VALUE="Go Away!" onClick="go();"> <SPAN ID=Egg1 Style="Visibility:visible; Filter:revealTrans(duration=2);width:179;height:110"> <IMG SRC="eggs.gif"> </SPAN>
The first line is a form button with the text "Go Away!" The button is there to act as a trigger for the function "go()". When clicked, the function fires.
The code is a basic SPAN surrounding the image flag displaying the eggs.gif. A NAME, Egg1, is given to the SPAN. That links it to the next JavaScript covered in the next paragraph. Also, inside the SPAN are some Style Sheet commands and the filter. Note that the visibility of the SPAN is set to "visible". That changes in the next script. Then comes the Filter:revealTrans(duration=2). You can probably guess that the 2 means two seconds for the effect. Then the height and width of the image are given so that the SPAN fits it perfectly.
Okay. Got the SPAN? Good. Now here is the script that does the dirty work:
<SCRIPT LANGUAGE="javascript"> function go() { Egg1.filters[0].Apply(); if (Egg1.style.visibility == "visible") { Egg1.style.visibility = "hidden"; Egg1.filters.revealTrans.transition=12; } else { Egg1.style.visibility = "visible"; Egg1.filters[0].transition=12; } Egg1.filters[0].Play(); } </SCRIPT>
The script is pretty straightforward. When the function go() is triggered, the filter in Egg1 is applied. Remember that Egg1 is the SPAN. We gave it that name in the first script.
Then, if Egg1 is visible, set its value to "hidden" using transition number 12. Otherwise, make the SPAN Egg1 visible by using transition number 12.
Then play the transition! There's nothing to it!
Transition Numbers
There are 22 different transitions to choose from. There is also transition 23, which chooses a number at random. I just happen to like 12.
And no, you do not need both transitions set to 12. It can be two different numbers. Table 3.1 describes the magic 23 numbers.
Table 3.1 Transition Effects
Number |
What Happens |
1 |
Reveals from inside out |
2 |
Scrolls in from outer parts |
3 |
Scrolls out from the center |
4 |
Scrolls up from the button |
5 |
Scrolls down from the top |
6 |
Scrolls left to the right |
7 |
Scrolls right to the left |
8 |
Displays vertical blinds left to right |
9 |
Displays horizontal blinds top to bottom |
10 |
Displays a combination of 8 and 9 |
11 |
Looks a lot like 8 |
12 |
Comes in, in pixels |
13 |
Scrolls in from outer parts |
14 |
Scrolls out from the center |
15 |
Closes from both the top and bottom |
16 |
Opens from center to top and bottom |
17 |
Displays a diagonal roll from right to left |
18 |
Displays a different angle diagonal roll right to left |
19 |
Displays number 17 the other way |
20 |
Displays number 18 the other way |
21 |
Displays random horizontal lines |
22 |
Displays random vertical lines |
23 |
Displays completely random |
After Transition 23, the cycle of effects appears to start over. Any one will work just fine. Some are just more interesting than others.
Make It Go Away
There are really two things to discuss when it comes to reversing the effect: simply setting the current script to go the opposite way and putting a second revealTrans() on a page.
If all you want to do is make the script and SPAN previously noted to go from invisible to visible, it's simple. Everywhere you see the word "visible", you change it to "hidden"; and everywhere you see the word "hidden", you change it to "visible". Don't forget to change the instance in the SPAN as well.
But what if you want to put a second revealTrans() on the same page? You can, but you need to do two things.
First, you need to set a new function name in the script. I chose goAway() for my second function name. Then you need to update that name in the Form Button onClick Event Handler.
Then there's the NAME= in the SPAN. Remember how we named the SPAN in the original script Egg1? Well, that NAME connected the SPAN and the JavaScript. This means that if you put another revealTrans() on a page, the first name is dead and cannot be used by anything else. Thus, you have to change the name of the SPAN and each time that name appears in the script.
Here's a hint to do it quickly: Copy the script and previous SPAN on to a separate text editor, like WordPad or SimpleText. Then, choose Replace from the Edit menu. Type in the current name of the SPAN and then what you would like the new name to be and choose to Replace All. Bingo! It's done.
Now you can copy the new script and SPAN and paste it wherever you want it. No sweat! That's what I had to do here. I went with the name Egg2. Clever, huh?
The following code is the second script and SPAN from before. It has a new function name, a new NAME for the SPANwhich has also been changed throughout the scriptand is set to go in the opposite direction of the first revealTrans(). The transitions are still set to 12, though. I really do like that number:
<SCRIPT LANGUAGE="javascript"> function goAgain() { Egg2.filters[0].Apply(); if (Egg2.style.visibility == "hidden") { Egg2.style.visibility = "visible"; Egg2.filters.revealTrans.transition=12; } else { Egg2.style.visibility = "hidden"; Egg2.filters[0].transition=12; } Egg2.filters[0].Play(); } </SCRIPT> <INPUT TYPE=button VALUE="Lemme See It" onClick="goAgain();"> <SPAN ID=Egg2 Style="Visibility:hidden; Filter:revealTrans(duration=2);width:179;height:110"> <IMG SRC="eggs.gif"> </SPAN>
No Button
As you can probably tell, using a button to start the effect is not very useful. Just remember that as long as the effect can be surrounded by a SPAN and an Event Handler is used to trigger the function, this can be triggered any number of ways. An onLoad Event Handler can trigger the effect when the page loads. You could also set the effect to trigger using an onMouseOver as illustrated in Figure 3.5.
Figure 3.5 See the text "Thank You!" coming in?
By the way, I got the effect to occur in less than a second by setting the duration to .25. Faster than that seemed to kill the effect.