useElementVisibility
跟踪元素在视窗中的可见性。
演示
用法
vue
<script setup>
import { useElementVisibility } from '@vueuse/core'
import { ref } from 'vue'
const target = ref(null)
const targetIsVisible = useElementVisibility(target)
</script>
<template>
<div ref="target">
<h1>Hello world</h1>
</div>
</template>
组件用法
此函数还通过
@vueuse/components
包提供了一个无渲染组件版本。 了解有关用法的更多信息.
vue
<template>
<UseElementVisibility v-slot="{ isVisible }">
Is Visible: {{ isVisible }}
</UseElementVisibility>
</template>
指令用法
此函数还通过
@vueuse/components
包提供了一个指令版本。 了解有关用法的更多信息.
vue
<script setup>
import { vElementVisibility } from '@vueuse/components'
import { ref } from 'vue'
const target = ref(null)
const isVisible = ref(false)
function onElementVisibility(state) {
isVisible.value = state
}
</script>
<template>
<div v-element-visibility="onElementVisibility">
{{ isVisible ? 'inside' : 'outside' }}
</div>
<!-- with options -->
<div ref="target">
<div v-element-visibility="[onElementVisibility, { scrollTarget: target }]">
{{ isVisible ? 'inside' : 'outside' }}
</div>
</div>
</template>
类型声明
typescript
export interface UseElementVisibilityOptions
extends ConfigurableWindow,
Pick<UseIntersectionObserverOptions, "threshold"> {
scrollTarget?: MaybeRefOrGetter<HTMLElement | undefined | null>
}
/**
* Tracks the visibility of an element within the viewport.
*
* @see https://vueuse.org.cn/useElementVisibility
*/
export declare function useElementVisibility(
element: MaybeComputedElementRef,
options?: UseElementVisibilityOptions,
): Ref<boolean, boolean>
来源
贡献者
Anthony Fu
Scott Bedard
Anthony Fu
wheat
Amr Bashir
huiliangShen
ziolko-appfire
erikwu
Curt Grimes
vaakian X
sun0day
三咲智子
Jelf
webfansplz
AllenYu
Ary Raditya
Chung, Lian
Carlos Yanes
Alex Kozack
变更日志
v10.8.0
on 2/20/2024v10.7.0
on 12/5/2023v10.4.0
on 8/25/2023v10.0.0-beta.4
on 4/13/20234d757
- feat(types)!: 将 MaybeComputedRef
重命名为 MaybeRefOrGetter
v10.0.0-beta.2
on 3/28/2023v9.11.0
on 1/17/2023v9.2.0
on 9/5/2022