Skip to content

useElementVisibility

Category
Export Size
793 B
Last Changed
5 months ago

Tracks the visibility of an element within the viewport.

Demo

Info on the right bottom corner
Target Element (scroll down)
Element inside the viewport

Usage

vue
<script setup lang="ts">
import { 
useElementVisibility
} from '@vueuse/core'
import {
useTemplateRef
} from 'vue'
const
target
=
useTemplateRef
<HTMLDivElement>('target')
const
targetIsVisible
=
useElementVisibility
(
target
)
</script> <template> <
div
ref
="
target
">
<
h1
>Hello world</
h1
>
</
div
>
</template>

rootMargin

If you wish to trigger your callback sooner before the element is fully visible, you can use the rootMargin option (See MDN IntersectionObserver/rootMargin).

ts
const 
targetIsVisible
=
useElementVisibility
(target, {
rootMargin
: '0px 0px 100px 0px',
})

threshold

If you want to control the percentage of the visibility required to update the value, you can use the threshold option (See MDN IntersectionObserver/threshold).

ts
const 
targetIsVisible
= useElementVisibility(target, {
threshold
: 1.0, // 100% visible
})

Component Usage

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

vue
<template>
  <UseElementVisibility v-slot="{ 
isVisible
}">
Is Visible: {{
isVisible
}}
</UseElementVisibility> </template>

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 { 
vElementVisibility
} from '@vueuse/components'
import {
shallowRef
,
useTemplateRef
} from 'vue'
const
target
=
useTemplateRef
<HTMLDivElement>('target')
const
isVisible
=
shallowRef
(false)
function
onElementVisibility
(
state
) {
isVisible
.
value
=
state
} </script> <template> <
div
v-element-visibili
ty="
onElementVisibility
">
{{
isVisible
? 'inside' : 'outside' }}
</
div
>
<!-- with options --> <
div
ref
="
target
">
<
div
v-element-visibili
ty="
[
onElementVisibility
, {
scrollTarget
:
target
}]
">
{{
isVisible
? 'inside' : 'outside' }}
</
div
>
</
div
>
</template>

Type Declarations

ts
export interface UseElementVisibilityOptions
  extends ConfigurableWindow,
    
Pick
<
UseIntersectionObserverOptions
, "threshold"> {
/** * @see https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/rootMargin */
rootMargin
?:
MaybeRefOrGetter
<string>
/** * The element that is used as the viewport for checking visibility of the target. */
scrollTarget
?:
MaybeRefOrGetter
<HTMLElement | undefined | null>
/** * Stop tracking when element visibility changes for the first time * * @default false */
once
?: boolean
} /** * Tracks the visibility of an element within the viewport. * * @see https://vueuse.org/useElementVisibility */ export declare function
useElementVisibility
(
element
:
MaybeComputedElementRef
,
options
?: UseElementVisibilityOptions,
):
ShallowRef
<boolean, boolean>

Source

SourceDemoDocs

Contributors

Anthony Fu
Anthony Fu
IlyaL
Scott Bedard
Dominik Ritter
wheat
Amr Bashir
SerKo
David Gonzalez
IlyaL
huiliangShen
ziolko-appfire
erikwu
Curt Grimes
vaakian X
sun0day
三咲智子
Jelf
webfansplz
AllenYu
Ary Raditya
Chung, Lian
Carlos Yanes
Alex Kozack

Changelog

v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v12.6.0 on
f2f94 - feat: add once options (#4577)
v12.3.0 on
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
v12.1.0 on
3a928 - feat: add rootMargin option (#4100)
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.8.0 on
ce9bb - fix(useElementVisiblity): can configurable threshold (#3715)
v10.7.0 on
07d39 - fix: use last intersection entry (#3365)
v10.4.0 on
429ed - fix: adjust threshold to 0 to fix visibility issue with large element (#3308)
v10.0.0-beta.4 on
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
v10.0.0-beta.2 on
74b00 - fix!: use useIntersectionObserver instead of scroll event handler (#2551)

Released under the MIT License.

FREE WEEKEND
48 Hours of Unlimited Official Vue.js Certification Training
Reserve Your Spot
23-24 August