Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

188
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda