跳到内容

useIntersectionObserver

类别
导出大小
642 B
最后更改
5 天前

检测目标元素的可见性。

演示

向下滚动我!

你好,世界!

元素 在视口外

用法

vue
<script setup lang="ts">
import { useIntersectionObserver } from '@vueuse/core'
import { shallowRef, useTemplateRef } from 'vue'

const target = useTemplateRef<HTMLDivElement>('target')
const targetIsVisible = shallowRef(false)

const { stop } = useIntersectionObserver(
  target,
  ([entry], observerElement) => {
    targetIsVisible.value = entry?.isIntersecting || false
  },
)
</script>

<template>
  <div ref="target">
    <h1>Hello world</h1>
  </div>
</template>

指令用法

此函数还通过 @vueuse/components 包提供了一个指令版本。 了解更多关于用法的信息

vue
<script setup lang="ts">
import { vIntersectionObserver } from '@vueuse/components'
import { shallowRef, useTemplateRef } from 'vue'

const root = useTemplateRef<HTMLDivElement>('root')

const isVisible = shallowRef(false)

function onIntersectionObserver([entry]: IntersectionObserverEntry[]) {
  isVisible.value = entry?.isIntersecting || false
}
</script>

<template>
  <div>
    <p>
      Scroll me down!
    </p>
    <div v-intersection-observer="onIntersectionObserver">
      <p>Hello world!</p>
    </div>
  </div>

  <!-- with options -->
  <div ref="root">
    <p>
      Scroll me down!
    </p>
    <div v-intersection-observer="[onIntersectionObserver, { root }]">
      <p>Hello world!</p>
    </div>
  </div>
</template>

IntersectionObserver MDN

类型声明

显示类型声明
typescript
export interface UseIntersectionObserverOptions extends ConfigurableWindow {
  /**
   * Start the IntersectionObserver immediately on creation
   *
   * @default true
   */
  immediate?: boolean
  /**
   * The Element or Document whose bounds are used as the bounding box when testing for intersection.
   */
  root?: MaybeComputedElementRef | Document
  /**
   * A string which specifies a set of offsets to add to the root's bounding_box when calculating intersections.
   */
  rootMargin?: string
  /**
   * Either a single number or an array of numbers between 0.0 and 1.
   * @default 0
   */
  threshold?: number | number[]
}
export interface UseIntersectionObserverReturn extends Pausable {
  isSupported: ComputedRef<boolean>
  stop: () => void
}
/**
 * Detects that a target element's visibility.
 *
 * @see https://vueuse.org.cn/useIntersectionObserver
 * @param target
 * @param callback
 * @param options
 */
export declare function useIntersectionObserver(
  target:
    | MaybeComputedElementRef
    | MaybeRefOrGetter<MaybeElement[]>
    | MaybeComputedElementRef[],
  callback: IntersectionObserverCallback,
  options?: UseIntersectionObserverOptions,
): UseIntersectionObserverReturn

来源

Source演示文档

贡献者

Anthony Fu
Anthony Fu
IlyaL
Alex Liu
远方os
Jelf
webfansplz
我想静静
cyril
Hongkun
Sma11X
schelmo
Fernando Fernández
Curt Grimes
Waleed Khaled
Hassan Zahirnia
karma
Shinigami
Alex Kozack
Jacob Clevenger
Antério Vieira
Evgeniy Gavrilov
听风吟丶

更新日志

v12.8.0 on 3/5/2025
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v12.3.0 on 1/2/2025
021d0 - feat(toArray): new utility function (#4432)
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
v12.0.0-beta.1 on 11/21/2024
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v11.1.0 on 9/16/2024
6b584 - fix: add Document type for root (#4210)
v11.0.0-beta.2 on 7/17/2024
13e36 - fix!: update the threshold default to 0 (#4069)
v10.0.2 on 4/14/2023
7d001 - fix: module reference, close #2972
v10.0.1 on 4/14/2023
b95b6 - fix: targets length check (#2968)
v10.0.0 on 4/14/2023
f87f8 - feat: allow multiple targets (#2964)
v10.0.0-beta.4 on 4/13/2023
4b336 - feat: support for Pausable interface (#2883)
v10.0.0-beta.2 on 3/28/2023
74b00 - fix(useElementVisibility)!: use useIntersectionObserver instead of scroll event handler (#2551)

在 MIT 许可证下发布。