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

396
Vistas
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 Respuestas
Responde la pregunta

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 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