Skip to content

useElementVisibility

Category
Export Size
796 B
Last Changed
3 days 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
('target')
const
targetIsVisible
=
useElementVisibility
(
target
)
const
target2
=
useTemplateRef
('target2')
const
targetVisibilityController
=
useElementVisibility
(
target2
, {
controls
: true })
</script> <template> <
div
ref
="
target
">
<
h1
>Hello world</
h1
>
</
div
>
<
div
ref
="
target2
">
<
h1
>Hi there</
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
('target')
const
isVisible
=
shallowRef
(false)
function
onElementVisibility
(
state
) {
isVisible
.
value
=
state
} const
target2
=
useTemplateRef
('target2')
const
isVisible2
=
shallowRef
(false)
function
onElementVisibilityWithControls
(
state
) {
isVisible2
.
value
=
state
.isVisible.value
if (
state
.isVisible.value) {
state
.stop()
} } </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
>
<!-- with controls --> <
div
ref
="
target2
">
<
div
v-element-visibili
ty="
[
onElementVisibilityWithControls
, {
controls
: true }]
">
{{
isVisible2
? 'inside' : 'outside' }}
</
div
>
</
div
>
</template>

Type Declarations

Show Type Declarations
ts
export interface 
UseElementVisibilityOptions
<
Controls
extends boolean = false>
extends ConfigurableWindow,
Pick
<
UseIntersectionObserverOptions
, "rootMargin" | "threshold"> {
/** * Initial value. * * @default false */
initialValue
?: boolean
/** * The element that is used as the viewport for checking visibility of the target. */
scrollTarget
?:
UseIntersectionObserverOptions
["root"]
/** * Stop tracking when element visibility changes for the first time * * @default false */
once
?: boolean
/** * Expose more controls * * @default false */
controls
?:
Controls
} export type
UseElementVisibilityReturn
<
Controls
extends boolean = false> =
Controls
extends true
? UseElementVisibilityReturnWithControls :
ShallowRef
<boolean>
export interface UseElementVisibilityReturnWithControls extends UseIntersectionObserverReturn {
isVisible
:
ShallowRef
<boolean>
} /** * Tracks the visibility of an element within the viewport. * * @see https://vueuse.org/useElementVisibility */ export declare function
useElementVisibility
(
element
:
MaybeComputedElementRef
,
options
?:
UseElementVisibilityOptions
<false>,
):
UseElementVisibilityReturn
<false>
export declare function
useElementVisibility
(
element
:
MaybeComputedElementRef
,
options
?:
UseElementVisibilityOptions
<true>,
):
UseElementVisibilityReturn
<true>

Source

SourceDemoDocs

Contributors

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

Changelog

Pending for release...
0cb03 - feat: add controls option (#5191)
v14.2.0 on
46682 - feat: inherit rootMargin from useIntersectionObserver (#5207)
v14.1.0 on
13f36 - feat: add initialValue option (#5159)
v14.0.0-alpha.0 on
8c521 - feat(components)!: refactor components and make them consistent (#4912)
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)

Released under the MIT License.

The new era of AI engineering.
Workflows, artifacts, and judgment that scale.
Request Early Access