Ken Ward's Java Script Tutorial ...

Image Hover Buttons or Rollovers

Hover Button The button on the left is a hover button. Let your mouse cross it and the image changes. This can be a neat effect in menus, and it actually helps the user know that he or she is looking at an active image. In this case when you put your mouse over the image it turns yellow, and if you click it you go to the contents page of the Java Script Tutorial.

Here is the code (and as you will see later , you can use this code with few modifications however many hover buttons you have):

<html>

<head>

<script type="text/javascript">

<!-- //

//check if browser is capable, NS3+, IE4+

if (document.images) {

//preload images

//base image

img1N= new Image(108,66);

img1N.src= 'Hovern.gif' ;

//hover or rollover image

img1H= new Image(108,66);

img1H.src= 'Hoverna.gif' ;

function myOn(myImgName) {

//we need to name the image in the BODY

//so we can use its name here

document[myImgName].src=eval(myImgName+ 'H' ).src;

}

function myOut(myImgName) {

document[myImgName].src=eval(myImgName+ 'N' ).src;

}

} //end of if document.images

//-->

</script>

</head>

<body bgcolor="white">

<h1>

my heading

</h1>

<img src="Hovern.gif" name="img1" border="0" width="108" height="66" onmouseover="myOn('img1')" alt="Hover Button" onmouseout="myOut('img1')" />

</body>

</html>

Starting with the image link <img> in the body of the document, we have an image called, "img1". When we move the mouse over the image, the onMouseOver event calls the function, myOn() . We give this function the name of the image, which is img1 , and so the call is myOn('img1') . Later, due to laziness, we will do something else so we don't have to write the image name everytime when we have a lot of image rollovers or hover buttons. Note: image tags require an "alt" attribute, even if it is blank (alt=""). This is for future compatibility with XHTML.

Preloading

We will now take a break from this line, and look at how we preload the images for this one-button rollover. Preloading the images makes the rollover faster because the image is loaded before the document is complete. It also enables us to name our images for the rollover. The preloading code goes like this:

if (document.images) {

//preload images

//base image

img1N= new Image(108,66);

img1N.src= 'Hovern.gif' ;

//hover or rollover image

img1H= new Image(108,66);

img1H.src= 'Hoverna.gif' ;

The first line checks whether the browser is capable (that is, not too old, text-only browser, etc):

if (document.images) {

This means the browser is at least NS3 or IE4. Next we define the images:

//base image

img1N= new Image(108,66);

img1N.src= 'Hovern.gif' ;

The first (and in this case, only) image is named in the HTML as 'img1' and we tell the browser that the 'normal' image is 'img1N' and the rollover or hover is 'img1H'. We tell the browser the size of the image (although this isn't essential) and then tell it the name of the image file. We do the same for the hover image:

//hover or rollover image

img1H= new Image(108,66);

img1H.src= 'Hoverna.gif' ;

The Functions

We have now preloaded the images (er... image) so the browser knows what we are talking about when we come to the functions. The first function is

function myOn(myImgName) {

//we need to name the image in the BODY

//so we can use its name here

document[myImgName].src=eval(myImgName+ 'H' ).src;

}

The function has only one line, so it is very simple. We tell the function the name of the image we are talking about (myImgName), then we set the image in the document. We identify the image in the document using:

document[myImgName].src

and we make the loaded image:

eval(myImgName+ 'H' ).src

When we preloaded the image, we referred to the image name, img1, and we made the normal image 'img1N' and the hover image, 'img1N'. So here we add an 'H' to the name, 'img1', so we get the hover image 'img1H'. We do not merely add an 'H', but use the function eval() to tell the browser that this isn't simply some name (string), but it is an object.

The myOut() function works in exactly the same way as this one.

More than one rollover image

Below there is an example of using three rollovers (you can of course use as many as you like). The code for this is basically the same as before, we haven't changed the functions, we have merely added some more preloaded images. So in the 3 button example, the images are named: 'image1', 'image2', and the unscrupulous 'image3'. The preload code looks like this:

//check if browser is capable, NS3+, IE4+

if (document.images) {

//3 image example

//first image image1

//base image

image1N= new Image(100,30);

image1N.src= 'Homen.gif' ;

//hover or rollover image

image1H= new Image(100,30);

image1H.src= 'Homea.gif' ;

//image2

//base image

image2N= new Image(100,30);

image2N.src= 'alertn.gif' ;

//hover or rollover image

image2H= new Image(100,30);

image2H.src= 'alerta.gif' ;

//image3

//base image

image3N= new Image(100,30);

image3N.src= 'promptn.gif' ;

//hover or rollover image

image3H= new Image(100,30);

image3H.src= 'prompta.gif' ;

//end of 3 image example

The code for the images is basically the same as before. Here is the code for the first image:

< a HREF= "index.html" >< img

SRC= "Homen.gif" ALT= "Java Script Contents" NAME= "image1" BORDER= "0" WIDTH= "100"

HEIGHT= "30"

onMouseOver = "myOn(this.name)"

onMouseOut = "myOut(this.name)" >< /a >

However, instead of writing the image name in everytime I called the functions for each image, I used:

onmouseover = "myOn( this.name )"

onmouseout = "myOut( this.name )"

By using ' this.name ', the browser finds the name of the image itself, and does not have to bother me for it:-)

Java Script Contents Go to the Java Script Tutorial contents page.
Hover Button Back to those infuriating 'Hello' and 'Goodbye' alerts.
Hover Button On to prompting for user input.