Forum Moderators: open
Are all the images of the same size? Do you have exactly 3X images, or is the number of images fluctuating? What will happen once you get to the end, or back to the beginning? Should it loop, or just stop?
We will start with something like this:
<a href="" onclick="previmg()">Prev</a>
<img id="imga" src="image1.jpg" alt="">
<img id="imgb" src="image2.jpg" alt="">
<img id="imgc" src="image3.jpg" alt="">
<a href="" onclick="nextimg()">Next</a>
previmg() or nextimg() is called, decrease or increase the counter by 3. If you go below 0, set to 0. If you go beyond the number of records, reduce to "3X - 3". src. <script type="text/javascript">
/*
Assumptions:
The images associated with each page are sequentially named.
(image1.jpg, image2.jpg, etc)
You also have a blank gif, called blank.gif.
*/
// How many images?
var intNumOfImgs = 10;
// What is the path to the images?
var strPathToImg = "./imgs/";
// What is the first part of the image name?
var strImgPrefix = "image";
// What is the image suffix (i.e. type)?
var strImgSuffix = ".jpg";
/*
YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS POINT!
*/
var intImgPerSet = 3;
var intCurImgSet = 1;
var intTotImgSet = (intNumOfImgs + intImgPerSet - (intNumOfImgs % intImgPerSet)) / intImgPerSet;
var arrImgSrc = new Array(intNumOfImgs + intImgPerSet - (intNumOfImgs % intImgPerSet));
for(var i = 0; i < intNumOfImgs + intImgPerSet - (intNumOfImgs % intImgPerSet); i++) {
arrImgSrc[i] = new Image();
if(i < intNumOfImgs) {
arrImgSrc[i].src = strPathToImg + strImgPrefix + (i + 1) + strImgSuffix;
}
else {
arrImgSrc[i].src = strPathToImg + "blank.gif";
}//end if
}//end for
function previmg() {
intCurImgSet--;
if(intCurImgSet < 1) {
intCurImgSet = 1;
}//end if
return setimages();
}//end previmg()
function nextimg() {
intCurImgSet++;
if(intCurImgSet > intTotImgSet) {
intCurImgSet = intTotImgSet;
}//end if
return setimages();
}//end nextimg()
function setimages() {
document.getElementById(imga).src = arrImgSrc[((intCurImgSet - 1) * intImgPerSet) + 1].src;
document.getElementById(imgb).src = arrImgSrc[((intCurImgSet - 1) * intImgPerSet) + 2].src;
document.getElementById(imgc).src = arrImgSrc[((intCurImgSet - 1) * intImgPerSet) + 3].src;
/*
If you ever want to use more than three images per page, do the following:
1) Edit the value for the intImgPerSet variable above
2) Add more <img> tags with IDs imgd, imge, imgf, etc.
3) Add more lines above:
document.getElementById(imgd).src = arrImgSrc[((intCurImgSet - 1) * intImgPerSet) + 4].src;
document.getElementById(imge).src = arrImgSrc[((intCurImgSet - 1) * intImgPerSet) + 5].src;
document.getElementById(imgf).src = arrImgSrc[((intCurImgSet - 1) * intImgPerSet) + 6].src;
etc.
*/
return true;
}//end setimages()
</script>
I am going to check some tutorials to see if I can make sense of this. Thanks again, and if you don't mind helping just a bit more it would be a godsend!
Sorry to be such a pain.
[edited by: coopster at 3:13 pm (utc) on April 6, 2009]
[edit reason] no personal urls please TOS [webmasterworld.com] [/edit]