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

170
Visualizações
Why does this setinterval work only once?

Ok so, on my website I have three long panorama images on top of each other. I want them all to scoot 100px to the left every second. Here's what I tried:

var panoramas = document.getElementsByClassName("pano");

setInterval(scoot, 1000);

function scoot(){ 
    for (i=0; i < panoramas.length; i++){ 
        panoramas[i].style.left = panoramas[i].style.left - 100; 
    }
}

But what happens is that they scoot 100px to the left once, and stop. I tried some console.logs in there so I know it's looping, but they won't move more than once.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

panoramas[i].style.left does not read styles from CSS files. It returns a string with unit and expects a string with unit.

var panoramas = document.getElementsByClassName("pano");

setInterval(scoot, 1000);

function scoot() {
  for (i = 0; i < panoramas.length; i++) {
    panoramas[i].style.left = panoramas[i].style.left.replace(/\d+/, p1 => +p1 - 100);
  }
}
.pano {
  display: block;
  width: 50px;
  height: 50px;
  background-color: blue;
  position: absolute;
}
<div class="pano" style="left: 200px; top: 50px;"></div>
<div class="pano" style="left: 250px; top: 100px;"></div>
<div class="pano" style="left: 300px; top= 150px;"></div>

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