I am new to JS and am trying to tweak this code to show a range of images. As you can see I have named the images from 1-50. (although I haven't finished)
Is there a way I can add each image in the function rather then copying the function for each image? The way I have copied it, as soon as it hits the 2nd image it will only add the next image. Do I have to copy the function again and again or can I add all images in the function.
I will need someone to give me the code if possible as I am only new to JS. Thank you.
- Code: Select all
/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Solomon, the Sleuth :: http://www.freewebs.com/thesleuth/scripts/ */
// List image names without extension
var myImg= new Array(50)
myImg[0]= "1";
myImg[1]= "2";
myImg[2]= "3";
myImg[3]= "5";
myImg[4]= "6";
myImg[5]= "7";
myImg[6]= "8";
myImg[7]= "9";
myImg[8]= "10";
myImg[9]= "11";
myImg[10]= "12";
myImg[11]= "13";
myImg[12]= "14";
myImg[13]= "";
myImg[14]= "";
myImg[15]= "";
myImg[16]= "";
myImg[17]= "";
myImg[18]= "";
myImg[19]= "";
myImg[20]= "";
myImg[21]= "";
myImg[22]= "";
myImg[23]= "";
myImg[24]= "";
myImg[25]= "";
myImg[26]= "";
myImg[27]= "";
myImg[28]= "";
myImg[29]= "";
myImg[30]= "";
myImg[31]= "";
myImg[32]= "";
myImg[33]= "";
myImg[34]= "";
myImg[35]= "";
myImg[36]= "";
myImg[37]= "";
myImg[38]= "";
myImg[39]= "";
myImg[40]= "";
myImg[41]= "";
myImg[42]= "";
myImg[43]= "";
myImg[44]= "";
myImg[45]= "";
myImg[46]= "";
myImg[47]= "";
myImg[48]= "";
myImg[49]= "";
myImg[50]= "";
// Tell browser where to find the image
myImgSrc = "images/";
// Tell browser the type of file
myImgEnd = ".gif"
var i = 0;
// Create function to load image
function loadImg(){
document.imgSrc.src = myImgSrc + myImg[i] + myImgEnd;
}
// Create link function to switch image backward
function prev(){
if(i<1){
var l = i
} else {
var l = i-=1;
}
document.imgSrc.src = myImgSrc + myImg[l] + myImgEnd;
}
// Create link function to switch image forward
function next(){
if(i>2){
var l = i
} else {
var l = i+=1;
}
document.imgSrc.src = myImgSrc + myImg[l] + myImgEnd;
}
// Load function after page loads
window.onload=loadImg;[b]



