Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useIntersectionObserver.md
1---2category: Elements3---45# useIntersectionObserver67Detects that a target element's visibility.89## Usage1011```vue12<script setup lang="ts">13import { useIntersectionObserver } from '@vueuse/core'14import { shallowRef, useTemplateRef } from 'vue'1516const target = useTemplateRef('target')17const targetIsVisible = shallowRef(false)1819const { stop } = useIntersectionObserver(20target,21([entry], observerElement) => {22targetIsVisible.value = entry?.isIntersecting || false23},24)25</script>2627<template>28<div ref="target">29<h1>Hello world</h1>30</div>31</template>32```3334## Directive Usage3536```vue37<script setup lang="ts">38import { vIntersectionObserver } from '@vueuse/components'39import { shallowRef, useTemplateRef } from 'vue'4041const root = useTemplateRef('root')4243const isVisible = shallowRef(false)4445function onIntersectionObserver([entry]: IntersectionObserverEntry[]) {46isVisible.value = entry?.isIntersecting || false47}48</script>4950<template>51<div>52<p>53Scroll me down!54</p>55<div v-intersection-observer="onIntersectionObserver">56<p>Hello world!</p>57</div>58</div>5960<!-- with options -->61<div ref="root">62<p>63Scroll me down!64</p>65<div v-intersection-observer="[onIntersectionObserver, { root }]">66<p>Hello world!</p>67</div>68</div>69</template>70```7172[IntersectionObserver MDN](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver)7374## Type Declarations7576```ts77export interface UseIntersectionObserverOptions extends ConfigurableWindow {78/**79* Start the IntersectionObserver immediately on creation80*81* @default true82*/83immediate?: boolean84/**85* The Element or Document whose bounds are used as the bounding box when testing for intersection.86*/87root?: MaybeComputedElementRef | Document88/**89* A string which specifies a set of offsets to add to the root's bounding_box when calculating intersections.90*/91rootMargin?: MaybeRefOrGetter<string>92/**93* Either a single number or an array of numbers between 0.0 and 1.94* @default 095*/96threshold?: number | number[]97}98export interface UseIntersectionObserverReturn extends Supportable, Pausable {99stop: () => void100}101/**102* Detects that a target element's visibility.103*104* @see https://vueuse.org/useIntersectionObserver105* @param target106* @param callback107* @param options108*/109export declare function useIntersectionObserver(110target:111| MaybeComputedElementRef112| MaybeRefOrGetter<MaybeElement[]>113| MaybeComputedElementRef[],114callback: IntersectionObserverCallback,115options?: UseIntersectionObserverOptions,116): UseIntersectionObserverReturn117```118