Where to use 'em
Rollover images are great whenever you want to make it obvious that the user should click on them (for example, buttons and menus).
Where not to use 'em
In the bath.
OK wise guy - how's it done?
Well it all hangs around JavaScript's Image class. Let's look at how we did the rollover button above, to help us understand this class.
The first thing we need to do is create two images - one for the "normal" button, and one for the "active" (rollover) button. We do this with the new Image() constructor, which creates the two Image objects:
Code: Select all
eg_on = new Image ( );
eg_off = new Image ( );
Code: Select all
eg_on.src = "images/eg_on.gif";
eg_off.src = "images/eg_off.gif";
Our example on this page only has one rollover button; however, often you'll want several buttons on one page, as in a menu, for example. To make this easy to do, we've created a couple of functions to handle the actual image changing. That way, you can just call the same functions for each of your buttons, without having to duplicate lots of code.
Here are those functions, called button_on() and button_off():
Code: Select all
function button_on ( imgId )
{
if ( document.images )
{
butOn = eval ( imgId + "_on.src" );
document.getElementById(imgId).src = butOn;
}
}
function button_off ( imgId )
{
if ( document.images )
{
butOff = eval ( imgId + "_off.src" );
document.getElementById(imgId).src = butOff;
}
}
The two functions are nearly identical. Both take the Image object specified by imgId, which is the ID we will give the button in the img tag (in this case "eg"), and replace its source with the "_off" or the "_on" image's source, as appropriate.
I'm confused! What are all these different images?
There are three Image objects at work here:
eg | The actual image in the HTML page (see below); initially it points to "images/eg_off.gif" |
eg_off | The "off" version of the image (which also points to "images/eg_off.gif") |
eg_on | The "on", or rolled-over, version of the image, which points to "images/eg_on.gif" |
Does the browser support rollovers?
Nearly all modern browsers support rollover images, but it's wise to check this before trying to manipulate images. If we don't do a check, then browsers that can't do rollovers will throw an error.
We make this check in the code above using the line:
Code: Select all
if ( document.images )
The HTML bit
The last piece of the puzzle is to make the call to our JavaScript functions from within the HTML itself. To do this, we need to have an active link (an <a> tag) around our rollover button. As our button doesn't need to link anywhere, we link it to # (an empty anchor).
Code: Select all
<a href="#"
onmouseout="button_off('eg'); return true"
onmouseover="button_on('eg'); return true"><img
src="images/eg_off.gif" style="width: 64px; height: 64px;
border: none;" alt="Button" id="eg"/></a>
Last - but by no means least - the button image is given an ID using the attribute id="eg". This ID is very important, as it is used by the button_on() and button_off() functions to determine the right image object to change (in our case, "eg"). When using many rollovers in one page, make sure you use a different image ID for each button!
Go for it!
Now you should be able to take the above code and use it to make your own rollover images. You can make as many rollovers as you like in one page - just remember to give each one a unique id, and to call the functions button_off() and button_on() with that ID.