Search code examples
javascripthtmlcsscarousel

Javascript: Carousel active dots and left-right buttons


I am beginner level javascript developer. I have decide to make carousel by using pure Javascript. I successfully manage to slide the carousel. I have decided to make active dots(also clickable), i want the dots background colour change when slide happen and left-right button. I have created the dots but I don't know how to implement the dots in my slide function as well left and right button. Here is my code:

Also please explain me the logic. Thank you

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

function run() {
  const dot = [...document.getElementsByClassName('star')];
  index++;


  if (index > allImages.length - 1) {
    index = 0
    dot.forEach(i => i.classList.add('active'))
  }
  imgs.style.transform = `translateX(${-index * 500}px)`

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

let x = setInterval(run, 2000);

images.onmouseover = () => {
  clearInterval(x)
}
images.onmouseout = () => {
  x = setInterval(run, 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>


Solution

  • You were close to the result. In your run function, you just have to change the class of the current active dot and set the new one to active. I renamed your dot variable to dots for the sake of comprehension.

    function run() {
      const dots = [...document.getElementsByClassName('star')];
      index++;
    
    
      if (index > allImages.length - 1) {
        index = 0
      }
      
      dots.forEach((dot, i) => {
        if (dot.classList.contains('active')) dot.classList.remove('active');
        if (i === index) dot.classList.add('active'));
      }
      imgs.style.transform = `translateX(${-index * 500}px)`
    
    }