|
||||||
How to Create a Javascript Web Page Slide ShowAutomatically Updating Displayed Images by Using Javascript CodeThe setTimeout enables a web site programmer to create animations simply and easily. They can create an impressive slide show show with just a little Javascript coding
It is very easy to create an animation loop with Javascript . How to Make Web Page Animations Using Javascript shows how an image can be moved around the display area of a web browser using the setTimeout method. The command is used by a function to call itself after a short interval (for example 10ms) and this creates an infinite loop which can be used to animate a web page object. And, of course, this infinite loop can also be used to produce other effects on the web page. Effects such as a slide show. Creating an Infinite Loop with setTimeoutThe infinite loop is created by making a function call itself after a set time period (in this case 1 second): function slideshow () {
setTimeout (function() { slideshow ()} , 1000, "JavaScript");
}
Here the function reset the timer when ever it runs, however it does not run automatically. The function mas be called from Javascript in order to set the loop in motion: slideshow () ;
Nothing obvious will happen if this is viewed in a web browser, but something more definite can be seen with a little more code: <body>
<div id=opDiv></div>
<script language="javascript">
var i = 0;
var opDiv = document.getElementById("opDiv");
function slideshow () {
if (i>10) {i=0;}
opDiv.innerHTML = i;
i++;
setTimeout (function() { slideshow ()} , 1000, "JavaScript");
}
slideshow () ;
</script>
This time the web browser will be seen to continually count from 0 to 9. It's this simple technique that can be adapted to create a simple slide show. Creating a Slide Show with setTimeoutThe first step of creating a slide show is to set where the images are to be located and which is the default image: <body>
<img id="slideshow" src="image03.jpg">
<script language="javascript">
var slideshow_img = document.getElementById("slideshow");
The next step is to define an array with the names (or the url if they are in a different location to the web page) of the images to be used in the slide show (examples of which can be seen in Figures 1-3 at the bottom of this article). The array is created by using the "new" method: var images= new Array ("image01.jpg","image02.jpg","image03.jpg");
And then the programmer can then create the animation that will produce the slide show: var i = 0;
function slideshow () {
if (i>images.length-1) {i=0;}
slideshow_img.src = images[i];
i++;
setTimeout (function() { slideshow ()} , 1000, "JavaScript");
}
It is worth noting here how the length of the array is used to determine when the loop should be reset to zero, and that the loop count is then used as the index number for the image to be loaded. This means that if the code is saved into a .html file and viewed in a web browser then the user will see a slide show that reveals a fresh image once every second.
The copyright of the article How to Create a Javascript Web Page Slide Show in Javascript/Java Programming is owned by Mark Alexander Bain. Permission to republish How to Create a Javascript Web Page Slide Show in print or online must be granted by the author in writing.
Comments
Oct 4, 2009 10:18 AM
Guest :
Oct 10, 2009 10:40 AM
Yuen Kit Mun :
2 Comments
|
||||||
|
|
||||||
|
|
||||||