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

172
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
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