Least Tern > Tech Class > Teacher Resources > Resources for Web Design

JavaScript: Hover & Click Effects

Hover effect: change graphic | Alert Box | Open new window from link


A graphic that changes with the onMouseover event (this is also a link to Google):

You will need two saved graphics in the same folder as your page. They must be exactly the same size. Keep the graphics small in size.

Code: Change the words in red to your link reference and your image file paths.

Paste this between <head> and </head>:

<script language = "JavaScript">
<!--
//Active images:if (document.images)
{
img1on=new Image();
img1off=new Image();
img1on.src="greendot.gif";
img1off.src="reddot.gif";
}
//Function to 'activate' images:
function imgOn(imgName)
{
if (document.images)
{
document[imgName].src = eval(imgName + "on.src");
}
}
function imgOff(imgName)
{
if (document.images)
{
document[imgName].src = eval(imgName + "off.src");
}
}
var leftPos = 0
if (screen) {
leftPos=screen.width-60}

-->
</SCRIPT>

Paste this into the <body> where you want the image to appear.

<a href = "http://www.google.com" onMouseOver="imgOn('img1')" onMouseOut="imgOff('img1')"><img src="reddot.gif" border=0 name="img1" width="32" height="32"></a>


Next: Click this link to pop open an alert box. You can use this with onMouseOver as well.

Code: add the red to your <a href...> tag. Change the alert message. The quotation marks and parenthesis are important!!

<a href="#" onClick="alert('Use this for notes, hints and definitions')">

Another version of this alert box script can be found on the Claris Home Page styles tutorial.


Last: open a new window by clicking on a graphic (or on a link).

Click this green dot to open a new window to see a list of possible "contacts". The size of the window is set in the JavaScript function. Resize this NewWindow to suit the needs of your image or information.

Elizabeth Sky-McIlvain 1/19/04