Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!