Skip to content

useElementVisibility

类别
导出体积
796 B
上次更改
last week

跟踪元素在视口内的可见性。

示例

右下角的信息
目标元素(向下滚动)
元素 外部 视口

用法

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

如果你想在元素完全可见之前更早地触发回调,可以使用 rootMargin 选项(参见 MDN IntersectionObserver/rootMargin)。

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

threshold

如果您想控制更新值所需的可见性百分比,可以使用 threshold 选项(请参见 MDN IntersectionObserver/threshold)。

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

组件用法

vue
<template>
  <UseElementVisibility v-slot="{ 
isVisible
}">
是否可见:{{
isVisible
}}
</UseElementVisibility> </template>

指令用法

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
? '在内部' : '在外部' }}
</
div
>
<!-- 使用选项 --> <
div
ref
="
target
">
<
div
v-element-visibili
ty="
[
onElementVisibility
, {
scrollTarget
:
target
}]
">
{{
isVisible
? '在内部' : '在外部' }}
</
div
>
</
div
>
<!-- 带控制 --> <
div
ref
="
target2
">
<
div
v-element-visibili
ty="
[
onElementVisibilityWithControls
, {
controls
: true }]
">
{{
isVisible2
? 'inside' : 'outside' }}
</
div
>
</
div
>
</template>

类型声明

显示类型声明
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>
} /** * 跟踪元素在视口中的可见性 * * @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>

源码

源码演示文档

贡献者

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

更新日志

Pending for release...
0cb03 - feat: add controls option (#5191)
46682 - feat: inherit rootMargin from useIntersectionObserver (#5207)
13f36 - feat: add initialValue option (#5159)
8c521 - feat(components)!: refactor components and make them consistent (#4912)
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
f2f94 - feat: add once options (#4577)
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
3a928 - feat: add rootMargin option (#4100)
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
ce9bb - fix(useElementVisiblity): can configurable threshold (#3715)
07d39 - fix: use last intersection entry (#3365)