create a slide show using javascipt ?



How to create a  slide show using javascript

Step 1: take 3 images, I am using these three images:


1.png

2.png

3.png




Step 2: Preload the images using JavaScript.
The term "preload" in JavaScript refers to the loading of images into cache prior to displaying them. Preloading images is "necessary" in a slide 

show, since the switching between images have to be instantaneous, without any delay:

<head>
<script type="text/javascript">
<!--
var image1=new Image()
image1.src="1.png"
var image2=new Image()
image2.src="2.png"
var image3=new Image()
image3.src="3.png"
//-->
</script>
</head>

We've created three instances of the image object, each referring to one of the images that make up the slide show. By doing so, the images are 

loaded into cache, standing by for us to display at anytime. Notice that the entire code is inserted in the <head> section of the page (Detailed 

discussion of preloading images can be found here).

Step 3: Add in the html codes necessary to display the first image of the slide show.

<body>
<img src="1.png" name="slide" width=200px height=200px>
</body>

All we've done above is insert the first image of the slide show using HTML. Notice how we gave the image a "name" attribute. By naming the image 

with an arbitrary name, it enables JavaScript to access and manipulate the image, which we will see next.
Step 4: With everything in place, all we need now is a script that accesses the above image and changes the src of the image periodically, creating 

a slide show. The below lists the complete script:

<script type="text/javascript">
<!--
//variable that will increment through the images
var step=1
function slideit(){
//if browser does not support the image object, exit.
if (!document.images)
return
document.images.slide.src=eval("image"+step+".src")
if (step<3)
step++
else
step=1
//call function "slideit()" every 2.5 seconds
setTimeout("slideit()",2500)
}
slideit()
//-->
</script>

The core of this script is the part in red:

document.images.slide.src=eval("image"+step+".src")

The left-hand side accesses the path of image "slide" using the image object, then the name of the image, then the keyword "src". The path of any 

image in an HTML document can be accessed this way. The right hand side dynamically assigns a new src (path) to the image, thus changing the 

image. The three possible paths the image may receive are:

image1.src  //"1.pnf"
image2.src  //"2.png"
image3.src  //"3.png"

in that order.

This will look like thsi:-


Step 5: Putting it all together.
We've preloaded the images, inserted the first image of the slideshow, and created the function necessary to change the path associated with the 

image every few seconds. All we have to do now is put it all together into one page, and we've got ourselves a slideshow:

<html>
<head>
<script type="text/javascript">
<!--
var image1=new Image()
image1.src="1.png"
var image2=new Image()
image2.src="2.png"
var image3=new Image()
image3.src="3.png"
//-->
</script>
</head>
<body>
<img src="1.png" name="slide" width="200px" height="200px" />
<script>
<!--
//variable that will increment through the images
var step=1
function slideit(){
//if browser does not support the image object, exit.
if (!document.images)
return
document.images.slide.src=eval("image"+step+".src")
if (step<3)
step++
else
step=1
//call function "slideit()" every 2.5 seconds
setTimeout("slideit()",2500)
}
slideit()
//-->
</script>
</body>
</html>