Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

185
Visualizações
Why do I need a counter for the images in a slide carousel and why is it so fundamental to get the images moving the right way?

I'm following a tutorial on how to make an image carousel slider. I tried breaking down the problem on pen and paper but I never knew I would need a counter for the images. I feel like I'm just copying the code and not really understanding it. If someone could explain this to me that would be great.

const slideContainer = document.querySelector(".slide")
const carouselImages = document.querySelectorAll(".carousel-container img")
const lastCloneImage = document.querySelector("#last-clone-image")
const firstCloneImage = document.querySelector("#first-clone-image")
const prevArrowButton = document.querySelector("#prev")
const nextArrowButton = document.querySelector("#next")

let counter = 1;
const size = carouselImages[0].clientWidth

nextArrowButton.addEventListener("click", imageNextMove)
prevArrowButton.addEventListener("click", imagePrevMove)

// this selects the slide with the images in it and moves the container itself with all the images in it 
slideContainer.style.transform = 'translateX(' + (-size * counter) + 'px)';

function imageNextMove() {
    slideContainer.style.transition = 'transform 0.4s ease-in-out'
    counter++;
    slideContainer.style.transform = 'translateX(' + (-size * counter) + 'px)';


}
function imagePrevMove() {
    slideContainer.style.transition = 'transform 0.4s ease-in-out'
    counter--;
    slideContainer.style.transform = 'translateX(' + (-size * counter) + 'px)';

}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda