Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > PHP/MySQL/Scripting

How to Create Multiple Rollovers with JavaScript

📄 Contents

  1. Multiple Links Changing a Single Rollover
  2. Working with Multiple Rollovers
  • Print
  • + Share This
Need to incorporate more than one rollover on a web page? In this article, Peachpit Press authors Tom Negrino and Dori Smith show you how it's done by adding just a little extra JavaScript to your original rollover script.
This article is excerpted from From Chapter 3 of Javascript for the World Wide Web: Visual QuickStart Guide, 4th Edition, by Tom Negrino and Dori Smith.
From the author of

It's unlikely that you're going to want to put just one rollover on a page. In order to use more than one, you need to add just a little extra code, as in Script 1. The first rollover on the page is used in Figure 1, and the second one can be seen in Figure 2.

Figure 1 You can also put multiple rollovers on the same page.

Figure 2 Hovering over the second rollover.

To put multiple rollovers on a page:

  1. button1Red = new Image
    button1Blue = new Image
    button2Red = new Image
    button2Blue = new Image

    In this example, we need to create four new image objects. In general, you'll need to create two new image objects per rollover. So in this step, you're setting up variables for two rollovers.

  2. button1Red.src = "images/redButton1.gif"
    button1Blue.src = "images/blueButton1.gif"
    button2Red.src = "images/redButton2.gif"
    button2Blue.src = "images/blueButton2.gif"
  3. Here, we set the src attribute of each new image object to the graphic file with which it is associated.

  4. button1Red = ""
    button1Blue = ""
    button2Red = ""
    button2Blue = ""
  5. To prevent errors in older, dumber browsers, we again set the dummy variables (four, this time) to the empty string.

  6. document.button1 = ""
    document.button2 = ""
  7. As there are two rollovers this time instead of one, we need to have two dummy document variables, one for each rollove


Note that each image object and each IMG tag has a unique name. If you copy and paste rollover code from one section of your page to another, make sure that you set the new version to have a unique name. Otherwise, JavaScript gets confused when doing the rollover and doesn't know what image to put where.

Multiple Links Changing a Single Rollover

Up to now, you've seen how mousing over a single image (or actually, the link associated with that image) can trigger a rollover effect. But you can also have several different images that trigger a rollover. This can be very useful when you have several images that you want to annotate. Rolling over each of the images would make the description of that image appear. In this example, we've done just this with images of three of Leonardo daVinci's inventions. As you roll over each image, the description of that image appears in a text box. The description itself is another image. Actually, it's three images, one for each of the three inventions. Figure 3 shows Script 2 in action. As with most of the scripts in this book, it builds on previous examples, so we'll just explain the new concepts.

Figure 3 This page has three images, a flying machine, a tank, and a helicopter. When you roll over an image, its description appears under Leonardo's face.

To make multiple links change a single rollover:

  1. <A HREF="flyPage.html" onMouseover= "document.textField.src=flyText.src" 
    onMouseout= "document.textField.src=bgText.src"> <IMG SRC="images/flyer.gif" 
    WIDTH="293" HEIGHT="165" BORDER="0" VSPACE="20" ALT="Flying Machine"></A>

    This line handles the top invention, the flying machine. The onMouseover event handler puts the contents of flyText.src into document.textField.src (textField is the name of the area that has the description of the invention), and when the mouse is not over the image, replaces the description with a blank background image.

  2. <A HREF="tankPage.html" onMouseover=
    "document.textField.src=tankText.src" onMouseout=
  3. The link for the second image on the page (the tank) works in much the same way as the link for the first image. All that's different is that it sets document.textField.src to tankText.src.

  4. <A HREF="heliPage.html" onMouseover=
    "document.textField.src=heliText.src" onMouseout=
  5. The third link, for the helicopter, sets document.textField.src equal to heliText.src.

  6. <IMG SRC="images/bg.gif" WIDTH="208" HEIGHT="27" NAME="textField" VSPACE="20" ALT="Text Field">

    This is the IMG tag for the description image, with the name textField, and it shows the key to this script. All three of the anchor tags reference the same document object (document.textField.src), not different objects. That's because they're all changing the same object.

  • + Share This
  • 🔖 Save To Your Account