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's the HTML code:

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

Here's 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

βˆ‘ JS in Depth
XAH Β BUY NOW

JS in Depth

JS Obj Ref

DOM


DOM

Overview

Basic Examples

HTML Input

Web Scripting Examples

Web Scripting Misc

jQuery

node.js