Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > Ajax and JavaScript

  • Print
  • + Share This
This chapter is from the book

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. Script 4.16 shows the HTML needed, and the JavaScript in Script 4.17 has what you need to add slideshows to your pages.

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. Figure 4.14 shows the new slideshow.

Figure 4.14

Figure 4.14 Clicking the Previous or Next link calls the processPrevious() or processNext() function, respectively.

Script 4.16. This HTML page creates a slideshow.

→ Transitional//EN"
        → xhtml1-transitional.dtd">
<html xmlns="">
     <title>Image Slideshow</title>
     <script type="text/javascript"
     → src="script09.js"></script>
<body bgcolor="#FFFFFF">
     <div align="center">
        <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">&lt;&lt;
        → Previous</a>&nbsp;&nbsp;<a
        → href="next.html" id="nextLink">Next
        → &gt;&gt;</a></h2>

Script 4.17. 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;
     document.getElementById("myPicture").src =myPix[thisPic];
     return false;

function processNext() {
     if (thisPic == myPix.length) {
        thisPic = 0;
     document.getElementById("myPicture").src =myPix[thisPic];
     return false;

To build a wraparound slideshow:

  1. window.onload = initLinks;

    When the window finishes loading, trigger the initLinks() function.

  2. function initLinks() {
      → ("prevLink").onclick = 
      → processPrevious;
      → ("nextLink").onclick =
      → processNext;

    This function sets up the onclick event handlers for the Previous and Next links.

  3. 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.

  4. thisPic--;
    → ("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.

  5. thisPic++;
    if (thisPic == myPix.length) {
      thisPic = 0;
    →("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.

  • + Share This
  • 🔖 Save To Your Account