I have been utilizing a straightforward function that doesn't rely on any framework or slide transitions, making it an excellent starting point for beginners.
function SlideShow( elem_id, hold_time )
{
this.elem = document.getElementById( elem_id );
this.slides = [];
this.num_slides = 0;
this.cur_slide = 1;
this.add_slide = function( image )
{
this.slides[ this.num_slides++ ] = image;
}
var self = this;
this.next_slide = function()
{
if ( self.num_slides > 1 )
{
self.elem.src = self.slides[ self.cur_slide++ ].src;
if ( self.cur_slide == self.num_slides )
self.cur_slide = 0;
}
}
setInterval( self.next_slide, hold_time )
}
The parameters required are the element_id of an img tag and the duration in milliseconds to display each slide.
The add_slide function accepts a JavaScript Image object.
The initial value of cur_slide is set to 1 because I preload the img tag with the first image.
In my implementation, I construct the slideshow within the window.onload method and ensure each Image adds itself to the slideshow upon loading.
Example (not tested):
window.onload = function() {
var slide_show = new SlideShow( "slide_image", 4000 )
{ var img = new Image();
img.onload = function(){ slide_show.add_slide(img); };
img.src="/images/slide1.jpg"; }
...
/* Repeat process for each image */
}
This method works well if the sequence of slides does not matter.