JavaScript: Image Rollover

By Xah Lee. Date: . Last updated: .

This page shows a JavaScript based image rollover.

This technique is used around 1999. Today, you should use pure CSS based image rollover instead, see CSS: Image Rollover

Move your mouse to the following image and see it change. (on mobile devices, touch the image also works.)

tile A

Here is the HTML code:

<img id="img75439" src="i/tile_A.png" alt="tile A">

Here is the JavaScript code:

const img75439 = document.getElementById("img75439");

// pre-cache
(new Image()).src = "i/tile_B.png";

const f_toA = (() => { img75439.src="i/tile_A.png"; });

const f_toB = (() => { img75439.src="i/tile_B.png"; });

img75439.addEventListener("mouseover", f_toB , false);

img75439.addEventListener("mouseout", f_toA , false);

Pre-Cache Images

JavaScript: Pre-Cache Images

JavaScript in Depth

DOM Scripting


DOM Methods

Basic Examples

HTML Input

Web Scripting Examples

Web Scripting Misc