Krisna
Krisna

Reputation: 3423

Carousel prev and next button logic does not work

I am trying to a make carousel using pure Javascript. I successfully manage to slide the carousel and have created left and right buttons.

I took my slide functions and added them to the button on-click event-listener, but I have problems when I implement the function on my buttons. It does not behave as expected. My code is below, how can I fix this?

const images = document.getElementById('imgs'); //here
const allImages = document.querySelectorAll('#imgs img');
const leftBtn = document.getElementById('left');
const rightBtn = document.getElementById('right');
let index = 0;

function changeSliderPage() {
  const dot = [...document.getElementsByClassName('star')];

  index++;

  if (index > allImages.length - 1) {
    index = 0
  }

  imgs.style.transform = `translateX(${-index * 500}px)`;

  dot.forEach((dot, i) => {
    if (i === index) {
      dot.classList.add('active')
    } else {
      dot.classList.remove('active')
    }
  });

};

allImages.forEach(i => {
  const elem = document.createElement('div');
  elem.classList.add('star');
  document.body.appendChild(elem)
});




rightBtn.onclick = () => {
  changeSliderPage(index + 1); 
}
leftBtn.onclick = () => {
 changeSliderPage(index - 1);
}

let x = setInterval(changeSliderPage, 100000);


images.onmouseover = () => {
  clearInterval(x)
}

images.onmouseout = () => {
  x = setInterval(changeSliderPage, 2000);
}
*{
  box-sizing: border-box;
}

body {
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}

.carousel {
  overflow: hidden;
  width: 500px;
  height: 500px;
  box-shadow: 2px 2px 5px rgba(0, 0, 0, .3);
  border-radius: 5px;
}

.image-container {
  display: flex;
 transition: transform 300ms linear;
 transform: translateX(0);
}

img {
  width:500px;
  height: 500px;
  object-fit: cover;
}

.star{
  cursor: pointer;
  height: 15px;
  width: 15px;
  margin: 0 10px;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
  background-color: #eeeeee;

}
.star.active{
  background-color: red;
}
button{
  cursor: pointer;
  position: relative;
  font-size: 18px;
  transition: 0.6s ease;
  user-select: none;
  height: 50px;
  width: 40px;
  display: flex;
  justify-content: center;
  align-items: center;
  align-content: center;
  top: calc(50% - 25px);
}
button:hover {
  background-color: rgba(0,0,0,0.8);
};

button.left {
  border-radius: 3px 0 0 3px;
  right: 0;
}
button.left {
  border-radius: 3px 0 0 3px;
  left: 0;
}
<button id="left">&#10094;</button>
  <button id="right">&#10095;</button>

  <div class="carousel">
    <div class="image-container" id="imgs" >
      <img src="https://images.unsplash.com/photo-1599736375341-51b0a848f3c7?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60" alt="">
      <img src="https://images.unsplash.com/photo-1516026672322-bc52d61a55d5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60" alt="">
      <img src="https://images.unsplash.com/photo-1573081586928-127ecc7948b0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60" alt="">
      <img src="https://images.unsplash.com/flagged/photo-1572850005109-f4ac7529bf9f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60" alt="">
    </div>
  </div>

Upvotes: 0

Views: 455

Answers (1)

Kacper Witas
Kacper Witas

Reputation: 46

Logic that I use with carousels:

for example you have 4 images: [1][2][3][4]

I have an animation for sliding every image, I add 5th image which is same as image no 1: [1][2][3][4][1]

Imagine cursor which shows what image is currently displayed, Ill mark cursor as ! ! So at begin: [!1!][2][3][4][1]

Now the slider moves on... [1][!2!][3][4][1]

etc...

It moves to last image: [1][2][3][4][!1!]

And now it has to move under the hood from last image to first image, but without any animation so the whole change is not visible by user:

[!1!][2][3][4][5]

This way you can get inifinite carousel, just need to check in javascript if current image is last one and you want to slide right -> no animation. Same if you are on 1st image and want to slide left.

Upvotes: 1

Related Questions