swollenboy
swollenboy

Reputation: 65

Reset variable when cycling through index of a Javascript array

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!

Upvotes: 0

Views: 112

Answers (4)

Dexter Bengil
Dexter Bengil

Reputation: 6625

In your $('#prev') and $('#next') click functions, check your images first if there is an item at the index of [i] with that before doing the iteration. For example:

$('#prev').click(function(){
   if(images[i-1] != undefined){
      i--;
      currentImage(i);
   }else{
      // do the reset here, or if you want you can show the last image in an array
      currentImage(images.length);
   }
});

The same goes for the $("#next").click function, check the images[i+1] first before you iterate i variable.

Upvotes: 0

Developer
Developer

Reputation: 6440

var i = 0, images=["q","w","e","r","t","y"];

var arrayLength = images.length - 1;
currentImage(0); //show the first photo initially

function currentImage(i) {
    if(i < 0) i = arrayLength;
    if(i > arrayLength) i = 0;
    
    alert(images[i]); //replace this with logic for setting image
}

$('#prev').click(function(){
    i--;
    currentImage(i);
});
$('#next').click(function(){
    i++;
    currentImage(i);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<input type="button" value="previous" id="prev">
<input type="button" value="next" id="next">

Upvotes: 0

ibrahim mahrir
ibrahim mahrir

Reputation: 31692

Check i before passing it to currentImage

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] + ")");
}

$('#prev').click(function(){
    i--;
    if(i < 0) i = arrayLength;
    currentImage(i);
});

$('#next').click(function(){
    i++;
    if(i > arrayLength) i = 0;
    currentImage(i);
});

Upvotes: 1

A. L
A. L

Reputation: 12649

For forward you can use a modulus instead

i = (i+1) % images.length 

This will always return something in the array

For backwards, simply check if it goes into negative, if it does, then go to max

if (i < 0)
{
    i = arrayLength ;
}

Upvotes: 0

Related Questions