a | |||||||||
![]() |
Image Rollover? | ||||||||
An image rollover is the most loved
JavaScript effect. We recieve more questions about rollovers alone, then any other script.
The image rollover saved the name of JavaScript by erasing the bad name scrolling text
gave the language. This trick will change the graphic when the mouse is moved over it. When creating an image rollover, you need at least 2 different graphics; the one that loads with your webpage and the one that loads when the mouse is moved over the original image. In our example there is a total of 8 different images.
Above you see the images called within your javascript. The
first image objects called begin with In our example,
When calling an image rollover script, you use event
handlers passing two functions
This function passes the name of the image to the variable
That is the mystery behind that amazing rollover function in JavaScript. We have a second tutorial on an alternative way to do image rollovers. Please read below; this script checks the browser before loading and preloads the graphics. It is a bit more complicated.
|
|||||||||
There are many ways to do an image rollover. In this example, we see that the JavaScript preloads the images, where in the first, the images must load when the mouse passes over. This example also does a browser check to make sure the browser supports the script. The first thing you should do is have a browsercheck. The
best browsercheck when we're doing a rollover is Then next thing you should do is preload the images. First
"make" a new image object with the new Image() function Do I have to do that for every image you ask? Since we are very nice we have made a preloading function for you so you wont have to do that! To make it easy to follow the rest of this tutorial you should name you images like this: You make 5 images, name them norm1.gif norm2.gif and so on. And 5 images that the norm image will change to when you mouseover, name them over1.gif over2.gif and so on. Then place all the images in a folder named images. (you can change this later if you wish)
The function used above will preload the images. We call it
with the line There is how to preload the images in a nutshell. If you
have more then five pictures or other names on the pictures just change it in those
calling lines.(example: you have 15 images, they are in the same folder as the html
document, and they are named hello1.jpg hello2.jpg and so on. The line would then be: Here is the functions that will switch the images (we will name the images in page later):
That function is the one that changes the norm image to the over image when you mouseover. Lets go line by line:
And here is the rollout function:
This function does basically the same thing only the other way around. Now lets make the page that the script will run on: <HTML>
As you can see we name the images; norm1, norm2, norm3, norm4 and norm5. And we call the rollOn function on mouseover and the rollOff function on mouseout. The link point to # which really just means the top of the page. Now we have to put in the finished script:
Just place this between the head tag in the page and your set! |
|||||||||