SCSI Hard Drives
Search
Advanced Search

Categories


Recently Viewed
Pages



My Links
Web Directory Index
A human edited, comprehensive web directory list.
Link Exchange
DesignFirms Link Exchange


Changing images on mouse over using Javascript

By : yankees26an
Rating : Average Rating : 4.00 From 3 Voter(s)


The JavaScript function  can be placed in the HTML head or in an external JavaScript file (.js) so that it loads right at the beginning of the document.This function expects four arguments. The function is called from onmouseover and onmouseout event handlers and the four parameters are passed to it.

 

To facilitate smooth image rollovers in JavaScript, it's recommended that the images are preloaded so that the extra trip to the server is saved when the visitor moves the mouse over the image and the image changes immediately. Preloading is the process by which all images that are to be used for roll-overs are loaded along with the document.

Here is the code:


 


<SCRIPT LANGUAGE="JAVASCRIPT">
<!--


var image1 = new Image();
image1.src = "moveup.gif";
var image2 = new Image();
image2.src = "movedown.gif";
var image3 = new Image();
image3.src = "an1.gif";
var image4 = new Image();
image4.src = "an2.gif";


//-->
</SCRIPT>


Note: The preloader code should be placed in the HTML head, preferably before any other JavaScript code.


The code above assumes that the images are located in the same directory as the HTML file. If the images are placed in another directory, use relative URLs for the images in the preloader script as well for passing arguments to the rollover function.

 

 Let's consider you have 10 images being used as icons on a web page and since you have read the previous article, you plan to have JavaScript image rollovers for each image. Writing JavaScript code inside the <A> for each image becomes very messy, not to mention, very troublesome and error prone.


A simple solution would be to collect the code into a function that can be placed between <SCRIPT> tags, inside the HTML head or in an external JavaScript (.js) file. We can then call this function with appropriate arguments from the event handlers.

<SCRIPT LANGUAGE="JAVASCRIPT">
<!--


function roll(img_name, img_src)
   {
   document[img_name].src = img_src;
   }


//-->
</SCRIPT>



This function expects two arguments, one is the image name and the other is the image source. The function is called from onmouseover and onmouseout event handlers and the two parameters are passed to it.

<A HREF="somewhere.html"
onmouseover="roll('sub_but', 'movedown.gif')"
onmouseout="roll('sub_but', 'movetup.gif')">

<IMG SRC="moveup.gif" WIDTH="143" HEIGHT="39" BORDER="0"
ALT="Move your mouse over me" NAME="sub_but">

 








Comments / Feedback

RSS 2.0: Syndicate this article

Add Comment
* Name


* Email Address


Site



*Image Validation (?)


*Comments / Feedback





Print Article Print Article Send to a friend Send to a friend Save as PDF Save as PDF Social Bookmarking
Add to: Mr. Wong Add to: Webnews Add to: Icio Add to: Oneview Add to: Folkd Add to: Yigg Add to: Linkarena Add to: Digg Add to: Del.icio.us Add to: Reddit Add to: Simpy Add to: StumbleUpon Add to: Slashdot Add to: Netscape Add to: Furl Add to: Yahoo Add to: Spurl Add to: Google Add to: Blinklist Add to: Blogmarks Add to: Diigo Add to: Technorati Add to: Newsvine Add to: Blinkbits Add to: Ma.Gnolia Add to: Smarking Add to: Netvouz Information
Rate this Article :

1

2

3

4

5

6

7

8

9

10
Poor Excellent