コンテンツにスキップ

useIntersectionObserver

カテゴリー
エクスポートサイズ
634 B
最終変更日
先週

ターゲット要素の可視性を検出します。

デモ

下にスクロールしてください!

こんにちは、世界!

ビューポートの要素

使用法

vue
<script setup>
import { useIntersectionObserver } from '@vueuse/core'
import { ref } from 'vue'

const target = ref(null)
const targetIsVisible = ref(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 { ref } from 'vue'

const root = ref(null)

const isVisible = ref(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: Ref<boolean>
  stop: () => void
}
/**
 * Detects that a target element's visibility.
 *
 * @see https://vueuse.dokyumento.jp/useIntersectionObserver
 * @param target
 * @param callback
 * @param options
 */
export declare function useIntersectionObserver(
  target:
    | MaybeComputedElementRef
    | MaybeRefOrGetter<MaybeElement[]>
    | MaybeComputedElementRef[],
  callback: IntersectionObserverCallback,
  options?: UseIntersectionObserverOptions,
): UseIntersectionObserverReturn

ソース

ソースデモドキュメント

貢献者

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

変更履歴

v12.0.0-beta.1 2024/11/21
0a9ed - feat!: Vue 2のサポートを削除、バンドルを最適化、クリーンアップ (#4349)
v11.1.0 2024/09/16
6b584 - fix: rootにDocument型を追加 (#4210)
v11.0.0-beta.2 2024/07/17
13e36 - fix!: threshold のデフォルト値を 0 に更新 (#4069)
v10.0.2 2023/04/14
7d001 - fix: モジュール参照, close #2972
v10.0.1 2023/04/14
b95b6 - fix: targets の長さチェック (#2968)
v10.0.0 2023/04/14
f87f8 - feat: 複数のターゲットを許可 (#2964)
v10.0.0-beta.4 2023/04/13
4b336 - feat: Pausable インターフェースのサポート (#2883)
v10.0.0-beta.2 2023/03/28
74b00 - fix(useElementVisibility)!: スクロールイベントハンドラーの代わりに useIntersectionObserver を使用 (#2551)

MITライセンスの下でリリースされています。