Hello Friends,
Randomizing Your Slide Show This task shows how to produce a randomized slide show. The slide show continues to display random images for the list of available images as long as the page is being displayed in the browser.
<head> <script type="text/javascript"> var imageList = new Array; imageList[0] = new Image; imageList[0].src = “Blue hills.jpg”; imageList[1] = new Image; imageList[1].src = “Sunset.jpg”; imageList[2] = new Image; imageList[2].src = “Water lilies.jpg”; imageList[3] = new Image; imageList[3].src = “Winter.jpg”; function slideShow(imageNumber) { document.slideShow.src = imageList[imageNumber].src; var imageChoice = Math.floor(Math.random() * imageList.length); window.setTimeout(“slideShow(“ + imageChoice + “)”,3000); }
</script> </head> <body onLoad=”slideShow(0)”> <img src=”image1.jpg” width=100 name=”slideShow”> </body>
----------------------------------------------
Description 1)In the function, display the image specified in imageNumber in the place of the image named slideShow:
document.slideShow.src = imageList[imageNumber].src;
2)Create a variable named imageChoice, and assign it a random number from 0 to the last index in the imageList array by using
Math.floor, Math.random and imageList.length: var imageChoice = Math.floor(Math.random() * imageList.length);
3)Use the window.setTimeout method to schedule a call to the slideShow function with the value of imageChoice passed as a parameter. This will display the next random image in three seconds:
window.setTimeout(“slideShow(“ + imageChoice + “)”,3000);
|
No responses found. Be the first to respond and make money from revenue sharing program.
|