Documente Academic
Documente Profesional
Documente Cultură
Lecture 23
Objectives
Understand the names and usage of JavaScript events. Create an image rollover. Make a hyperlink rollover. Build a cycling banner. Display random images. Create a JavaScript slide show.
2
Event: This is an operating system response to the occurrence of a specific condition. It can invoke a function.
onMouseOut onMouseOver
Function: This is a piece of JavaScript written by the programmer that is called upon to perform certain tasks. It can contain any number of JavaScript statements, including calls to other functions or methods.
Result: the onMouseOver event turns the arrow red and onMouseOut turns the arrow blue.
Event handling: JavaScript event statements are placed within standard HTML tags. For example, the onMouseOver and onMouseOut events are located within the opening anchor (<A>) tag. These events call the functions turnBlue() and turnRed().
<A HREF=" " onMouseOut="turnBlue()" onMouseOver="turnRed()"> <IMG NAME="arrow" SRC="bluearrow.gif">
8
The document.arrow.src = blueArrow.src; and document.arrow.src = redArrow.src; objects have already been assigned.
var blueArrow = new Image; var redArrow = new Image; blueArrow.src = "bluearrow.gif"; redArrow.src = "redarrow.gif";
10
A cycling banner is really nothing more than a sequence of graphic images that are displayed one after another with a small pause between each image change.
11
12
14
<HTML> <HEAD> <TITLE>Cycling Banner</TITLE> <SCRIPT> var imgArray = new Array(4); imgArray[0] = new Image; imgArray[0].src = "lions.gif"; imgArray[1] = new Image; imgArray[1].src = "tigers.gif"; imgArray[2] = new Image; imgArray[2].src = "bears.gif"; imgArray[3] = new Image; imgArray[3].src = "ohmy.gif"; var index = 0; function cycle() { document.banner.src = imgArray[index].src; index++; if (index == 4) { index = 0; } setTimeout("cycle()", 2000); return; } </SCRIPT> </HEAD>
16
A cycling banner can display random images one after another with a small pause between each image change.
17
19
20
21
When you allow the user to change the image by clicking on some object with the mouse, the end result is something akin to a slide show.
22
27
Summary
Now you should understand the names and uses of JavaScript events. You can create an image rollover and can make a hyperlink rollover. You can build a cycling banner. You can display random images. You can create a JavaScript Slide Show.
28