Skip to content

useElementVisibility

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

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

示例

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

用法

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

如果你想在元素完全可见之前更早地触发回调,可以使用 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% visible
})

组件用法

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
<HTMLDivElement>('target')
const
isVisible
=
shallowRef
(false)
function
onElementVisibility
(
state
) {
isVisible
.
value
=
state
} </script> <template> <
div
v-element-visibili
ty="
onElementVisibility
">
{{
isVisible
? '在内部' : '在外部' }}
</
div
>
<!-- 使用选项 --> <
div
ref
="
target
">
<
div
v-element-visibili
ty="
[
onElementVisibility
, {
scrollTarget
:
target
}]
">
{{
isVisible
? '在内部' : '在外部' }}
</
div
>
</
div
>
</template>

类型声明

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
} /** * 跟踪元素在视口中的可见性 * * @see https://vueuse.org/useElementVisibility */ export declare function
useElementVisibility
(
element
:
MaybeComputedElementRef
,
options
?: UseElementVisibilityOptions,
):
ShallowRef
<boolean, boolean>
export type
UseElementVisibilityReturn
=
ReturnType
<typeof
useElementVisibility
>

源码

源码演示文档

贡献者

Anthony Fu
一纸忘忧
Anthony Fu
IlyaL
Scott Bedard
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

更新日志

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)
429ed - fix: adjust threshold to 0 to fix visibility issue with large element (#3308)