Rollover / Image Swap

Rollover / Image Swap


Rollovers, Mouseovers, Image Swaps... It goes by many variations of names, but means the same thing. Making one image change into a different one when a mouse pointer goes over a specified area.

The first thing you will need are 2 images. One for normal view, a second for the change.

The first image will be placed on the page as it normally would. The one main difference is you have to specify a name in the img tag like so...

This image can now be accessed by javascript as document.MyImage. Of course add in the width, height, alt, and border properties as desired too.

The next part is to create a link around that image. This will allow javascript to affect the image properties.

  • SRC is the SouRCe of the image file.
  • document.MyImage.src specifies that something is happening to the src of the object named MyImage on this document (web page). Remember... JavaScript is a step language. It goes level by level to find and use something.
  • OnMouseOver and OnMouseOut are self explanatory. They will do this specified coding when this event happens. For the example, They are being used to specify a new src for the named image.

This type of rollover effect is very common for navigation uses. You can create two images being close to the same. One image may have regular text on it, the other having highlighted text. Put them together in a rollover effect and you have a neat navigation happening. Placing a value in the href will complete the navigation effect.

Table of Contents