Forum Moderators: open
<!DOCTYPE html>
<html>
<head>
<title>Image mouseover/out test</title>
</head>
<body>
<!--
Each image gets a unique id. It doesn't matter what the name of
each image file is... they could be org0.jpg, foobar.jpg, 010110.jpg
-->
<img src="org0.jpg" id="img1">
<img src="org1.jpg" id="img2">
<img src="org2.jpg" id="img3">
<!--
It would be preferable to put this in an external script file but
for the sake of this example I'm including it in the HTML document.
Either way, put this at the END of the document, just before the
closing body tag to ensure we can find the images in the document.
-->
<script type="text/javascript">
(function () {
var el,
id,
imgSwap = { // Create the map of id to images
img1: { oldImg: 'org0.jpg', newImg: 'new0.jpg' },
img2: { oldImg: 'org1.jpg', newImg: 'new1.jpg' },
img3: { oldImg: 'org2.jpg', newImg: 'new2.jpg' }
};
for (id in imgSwap) {
if (imgSwap.hasOwnProperty(id)) {
el = document.getElementById(id);
if (el) {
// Attach event handlers
el.onmouseover = function (el, src) {
return function () {
el.src = src;
};
}(el, imgSwap[id].newImg);
el.onmouseout = function (el, src) {
return function () {
el.src = src;
};
}(el, imgSwap[id].oldImg);
}
}
}
})();
</script>
</body>
</html>
<img src="org0.jpg" id="img1">
<img src="org1.jpg" id="img2">
<img src="org2.jpg" id="img3">
imgSwap = { // Create the map of id to images
img1: { oldImg: 'org0.jpg', newImg: 'new0.jpg' },
img2: { oldImg: 'org1.jpg', newImg: 'new1.jpg' },
img3: { oldImg: 'org2.jpg', newImg: 'new2.jpg' }
};
for (id in imgSwap) {
if (imgSwap.hasOwnProperty(id)) {
el = document.getElementById(id);
if (el) {
// Attach event handlers
el.onmouseover = function (el, src) {
return function () {
el.src = src;
};
}(el, imgSwap[id].newImg);
el.onmouseout = function (el, src) {
return function () {
el.src = src;
};
}(el, imgSwap[id].oldImg);
// Preload images
var preloadImg = new Image();
preloadImg.src = imgSwap[id].newImg;
}
}
}