www.cryer.co.uk
Brian Cryer's Web Resources

How to implement an automatic slide show

An updated version of this slide show script (published July 2008), providing cross browser fade effects (the fade effect used here only works with Internet Explorer) can be found here. This copy is maintained here for historical purposes only.

A slide showslide show is where a number of images are displayed in sequence one after another. An example is shown here to the right of this paragraph.

To achieve this effect three things are required:

  1. An image placeholder
  2. JavaScript to run the slideshow
  3. The images to be shown

1. An image placeholder

This is a standard HTML image and is the image displayed before the slide show starts. The only things to bare in mind are the height and width of the image must be pre-set and that the image must be given a unique id. These are dealt with below under step 3. As an example, this page uses the following HTML for its image:

<img border="1" src="emily01.jpg" width="200" height="200" id="EmilyPicture">

2. JavaScript to run the slideshow

This page uses the following JavaScript, which you are free to copy:

<script type="text/javascript">
// Browser Slide-Show script.
// With image cross fade effect for those browsers that support it.
// Script copyright (c) 2004-2008 www.cryer.co.uk.
// Script is free to use provided this copyright header is included.
var slideCache = new Array();
function RunSlideShow(pictureName,imageFiles,displaySecs)
{
  var imageSeparator = imageFiles.indexOf(";");
  var nextImage = imageFiles.substring(0,imageSeparator);
  var picture = document.getElementById(pictureName);
  if (picture.filters)
  {
    picture.style.filter="blendTrans(duration=2)";
    picture.filters.blendTrans.Apply();
  }
  picture.src = nextImage;
  if (picture.filters)
  {
    picture.filters.blendTrans.Play();
  }
  var futureImages= imageFiles.substring(imageSeparator+1,imageFiles.length)
    + ';' + nextImage;
  setTimeout("RunSlideShow('"+pictureName+"','"+futureImages+"',"+displaySecs+")",
    displaySecs*1000);
  // Cache the next image to improve performance.
  imageSeparator = futureImages.indexOf(";");
  nextImage = futureImages.substring(0,imageSeparator);
  if (slideCache[nextImage] == null) {
    slideCache[nextImage] = new Image;
    slideCache[nextImage].src = nextImage;
  }
}
</script>

If this script were to be used in a number of pages then it might be best included in a separate script file instead of being embedded directly in a web page.

3. Images to be shown

The final step is to establish what images are to be shown as part of the slideshow and how long each image is to be displayed before moving onto the next one. As an illustration, this page uses the following:

<script type="text/javascript">
RunSlideShow("EmilyPicture","emily01.jpg;emily02.jpg;emily03.jpg;"
    + "emily04.jpg",5);
</script>

This line should be included in the HTML file after the JavaScript that defines the  "RunSlideShow" function and below the image placeholder.

The "RunSlideShow" JavaScript function takes three values:

1. pictureName The picture-name is the identifying name given to the image. This identifies the image in which to run the slide show. This image will be replaced with each slide show image in turn. (The name was specified as part of step 1 above "An image placeholder".) In the HTML the picture name is specified by including a statement of the form:
id="pictureName"

in the img declaration statement.

2. imageFiles This is a string containing the names of the source files for each image to be displayed. Include a semi-colon (';') as a separator between the names of each image file.

It is very important that each image is the same size (height and width) as the original - or that it has the same aspect ratio (ratio of height to width) as the original. This is because each image is loaded into the same area or space that the original image occupied. If this images are not the same size then the browser will stretch the image to fit (and for example thus give the impression of very fat or very thin people) and this may be undesirable.

3. displaySecs This is the number of seconds that each image is to be displayed for.

That's it.

Other points to note:

<script type="text/javascript">
RunSlideShow("Picture1","F3_key.gif;F4_key.gif",4);
setTimeout('RunSlideShow("Picture2","F3_key.gif;F4_key.gif",4);',2000);
</script>