HTML and CSS Reference
EXAMPLE 12.4 ( CONTINUED )
View next baby<br />
View previous baby<br />
The array myImages consisting of four images is created by the Array() constructor.
The index value for the array is assigned to a variable called index_val .
A function called next_image() is defined. When called, the function will cause
the next image in the array to be displayed.
By increasing the value of the index, the next image in the array will be accessed.
As long as the end of the array hasn't been reached, the block will be entered and
the new image displayed.
The name of the image, babypic , is used as an index into the images array to refer-
ence the default image by name. By assigning a new image (from the myImages ar-
ray) to the image's src property, the current image will be replaced by the new image.
If the end of the array has been reached, the statements within the else block will
be executed, resetting the array index back to the beginning, index 0.
A function called previous_image() is defined. When called, it will go backward in
the array and cause the previous image to be displayed.
If the index value is still
0, we are still within the boundaries of the array.
If by subtracting one from the index value, we have ended up with a value of -1,
we are out of the bounds of the array, and will set the index value back to the size
of the array, its length -1.
This is the initial image displayed on the screen before the user initiates an action.
See Figure 12.11.