- Creating Rollovers
- Creating More Effective Rollovers
- Building Three-State Rollovers
- Triggering Rollovers from a Link
- Making Multiple Links Change a Single Rollover
- Working with Multiple Rollovers
- Creating Cycling Banners
- Adding Links to Cycling Banners
- Building Wraparound Slideshows
- Displaying a Random Image
- Cycling Images with a Random Start
Building Wraparound Slideshows
Slideshows on Web sites present the user with an image and let the user control the progression (either forward or backward) of the images. JavaScript gives the user the interactive control needed. Listing 4.18 shows the HTML needed, and the JavaScript in Listing 4.19 has what you need to add slideshows to your pages.
Listing 4.18. This HTML page creates a slideshow.
<!DOCTYPE html> <html> <head> <title>Image Slideshow</title> <script src="script09.js"></script> <link rel="stylesheet" href="script01.css"> </head> <body> <div class="centered"> <h1>Welcome, Robot Overlords!</h1> <img src="images/robot1.jpg" id="myPicture" width="200" height="400" alt="Slideshow"> <h2><a href="previous.html" id="prevLink"><<Previous </a> <a href="next.html" id="nextLink">Next >></a></h2> </div> </body> </html>
Listing 4.19. This script builds a slideshow that the user can click through using links to control movement forward and back.
window.onload = initLinks;
var myPix = new Array("images/robot1.jpg","images/robot2.jpg","images/robot3.jpg"); var thisPic = 0;function initLinks() {
document.getElementById("prevLink").onclick = processPrevious;
document.getElementById("nextLink").onclick = processNext;
}
function processPrevious() {
if (thisPic == 0) {
thisPic = myPix.length;
}
thisPic--;
document.getElementById("myPicture").src = myPix[thisPic];
return false; } function processNext() {thisPic++;
if (thisPic == myPix.length) {
thisPic = 0;
}
document.getElementById("myPicture").src = myPix[thisPic];
return false; }
This script builds a slideshow that wraps around—that is, if you go past the end of the list you go back to the beginning and vice versa. shows the new slideshow.
To build a wraparound slideshow:
- window.onload = initLinks;
When the window finishes loading, trigger the initLinks() function.
function initLinks() { document.getElementById("prevLink").onclick = processPrevious; document.getElementById("nextLink").onclick = processNext; }
This function sets up the onclick event handlers for the Previous and Next links.
function processPrevious() { if (thisPic == 0) { thisPic = myPix.length;
This function makes the slideshow run in the Previous direction. This first part checks to see if thisPic is equal to 0. If it is, the function gets the number of pictures in the myPix array.
thisPic--; document.getElementById("myPicture").src = myPix[thisPic];
The first line reduces the value of thisPic by 1. The next line sets the src of myPicture to the element of the myPix array represented by the current value of thisPic.
thisPic++; if (thisPic == myPix.length) { thisPic = 0; } document.getElementById("myPicture").src = myPix[thisPic];
This code, inside the processNext() function, makes the slideshow run in the Next direction and is much like the processPrevious() function. The first thing it does is increment the value of thisPic by 1. Then it checks to see if the value of thisPic is the same as the number of items in the myPix array. If so, it sets thisPic back to 0. The next line sets the src of myPicture.