function ImageCollection(images) {
this.images = images;
this.i = 0;
this.next = function(imgId) {
var img = document.getElementById(imgId);
this.i++;
if (this.i == images.length )
this.i = 0;
img.src = images[this.i];
}
this.prev = function(imgId) {
var img = document.getElementById(imgId);
this.i--;
if (this.i <= 0)
this.i = images.length -1;
img.src = images[this.i];
}
}
Then you need to add two buttons below the image holder.
<img id='container' src="icono/antithese/preview.jpg" >
<input type='button' value='next' onclick='ic1.next("container")' />
<input type='button' value='prev' onclick='ic1.prev("container")' />
Now when you click the buttons it will move forward or backward.
You mentioned you don't fully understand what the code is doing so I offer this explanation:
First you are creating an object constructor named ImageCollection
which takes a parameter named images
. Next you are assigning the assumed 'array' of images to a member of that object named images
. Next you are creating a member named i
to keep track of the place in the array that you currently are.
The next two members are functions named next
and prev
. These functions take a single parameter that is the id of a container for the images.
Each of these functions does 4 things:
- Get a reference to the container object using the passed in id.
- Increments/Decrements the internal tracking member
i
. - Makes sure that the value of
i
will fall in the range of the array, that is to say there will be an element at positioni
. If there won't be, meaning you have gone past the end of the array or before the beginning of the array, theni
is reset so that it will be either the end or beginning of the array depending on which direction you are traveling. - Sets the source of the container to the value in the image array that matches
i
.
Now if you wanted to get really fancy you could make a pause play functionality for it also ensuring each function stops the automatic play. To do this look into the 'setTimeout' and 'clearTimeout' javascript functions.