Skip to content

useIntersectionObserver

Category
Export Size
646 B
Last Changed
last month

Detects changes to a target element's visibility.

Demo

RootMargin: 0px 0px 0px 0px

Scroll me down!

Hello world!

Element outside the viewport

Usage

vue
<script setup lang="ts">
import { 
useIntersectionObserver
} from '@vueuse/core'
import {
shallowRef
,
useTemplateRef
} from 'vue'
const
target
=
useTemplateRef
('target')
const
targetIsVisible
=
shallowRef
(false)
const {
stop
} =
useIntersectionObserver
(
target
,
([
entry
],
observerElement
) => {
targetIsVisible
.
value
=
entry
?.
isIntersecting
|| false
}, ) </script> <template> <
div
ref
="
target
">
<
h1
>Hello world</
h1
>
</
div
>
</template>

Controls and cleanup

useIntersectionObserver returns controls for the underlying observer:

StateTypeDescription
isSupportedComputedRef<boolean>Whether the IntersectionObserver API is available.
isActiveShallowRef<boolean>Whether the observer is currently running. Turns false after pause() or stop().
pause() => voidPause observing and set isActive to false.
resume() => voidResume observing.
stop() => voidStop observing permanently.

The observer is disconnected automatically via tryOnScopeDispose when the component or effect scope that created it is disposed, so in most cases you don't need to call stop yourself. Call stop() to disconnect the observer earlier, for example once the element has become visible:

ts
const { 
stop
} =
useIntersectionObserver
(
target, ([
entry
]) => {
if (
entry
?.
isIntersecting
) {
// react to the element becoming visible once, then stop observing
stop
()
} }, )
js
'use strict'
const { stop } = useIntersectionObserver(target, ([entry]) => {
  if (entry?.isIntersecting) {
    // react to the element becoming visible once, then stop observing
    stop()
  }
})

Directive Usage

This function also provides a directive version via the @vueuse/components package. Learn more about the usage.

vue
<script setup lang="ts">
import { 
vIntersectionObserver
} from '@vueuse/components'
import {
shallowRef
,
useTemplateRef
} from 'vue'
const
root
=
useTemplateRef
('root')
const
isVisible
=
shallowRef
(false)
function
onIntersectionObserver
([
entry
]: IntersectionObserverEntry[]) {
isVisible
.
value
=
entry
?.
isIntersecting
|| false
} </script> <template> <
div
>
<
p
>
Scroll me down! </
p
>
<
div
v-intersection-observ
er="
onIntersectionObserver
">
<
p
>Hello world!</
p
>
</
div
>
</
div
>
<!-- with options --> <
div
ref
="
root
">
<
p
>
Scroll me down! </
p
>
<
div
v-intersection-observ
er="
[
onIntersectionObserver
, {
root
}]
">
<
p
>Hello world!</
p
>
</
div
>
</
div
>
</template>

IntersectionObserver MDN

Type Declarations

Show Type Declarations
ts
export interface UseIntersectionObserverOptions extends ConfigurableWindow {
  /**
   * Start the IntersectionObserver immediately on creation
   *
   * @default true
   */
  
immediate
?: boolean
/** * The Element or Document whose bounds are used as the bounding box when testing for intersection. */
root
?:
MaybeComputedElementRef
| Document
/** * A string which specifies a set of offsets to add to the root's bounding_box when calculating intersections. */
rootMargin
?:
MaybeRefOrGetter
<string>
/** * Either a single number or an array of numbers between 0.0 and 1. * @default 0 */
threshold
?: number | number[]
} export interface UseIntersectionObserverReturn extends Supportable, Pausable {
stop
: () => void
} /** * Detects changes to a target element's visibility. * * @see https://vueuse.org/useIntersectionObserver * @param target * @param callback * @param options */ export declare function
useIntersectionObserver
(
target
:
MaybeComputedElementRefOrArray
,
callback
: IntersectionObserverCallback,
options
?: UseIntersectionObserverOptions,
): UseIntersectionObserverReturn

Source

SourceDemoDocs

Contributors

Anthony Fu
Anthony Fu
IlyaL
Vida Xie
Alex Liu
远方os
Jelf
webfansplz
Cristian Cananau
Eldar Dadashov
Jialong Lu
Sam Blowes
E66
Yu Lia
Kevin Luo
IlyaL
我想静静
cyril
Hongkun
Sma11X
schelmo
Fernando Fernández
Curt Grimes
Waleed Khaled
Hassan Zahirnia
karma
Shinigami
Alex Kozack
Jacob Clevenger
Antério Vieira
Evgeniy Gavrilov
听风吟丶

Changelog

bccb1 - fix: accept array template ref bound to v-for as observer target (#5514)
52d68 - fix(directive): create disposable directive func cleanup of side effects unmounted (#5244)
1d9c4 - fix(docs): typos in useManualRefHistory, useFocusWithin, useStorageAsync, useIntersectionObserver (#5329)
53abe - feat: make rootMargin reactive (#4934)
8c521 - feat(components)!: refactor components and make them consistent (#4912)
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
021d0 - feat(toArray): new utility function (#4432)
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
6b584 - fix: add Document type for root (#4210)
13e36 - fix!: update the threshold default to 0 (#4069)

Released under the MIT License.