Feel free to use the example code shown below and build upon it so you can add more sophisticated components for your slideshow. Using the example below, you can control how many images are included in the show and the duration of each slide.

The HTML

The only HTML code needed is a simple

containing one (image) element. Make sure that you assign an id to the image element. Here is an example…

The CSS

There isn’t any CSS code that is required for this slideshow to function. However, if you would like to add additional styles to the

element and/or image element, you could do so.

The JavaScript

As you can see in the example below, there is not much JavaScript code needed for this example. The first few lines are used to set up our variables. imgArray is an array used to keep all of the image source information. The curIndex variable is used to keep track of the current image being displayed in our show. imgDuration is used to set the amount of time (in milliseconds) the image will be displayed before displaying the next image. The function called slideShow() is where the magic happens. There are only a few lines of code. The first line is used to assign the image element a new image source. As the curIndex variable changes, the image element will display a different image. After the image source is assigned, the curIndex variable is incremented by one. Once the curIndex variable equals the total number of images in our array, the variable is assigned a value of 0 so that the first image is displayed once again. The last line in the function creates a sort of loop for our function. Here is the JavaScript code…

Complete Example