コンテンツへスキップ

useInfiniteScroll

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

要素の無限スクロール。

デモ

使用方法

Vue
<script setup lang="ts">
import { useInfiniteScroll } from '@vueuse/core'
import { ref } from 'vue'

const el = ref<HTMLElement | null>(null)
const data = ref([1, 2, 3, 4, 5, 6])

const { reset } = useInfiniteScroll(
  el,
  () => {
    // load more
    data.value.push(...moreData)
  },
  { distance: 10 }
)

function resetList() {
  data.value = []
  reset()
}
</script>

<template>
  <div ref="el">
    <div v-for="item in data">
      {{ item }}
    </div>
  </div>
  <button @click="resetList()">
    Reset
  </button>
</template>

ディレクティブの使用

この関数は、`@vueuse/components`パッケージを介してディレクティブバージョンも提供します。使用方法の詳細はこちら

Vue
<script setup lang="ts">
import { vInfiniteScroll } from '@vueuse/components'
import { ref } from 'vue'

const data = ref([1, 2, 3, 4, 5, 6])

function onLoadMore() {
  const length = data.value.length + 1
  data.value.push(...Array.from({ length: 5 }, (_, i) => length + i))
}
</script>

<template>
  <div v-infinite-scroll="onLoadMore">
    <div v-for="item in data" :key="item">
      {{ item }}
    </div>
  </div>

  <!-- with options -->
  <div v-infinite-scroll="[onLoadMore, { distance: 10 }]">
    <div v-for="item in data" :key="item">
      {{ item }}
    </div>
  </div>
</template>

型定義

型定義を表示
TypeScript
type InfiniteScrollElement =
  | HTMLElement
  | SVGElement
  | Window
  | Document
  | null
  | undefined
export interface UseInfiniteScrollOptions<
  T extends InfiniteScrollElement = InfiniteScrollElement,
> extends UseScrollOptions {
  /**
   * The minimum distance between the bottom of the element and the bottom of the viewport
   *
   * @default 0
   */
  distance?: number
  /**
   * The direction in which to listen the scroll.
   *
   * @default 'bottom'
   */
  direction?: "top" | "bottom" | "left" | "right"
  /**
   * The interval time between two load more (to avoid too many invokes).
   *
   * @default 100
   */
  interval?: number
  /**
   * A function that determines whether more content can be loaded for a specific element.
   * Should return `true` if loading more content is allowed for the given element,
   * and `false` otherwise.
   */
  canLoadMore?: (el: T) => boolean
}
/**
 * Reactive infinite scroll.
 *
 * @see https://vueuse.dokyumento.jp/useInfiniteScroll
 */
export declare function useInfiniteScroll<T extends InfiniteScrollElement>(
  element: MaybeRefOrGetter<T>,
  onLoadMore: (
    state: UnwrapNestedRefs<ReturnType<typeof useScroll>>,
  ) => Awaitable<void>,
  options?: UseInfiniteScrollOptions<T>,
): {
  isLoading: ComputedRef<boolean>
  reset(): void
}

ソースコード

ソースコードデモドキュメント

コントリビューター

Anthony Fu
Anthony Fu
webfansplz
Chris
schelmo
丶远方
Toni Engelhardt
erikwu
wonderl17
Scott Bedard
Sarwan Nizamani
Hawtim
sand4rt
Enzo Innocenzi
wheat
Melih Altıntaş

変更ログ

v12.0.0-beta.1 2024/11/21
0a9ed - feat!: Vue 2サポートの削除、バンドルの最適化、クリーンアップ (#4349)
v11.1.0 2024/9/16
f30cc - 修正: アンマウント時にウォッチを停止 (#4110)
v11.0.0-beta.2 2024年7月17日
aefb6 - 機能追加: resetメソッドを追加 (#3892)
v10.7.0 2023年12月5日
e780f - 機能追加: canLoadMore オプションを追加 (#3558)
v10.3.0 2023年7月30日
5ce61 - 修正: 可視性のチェックを改善 (#3212)
v10.2.1 2023年6月28日
a4dfa - 修正: v-show が false に設定された場合の無限ロードを防ぐ (#3143)
v10.1.1 2023年5月1日
a88fe - 修正: 非同期無限スクロールが解決されたときに、到達状態を再測定する (#3030)
v10.0.0-beta.5 2023年4月13日
d3a2b - 修正!: ローディング戦略の改善、クローズ #1701、クローズ #1685
v10.0.0-beta.4 2023年4月13日
4d757 - 機能追加(型): MaybeComputedRefMaybeRefOrGetter に名前変更
0a72b - 機能追加(toValue): resolveUnreftoValue に名前変更

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