Swap on Hover

Hover on any of the images to the left to change the larger image to the right. This method makes use of an old JavaScript technique that preloads the images it uses and loads a larger image using the onMouseOver and onMouseOut methods.



The Markup


<div id="thmbs">
	<img src="images/flowers.jpg" 
		onMouseOver="swap(1, 'imgDefault')" 
		onMouseOut="swap(0,'imgDefault')" >
	<img src="images/leaf.jpg" 
		onMouseOver="swap(2, 'imgDefault')" 
		onMouseOut="swap(0, 'imgDefault')">
	<img src="images/cat.jpg" 
		onMouseOver="swap(3, 'imgDefault')" 
		onMouseOut="swap(0, 'imgDefault')">
</div>
<div id="target">
	<img name="imgDefault" src="images/default.gif" >
</div>


The Script


<SCRIPT LANGUAGE="JavaScript">

  var aryImages = new Array(2);

  aryImages[0] = "images/default.gif";
	aryImages[1] = "images/flowers_LRG.jpg";
  aryImages[2] = "images/leaf_LRG.jpg";
	aryImages[3] = "images/cat_LRG.jpg";

  for (i=0; i < aryImages.length; i++) {
    var preload = new Image();
    preload.src = aryImages[i];
  }

  function swap(imgIndex, imgTarget) {
    document[imgTarget].src = aryImages[imgIndex];
  }
</script>
  

Man you made my day!

I've been looking for this for ages. It's insane how easy you made it look!!! Thanks a lot for the script. By the way I also loved the Javascript slideshow.

Thank you!

Thank you for your code. It's fanastic! Clean and simple. I appreciate your help and thank you for posting.

Very good

I have been searching 4 hours. Thank you very much for clean code.

Thanks a lot. That is

Thanks a lot. That is exactly what i want. Its a easy to understand and nice code.

Awesome

Thanks. nice easy and clean piece of code that I have been looking for.

Thank you! I've been

Thank you! I've been searching all over the internet for this markup and script, and I've found rollover items pretty similar but not exactly what I wanted, UNTIL NOW! Thank you for posting and sharing!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

Verify Your Humanity
This question is for testing whether you are a human visitor and to prevent automated spam submissions.