Lab 8: Javascript Events - Rollovers and Random ImagesAs you have seen in lecture, the implementation of rollovers depends on catching some JavaScript events and reacting to them, i.e. on creating Event Handlers. The events we are interested in are Click, MouseOver, and MouseOut. If you need a reminder about the relevant code to implement an image rollover, the lecture notes are linked here.
Part 1a: Simple RolloversToday, we will use the Rollover technique to create the effect, on a web page, of an image appearing from nowhere.
Part 1b: Double RolloversYou can use the same technique to catch more than one events! Look at these double rollovers.Part 2a: Using Javascript random numbersThe Javascript functionMath.random() generates a random fractional number
between 0 and 1.
To get a random number between 0 and n (n excluded), you use:
If you want an integer between 0 and n (n excluded), you can use
the You may want to look at this page, which uses the formula given above to produce integer random numbers in a certain range. (You will need to reload the page several times in order to see several random numbers.) You are encouraged to look at the source code as well! Part 2b: Displaying a random imageSeveral of you have expressed interest in displaying a random image on some page of your projects. Now, you can! For a demonstration, look at this page. (The Wellesley College home page also uses random images). Every time one accesses this page, they will see a different image. Reload the page to see this effect!Here is how to implement this:
Have fun with JavaScript!
Introduction | Syllabus | Assignments | Documentation | Project |