Skip to main content
Vuetify0 is now in beta!
Vuetify0 Logo
Theme
Mode
Palettes
Accessibility
Vuetify One
Sign in to Vuetify One

Access premium tools across the Vuetify ecosystem — Bin, Play, Studio, and more.

Not a subscriber? See what's included

useIntersectionObserver

A composable for detecting when elements enter or leave the viewport using the Intersection Observer API with automatic cleanup.

Usage

The useIntersectionObserver composable wraps the Intersection Observer API to detect when elements become visible in the viewport. It’s useful for lazy loading images, infinite scroll, entrance animations, and performance optimizations.

Tip

Why wrap IntersectionObserver? The native IntersectionObserver has no awareness of Vue’s effectScope lifecycle. If you create one inside a composable, it won’t automatically disconnect when the scope is disposed. useIntersectionObserver integrates onScopeDispose for automatic cleanup, defers creation until after hydration for SSR safety, and adds reactive target tracking — things the native API can’t do on its own.

vue
<script setup lang="ts">
  import { useIntersectionObserver } from '@vuetify/v0'
  import { ref, useTemplateRef } from 'vue'

  const target = useTemplateRef('target')
  const isVisible = ref(false)

  useIntersectionObserver(target, (entries) => {
    isVisible.value = entries[0].isIntersecting
  }, {
    threshold: 0.5, // Trigger when 50% visible
    rootMargin: '0px'
  })
</script>

<template>
  <div>
    <div style="height: 100vh">Scroll down to see the element</div>
    <div ref="target" :class="{ visible: isVisible }">
      I'm {{ isVisible ? 'visible' : 'hidden' }}
    </div>
  </div>
</template>

Architecture

useIntersectionObserver wraps the native IntersectionObserver API with Vue reactivity:

Options

OptionTypeDefaultNotes
immediatebooleanfalseFire the callback immediately on mount before any intersection
oncebooleanfalseStop observing after the first intersection fires
thresholdnumber | number[]0Intersection ratio(s) at which the callback fires
rootElement | nullnullAncestor to use as viewport (null = browser viewport)
rootMarginstring'0px'CSS margin around the root for intersection calculations

Reactivity

Property/MethodReactiveNotes
isActiveComputed from observer ref
isIntersectingShallowRef, readonly
isPausedShallowRef, readonly
targetAccepts MaybeRef, watched for changes
pause()Temporarily stop observing without disconnecting
resume()Resume after pause()
stop()Disconnect the observer permanently

useElementIntersection

Property/MethodReactiveNotes
isIntersectingShallowRef, readonly
intersectionRatioShallowRef, readonly (0.0 to 1.0)

Examples

API Reference

The following API details are for the useIntersectionObserver composable.
Was this page helpful?

Ctrl+/