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

173
Vistas
loop through elements and set CSS styling at same time

So I want to loop through elements with a class and then loop the individual element and gradually decrease the "left" css property.

let move = document.getElementsByClassName("move");
for (var i = 0; i < move.length; i++) {
  const left = move[i].getBoundingClientRect().left;
  const elementId = move[i].id;
  for (let j = left; j > -20; j--) {
    document.getElementById(elementId).style.left = j + "%";
    await new Promise(resolve => setTimeout(resolve, 20));
  }
}

However, I'm using "await" to delay so that it moves slowly and doesn't zip across the screen. I want it so that all the elements have their CSS left property decrease at the same time. But instead, because of the await, the first element increases its left property and when its left property reaches the end, only then the next element goes. Please advise

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

While I think it would be better to use pure CSS transitions/animations for this, you could use a while loop to iterate over the elements with the move class, subtracting 1% from their left value until they are completely off screen (right <= 0).

const start = async () => {
  const move = document.getElementsByClassName("move");

  const canMoveLeft = () => {
    const move = document.getElementsByClassName("move");
    for (let i = 0; i < move.length; i++) {
      if (move[i].getBoundingClientRect().right > 0) return true;
    }
    return false;
  };
  
  while (canMoveLeft()) {
    for (let i = 0; i < move.length; i++) {
      const { left, right } = move[i].getBoundingClientRect();
      if (right > 0) {
        const windowWidth = window.innerWidth;
        const leftVal = `${Math.round((left / windowWidth) * 100) - 1}%`;
        document.getElementById(move[i].id).style.left = leftVal;
      }
    }

    await new Promise((resolve) => setTimeout(resolve, 20));
  }
};

Edit loop-through-elements-and-set-css-styling-at-same-time

about 4 years ago · Juan Pablo Isaza Denunciar

0

From what I understood, you want to animate left property of elements with class "move".

If that's the case you can do:

1st:

add transition property in move class

.move {
 transition: left 2s;
}

2nd:

let move = document.getElementsByClassName("move");
for(let item of move) {
 item.style.left = "-20%";
}
about 4 years ago · Juan Pablo Isaza Denunciar
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