0

I have an Image carousel that contains three images. I have next and previous buttons to click through the array of images, but I would like it to go back to the first image when clicking 'next' on the last image in the array (and to the last one when clicking 'previous' on the first image). Is there a way I can use a loop to make this work?

var images = ['image1.jpg', 'image2.jpg', 'image3.jpg'];
var currentImg = 0;

document.querySelector('.carousel>img').src = 'images/' + images[0];

document.querySelector('.carousel').addEventListener('click', function (evt){
var target = evt.target;
if (target.classList.contains('control')) {

    if (target.classList.contains('next')) {
    //move to the next index in the array 
        currentImg += 1;

    } else if (target.classList.contains('prev')){
    // move to the previous index in the array
        currentImg -= 1;
    } 
    // display the new current image
    document.querySelector('.carousel>img').src = 'images/'
        + images[currentImg];
    }
});

I was able to do the following and it did what I wanted:

    if (target.classList.contains('next')) {
       if(currentImg==2){
           currentImg=0
      } else {
        currentImg += 1;
      }

    } else if (target.classList.contains('prev')){
       if(currentimg==0){
           currentimg=2
      } else { 
        currentImg -= 1;
    } 

Thank you for all the other suggestions I will look into those as well!

2
  • Welcome to Stack Overflow! What would help would be some HTML code... it would just be easier for us users to answer your question :). Commented Mar 2, 2016 at 21:04
  • The question is perfectly answerable without the HTML. Commented Mar 2, 2016 at 21:05

3 Answers 3

1

Since you want to go both ways (+ and -), consider:

var fac = target.classList.contains('next')? 1 : -1;
currentImage = (currentImage + fac + images.length) % images.length;

so if currentImage reaches images.length it wraps to the first image and if it goes to -1, it "wraps" to imaages.length - 1, i.e. the last image.

Sign up to request clarification or add additional context in comments.

Comments

0

Next:

++currentImg % 3

Prev:

--currentImg % 3

If needed replace 3 with whatever the size of the Array.

7 Comments

Or use images.length in place of 3 to make you code future proof.
@RobG I think javascript and php replace negative indexes with length-index , or count from other end.
@ArifBurhan nope, they don't.
@ArifBurhan— currentImage is just a number, it has no special properties.
|
0

Add this piece of code after you've either incremented or decremented currentImg and you should be fine:

if (currentImg < 0) { 
  currentImg = images.length - 1;
} else if (currentImg >= images.length) {
  currentImg = 0;
}

This checks to see if currentImg has "escaped" the images array boundaries.

1 Comment

or could use ternary operator here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.