The JavaScript
Here's a break. The JavaScript is self-contained and doesn't require you to do any changing. Hot dog! Copy and paste it from here and cram it between your <HEAD> commands just below the Style Sheet Block.
| <SCRIPT LANGUAGE="javascript"> function highlightButton(s) { if ("INPUT"==event.srcElement.tagName) event.srcElement.className=s } </SCRIPT> |
By the way, I invite you to learn to create your own JavaScripts by reading the the HTML Goodies 30-Step
The JavaScript Primers
.
The Button Code
Now the good stuff, the button code. Let's take a quick look at it and then I'll explain what the parts do.
<FORM NAME=highlight onMouseover="highlightButton('start')" onMouseout="highlightButton('end')"> <INPUT TYPE="button" VALUE="Pass Your Mouse Over Me" onClick="location.href='http://www.htmlgoodies.com'"> </FORM> |
Because of space constraints, I have broken the code into a series of lines, but when you place this on your page, the commands between the < and the > should all go on one line or you might get an error.
Here's What's Happening
- FORM starts the process. This button is created through form commands.
- NAME=highlight connects this button to the JavaScript above. Look again at the script. You'll see the word "highlight" in there, too.
- onMouseover is JavaScript for an event when the mouse passes over. In this case, the browser is told to enact a section of the JavaScript called "highlightButton('start'). Please note the class "start" is called for. Remember that from the Style Sheet Block? That's the blue background and yellow letters pattern.
- onMouseout is the same as above except it is enacted when the mouse leaves the button. Note again that the class "end" is being called for. That's the pink and green pattern.
- INPUT TYPE="button" is basic HTML to create a button.
- VALUE="--" puts the text on the button.
- onClick= is JavaScript to alert the browser to do something when the button is clicked. In this case I have it set to send the browser to the location.href of HTML Goodies. See that above? You can change out where this button points to by simply changing out the URL to Goodies with your own.
- /FORM closes the form commands and ends the button.
Altering and Adding Buttons
As I said above in the Style Sheet Block section, you can add as many different Style Sheet (
Link
) attributes as you'd like to the "start" and "end" classes. Given enough time, I'll bet you can make some pretty awful events.
If you'd like to offer your users more buttons, then you'll need to paste in the button code from above again and again until you have the number of buttons you need. You can, again, change where the button points to by changing out the URL attached to the onClick="location.href" command.
If you'd like to have a different color pattern for each of the buttons, then you'll need to add new classes to the Style Sheet Block. What you name the class doesn't matter, just make sure you have the period in front to let the browser know that what you're offering is a class.
Then, after you've added new classes, you can make each button call for a specific class of colors by changing out the class name in the onMouseOver and onMouseOut commands. Above the classes are ('start') and ('end'). See that? Just change out the words "start" and "end" with your own new class names. Notice there's no period required in the code that makes up the button. That's only in the Style Sheet Block.
You need not do anything to the JavaScript itself. It just acts as a go-between for the button and the Style Sheet Block.
And that brings another tutorial to an end. Remember that even though this is DHTML, you can still place it on your pages for all the world to see. Those with Internet Explorer will get the flip effect, and those with Netscape Navigator will get a basic gray button that works just fine.
Or at least until Netscape Navigator 5.0 comes out. That version is supposed to support DHTML. At least that's what the paperwork says.
Try these other HTML Goodies DHTML examples:
[
So, You Want DHTML, Huh?
]
[
A Video Game
]
[
Power Point Transistions
]
[
Folder Trees
]
[
Link
]
[
Link
]
[
Link
]
Enjoy!
[What I'm Talking About] [The Style Sheet Block]
[The JavaScript] [The Button Code]
[Altering and Adding Buttons]