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

94
Vistas
IntersectionObserver triggers before element enters view

Using Vue, I have the following setup in a component:

<template>
  <article>
    <!-- This content spans several viewport heights: you *have* to scroll to get to the bottom -->
    {{ content }}
  </article>

  <span ref="readMarker" />
</template>
// Composition API with <script setup>

const emit = defineEmit<{
  (e: "read", value: number): void
}>()

const readMarker = ref<HTMLSpanElement>()

let observer: IntersectionObserver;
onMounted(() => {
  if (!readMarker.value)
    return

  observer = new IntersectionObserver(
      () => {
        emit("read", 1)
      },
      {
        root: readMarker.value,
      },
  )
  observer.observe(readMarker.value)
})

onDeactivated(() => observer.disconnect())

The idea is that when the user scrolls to the bottom of the content (rootMarker enters the view), the read event is emitted. When the parent receives the event, it loads another component like this and so on (@read.once="loadMore").

However, as soon as I load the page, the event fires immediately, without me having any time to scroll at all. The same event for subsequent instances also fire as soon as the each instance loads.

I also tried using null as the root and creating the observer outside onMounted (with onMounted only containing the observer.observe() call), but that changed nothing.

I have clearly misunderstood something about IntersectionObserver, but I'm really confused about this behaviour.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

It turns out that if you want to observe scrolling on the viewport itself, the root of the IntersectionObserver has to be the document itself, whereas in my question I am trying to set readMarker.value as root, so the observer would only look in it and, of course see it as "visible" right away.

over 4 years ago · Santiago Trujillo 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