跳到主要內容

useIntersectionObserver

Category
匯出大小
642 B
Last Changed
5 days ago

Detects that a target element's visibility.

Demo

Scroll me down!

Hello world!

Element outside the viewport

Usage

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>

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 { 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

Type Declarations

Show Type Declarations
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.dev.org.tw/useIntersectionObserver
 * @param target
 * @param callback
 * @param options
 */
export declare function useIntersectionObserver(
  target:
    | MaybeComputedElementRef
    | MaybeRefOrGetter<MaybeElement[]>
    | MaybeComputedElementRef[],
  callback: IntersectionObserverCallback,
  options?: UseIntersectionObserverOptions,
): UseIntersectionObserverReturn

Source

原始碼Demo文件

貢獻者

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 於 2025/3/5
7432f - feat(types): 棄用 MaybeRefMaybeRefOrGetter,改用 Vue 原生寫法 (#4636)
v12.3.0 於 2025/1/2
021d0 - feat(toArray): 新增工具函式 (#4432)
59f75 - feat(toValue): 棄用 @vueuse/shared 中的 toValue,改用 Vue 原生寫法
v12.0.0-beta.1 於 2024/11/21
0a9ed - feat!: 移除 Vue 2 支援,最佳化 bundles 並清理程式碼 (#4349)
v11.1.0 於 2024/9/16
6b584 - fix: 為 root 新增 Document 類型 (#4210)
v11.0.0-beta.2 於 2024/7/17
13e36 - fix!: 更新 threshold 預設值為 0 (#4069)
v10.0.2 於 2023/4/14
7d001 - fix: 模組參考,關閉 #2972
v10.0.1 於 2023/4/14
b95b6 - fix: targets 長度檢查 (#2968)
v10.0.0 於 2023/4/14
f87f8 - feat: 允許使用多個 targets (#2964)
v10.0.0-beta.4 於 2023/4/13
4b336 - feat: 支援 Pausable 介面 (#2883)
v10.0.0-beta.2 於 2023/3/28
74b00 - fix(useElementVisibility)!: 使用 useIntersectionObserver 取代 scroll event handler (#2551)

以 MIT 許可證發布。