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

186
Visualizações
how insert an index into an array in a loop js

I want to make two tables to identify the position of each image before an animation. I tried several solutions I think the most logical would be a .push() but it doesn't work. Here is a code snippet:

const img = document.querySelectorAll('.myClass img')
let fromRight = []
let fromLeft = []
for (let i = 0; i < img.length; i++) {
  document.addEventListener('load', ()=>{
    if (img[i].getBoundingClientRect().x < (window.scrollY/2)) {
      fromLeft.push(i)
    } 
    else if (img[i].getBoundingClientRect().x > (window.scrollY/2)) {
      fromRight.push(i)
    }
  })
  console.log('left :', fromLeft);
  console.log('right :', fromRight);
}

I also tried:

Array.prototype.push.apply(fromLeft, i)

And :

fromLeft.concat(i)

Please I need help!

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

0

Try wrapping the whole code inside a single listener

document.addEventListener('load', ()=>{
  const img = document.querySelectorAll('.myClass img')
  let fromRight = []
  let fromLeft = []
  for (let i = 0; i < img.length; i++) {
    if (img[i].getBoundingClientRect().x < (window.scrollY/2)) {
      fromLeft.push(i)
    } 
    else if (img[i].getBoundingClientRect().x > (window.scrollY/2)) {
      fromRight.push(i)
    }
  }
  console.log('left :', fromLeft);
  console.log('right :', fromRight);
})

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