useIntersectionObserver
Know when an element enters or leaves the viewport without measuring scroll position by hand.
Edit the code and the preview updates as you type.
What it does
A long product page should defer below-the-fold work until a section is close to view, rather than running every image and animation immediately.
The playground is a small, runnable example. Change the code and try the interaction before copying it into an app.
The code
const target = ref(null)
const { stop } = useIntersectionObserver(target, ([entry]) => {
if (entry.isIntersecting) loadMore()
}, { threshold: 0.2 })Where you'd use it
- Lazy-loading images or expensive sections
- Starting a one-time reveal animation
- Triggering the next page in an infinite list
In Nuxt (and other gotchas)
IntersectionObserver is not available on the server. The composable can be called in setup, but the callback runs in the browser after the target exists. Render a stable placeholder so server and initial client markup agree.
When not to use it
For one image, native loading="lazy" is simpler. For exact scroll progress or sticky-position logic, an intersection threshold may not provide enough detail.
Links
- Official useIntersectionObserver docs for every option and type
- Source code on GitHub