JavaScript

  1. Home
  2. Computing & Technology
  3. JavaScript

Rollover Gallery

2. Obtain the Script

clr gif
Join the Discussion

Questions? Comments?

More of this Feature

Introduction Add to Your Page

To make a rollover gallery for your web page you first need to preload all of the larger images using a Javascript in the head section of your page.

Here is the code that I used for the sample page:

if (document.images) {
image0 = new Image;
image1 = new Image;
image2 = new Image;
image0.src = 'graphics\/pic1a.gif';
image1.src = 'graphics\/pic2a.gif';
image2.src = 'graphics\/pic3a.gif';
} else {
image0 = '';
image1 = '';
image2 = '';
document.rollimg = '';
}

The code shown in bold should be kept exactly as shown

In my example, I have three images so I need three sets of lines to define three images and load the particular graphical images I want associated with them. You just need to specify as many separate entries as you need for your particular setup.

Once you have created your code you just save it to an external javascript file. I called mine img.js. You then link it into the head of your page using the following code:

<script type="text/javascript" src="img.js">
</script>

Now all that remains is to add the images to your page.

Sample Page | Obtain the Script | Add to Your Page

Explore JavaScript

About.com Special Features

JavaScript

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.