Skip to content

useInfiniteScroll

Category
Export Size
1.98 kB
Last Changed
15 minutes ago

实现元素的无限滚动。

Demo

使用方法

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,
  () => {
    // 加载更多
    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>

指令使用

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>

  <!-- 使用选项 -->
  <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 {
  /**
   * 元素底部与视口底部的最小距离
   *
   * @default 0
   */
  distance?: number
  /**
   * 监听滚动的方向。
   *
   * @default 'bottom'
   */
  direction?: "top" | "bottom" | "left" | "right"
  /**
   * 两次加载更多之间的时间间隔(用于避免过多的调用)。
   *
   * @default 100
   */
  interval?: number
  /**
   * 用于确定特定元素是否可以加载更多内容的函数。
   * 如果对于给定的元素允许加载更多内容,则应返回 `true`,否则返回 `false`。
   */
  canLoadMore?: (el: T) => boolean
}
/**
 * Reactive infinite scroll.
 *
 * @see https://vueuse.org/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
}

Source

SourceDemoDocs

贡献者

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

变更日志

v11.1.0 on 9/16/2024
f30cc - fix: stop watch when unmounted (#4110)
v11.0.0-beta.2 on 7/17/2024
aefb6 - feat: add a reset method (#3892)
v10.7.0 on 12/5/2023
e780f - feat: add the canLoadMore option (#3558)
v10.3.0 on 7/30/2023
5ce61 - fix: improve visibility check (#3212)
v10.2.1 on 6/28/2023
a4dfa - fix: prevent infinite load when v-show set false (#3143)
v10.1.1 on 5/1/2023
a88fe - fix: re-measure arrived states when async infinite scroll resolves (#3030)
v10.0.0-beta.5 on 4/13/2023
d3a2b - fix!: improve loading strategies, close #1701, close #1685
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
0a72b - feat(toValue): rename resolveUnref to toValue

Released under the MIT License.