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

397
Visualizações
intersection oberserver used with a loop through all the elements ends in error

I want to show some cards on scroll and for that purpose I chose to use an intersection observer. But I ended up with:
Uncaught TypeError: Failed to execute 'observe' on 'IntersectionObserver': parameter 1 is not of type 'Element'.
Here is a sample code of what I am trying to do =>

HTML:

<div class="card">demo</div>
<div class="card">demo</div>
<div class="card">demo</div>
<div class="card">demo</div>

JS:

const cards = document.querySelectorAll(".card");

const options = {
    root: null,
    threshold: 0.2
}

const observer = new IntersectionObserver(function(entries, observer) {
    entries.forEach(entry => {
        if (entry.isIntersecting) {
            entry.target.classList.add("appear-on-scroll");
            observer.unobserve(entry.target);
        } else {
            return;
        }
    });
}, options);
observer.observe(cards);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The answer is to loop through the array that consists of nodes twice like:

const cards = document.querySelectorAll(".card");

const options = {
    root: null,
    threshold: 0.2
}

const observer = new IntersectionObserver(function(entries, observer) {
    entries.forEach(entry => {
        if (entry.isIntersecting) {
            entry.target.classList.add("appear-on-scroll");
            observer.unobserve(entry.target);
        } else {
            return;
        }
    });
}, options);

// second loop through
cards.forEach(card => observer.observe(card));
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