|
|
JavaScript image rolloversJavaScript image rollovers have been one of the most used features for bringing interactivity to web pages. They refer to the change in the image when the mouse cursor is moved over and subsequently off the image. Rollovers employ two JavaScript event handlers. onmouseover, that captures the action when the mouse cursor is brought over an image and onmouseout, that detects the action when the mouse cursor is moved off an image. These two even handlers are placed inside the anchor tag that surrounds the IMG tag. We also use the src property of the JavaScript image object. This property refers to the file name of the image being displayed (image source). Finally, we employ the NAME attribute of the HTML IMG tag to explicitly refer to the image in question. The logic is extremely simple. It's a matter of changing the image when the mouse cursor is placed over it and then changing it back to the original when the mouse cursor moves off the image. Let's construct our image tag: <IMG SRC="moveup.gif" WIDTH="143" HEIGHT="39" BORDER="0" ALT="Move your mouse over me" NAME="sub_but"> Note: We use the NAME attribute of the HTML IMG tag to give the name sub_but to our image. JavaScript event handlers will refer to the image with this name. Now, we place anchor tags around this image: <A HREF="somewhere.html"> <IMG SRC="moveup.gif" WIDTH="143" HEIGHT="39" BORDER="0" ALT="Move your mouse over me" NAME="sub_but"> </A> Lastly, we include the two JavaScript event handlers inside this anchor tag. <A HREF="somewhere.html" onmouseover="document.sub_but.src='movedown.gif'" onmouseout="document.sub_but.src='moveup.gif'"> <IMG SRC="moveup.gif" WIDTH="143" HEIGHT="39" BORDER="0" ALT="Move your mouse over me" NAME="sub_but"> </A> Remember that the image object is a part of the document object and its src property refers to the file name of the image being displayed.
Page contents: Learn how to change one or two images on moving the mouse over images using JavaScript built-in event handlers.
Page URL: http://www.webdevelopersnotes.com/tips/html/ javascript_image_rollovers.php3
|
|