<a href="page2.htm"><img border="0" src="page2.gif" width="100" height="20"></a> |
| Example. |
Here on the left is a link to the page as it is so far. As you can see, it is not very smashing (yet). Before we can add the JavaScript to change the SRC property of the IMG tag, we need to be able to identify the IMG tag that we want to change. To be able to do this, we need to assign a value (in this case P2) to the NAME property of the IMG tag. This is done by adding the red text below.
<a href="page2.htm"><img name="P2"
src="page2.gif" width="100" height="20" border="0"></a>
The next thing we do is add the JavaScript function changepic() somewhere on the page. I did it in the top of the document. The code for this function is here below.
<script language="JavaScript">
<!--
function changepic(imgTagName,imgFileName)
{
document.images[imgTagName].src=imgFileName
}
// -->
</script>
The only thing this function does is changing the src property of the img tag, that is identified by the name in the first parameter, to the filename that is identified by the second parameter. The next thing we have to do is make sure that this function is called with the proper parameters when the mouse is hovered over the image. Below we see how the onMouseOver() event of the hyperlink is used to call the function. This is done by adding the red text. The first parameter, in this case P2, is the name of the image tag that needs to be changed. The second parameter, in this case page2-sel.gif is the name of the image file that must be showed while the mouse is over the image.
<a href="page2.htm"
onMouseOver="changepic('P2','page2-sel.gif');"><img
name="P2"
src="page2.gif" width="100" height="20" border="0"></a>
|
|
| Example. |
Here on the left is a link to the example as we created it so far. As you can see, the image changes nicely when we hover the mouse over it, but unfortunately we forgot to change it back when we move the mouse away from the image. Changing the picture back to it's original is done in the same way, except that we now use the onMouseOut() event to call the function. You can see this in the red text below.
<a href="page2.htm"
onMouseOver="changepic('P2','page2-sel.gif');"
onMouseOut="changepic('P2','page2.gif');"><img
name="P2"
src="page2.gif" width="100" height="20" border="0"></a>
|
|
| Example. |
Again here on the left there is a link to the example as we built it so far. It looks like it is all working correctly, but with a small "final touch" we can still improve it a little. We have to realize that when we publish this page to the internet, this means that when the visitor of our site hovers his mouse over the button, the image has to be picked-up from the site. This can, specially when slow connections or servers are involved, take some time. If this happens, the whole effect goes down the drain. We can, however, make sure that the new image does not have to be picked-up from the server but is already present in the visitors local cache. To accomplish this, we have to make sure that all images are downloaded with the page. Of course we don't want this to be visible for our visitor. We can do this by adding an invisible layer to the page, containing all the images we need. How this is done is shown below. <!-- Image PreLoad --> <layer name="imagepreload" visibility=hidden> <div style="visibility:hidden"> <img src="page2-sel.gif" width="100" height="20"> </div> </layer> <!-- End Image PreLoad --> Naturaly we want this to work for both Netscape and Internet Explorer, so we use both the layer tag and the div tag. |
| Example. |
On the left there is a link to the "ready to run" product of our effort. If we want to have more hover buttons on our page, we can do this in the same way. We only have to make sure that all the button images get a different name, that the proper name is used in the fuctioncall to changepic(), and that all the images we need are added to the preload layer.
|
| Last change on May 10th 2000. |