跳到主要内容

useInfiniteScroll

分类
导出大小
2.51 kB
最近更改
5 天前

元素的无限滚动。

演示

用法

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

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

const { reset } = useInfiniteScroll(
  el,
  () => {
    // load more
    data.value.push(...moreData)
  },
  {
    distance: 10,
    canLoadMore: () => {
      // inidicate when there is no more content to load so onLoadMore stops triggering
      // if (noMoreContent) return false
      return true // for demo purposes
    },
  }
)

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>

方向

不同的滚动方向需要不同的 CSS 样式设置

方向必需的 CSS
bottom (默认)无需特殊设置
顶部display: flex;
flex-direction: column-reverse;
display: flex;
flex-direction: row-reverse;
display: flex;

警告

请确保使用 canLoadMore 指示何时没有更多内容可加载,否则只要有空间容纳更多内容,onLoadMore 就会触发。

指令用法

此函数还通过 @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))
}
function canLoadMore() {
  // inidicate when there is no more content to load so onLoadMore stops triggering
  // if (noMoreContent) return false
  return true // for demo purposes
}
</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, canLoadMore }]">
    <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.org.cn/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
IlyaL
webfansplz
Alan North
Chris
schelmo
丶远方
Toni Engelhardt
erikwu
wonderl17
Scott Bedard
Sarwan Nizamani
Hawtim
sand4rt
Enzo Innocenzi
wheat
Melih Altıntaş

更新日志

v12.8.0 于 2025/3/5
7432f - feat(types): 弃用 MaybeRefMaybeRefOrGetter,转而使用 Vue 原生方法 (#4636)
v12.3.0 于 2025/1/2
59f75 - feat(toValue): 弃用来自 @vueuse/sharedtoValue,转而使用 Vue 原生方法
v12.0.0-beta.1 于 2024/11/21
0a9ed - feat!: 移除 Vue 2 支持,优化包并清理代码 (#4349)
v11.1.0 于 2024/9/16
f30cc - fix: 修复卸载时停止监听的问题 (#4110)
v11.0.0-beta.2 于 2024/7/17
aefb6 - feat: 添加重置方法 (#3892)
v10.7.0 于 2023/12/5
e780f - feat: 添加 canLoadMore 选项 (#3558)
v10.3.0 于 2023/7/30
5ce61 - fix: 改进可见性检查 (#3212)
v10.2.1 于 2023/6/28
a4dfa - fix: 修复 v-show 设置为 false 时无限加载的问题 (#3143)
v10.1.1 于 2023/5/1
a88fe - fix: 修复异步无限滚动解决时重新测量到达状态的问题 (#3030)
v10.0.0-beta.5 于 2023/4/13
d3a2b - fix!: 改进加载策略,关闭 #1701, 关闭 #1685
v10.0.0-beta.4 于 2023/4/13
4d757 - feat(types)!: 将 MaybeComputedRef 重命名为 MaybeRefOrGetter
0a72b - feat(toValue): 将 resolveUnref 重命名为 toValue

在 MIT 许可证下发布。