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

95
Visualizações
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 Respostas
Responde à pergunta

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