I'm trying to create a background image 'slider' using javascript/ jQuery. The idea is to click an anchor and move through the images forward or backward respectively. I have my file names in an array called 'images.'
It works, except I can't figure how to revery form the last image back to the first and vise versa. In my console its throwing a variable undefined. Here is my code:
var i = 0;
var arrayLength = images.length - 1;
currentImage(0); //show the first photo initially
function currentImage(i) {
$('#image').css("background-image", "url(img/" + images[i] + ")");
if ( i > arrayLength ) {
$('#image').css("background-image", "url(img/" + images[0] + ")");
i = 0;
return;
}
console.log(i);
}
$('#prev').click(function(){
i--;
currentImage(i);
});
$('#next').click(function(){
i++;
currentImage(i);
});
I've tried using "if (i = undefined) {i=0; console.log("works");}" and "if (i > arrayLength) {i=0; console.log("works");}" both methods resulted in a successful console log, but neither method seems to reset i.
I can't seem to find a similar thread on SO, but maybe I'm searching the wrong things. Any help, or a point in the right direction would be great. Thanks!