useInfiniteScroll
元素的无限滚动。
演示
用法
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
}
源代码
贡献者
更新日志
v12.8.0
于 2025/3/5v12.3.0
于 2025/1/259f75
- feat(toValue): 弃用来自 @vueuse/shared
的 toValue
,转而使用 Vue 原生方法v12.0.0-beta.1
于 2024/11/21v11.1.0
于 2024/9/16v11.0.0-beta.2
于 2024/7/17v10.7.0
于 2023/12/5v10.3.0
于 2023/7/30v10.2.1
于 2023/6/28v10.1.1
于 2023/5/1v10.0.0-beta.5
于 2023/4/13v10.0.0-beta.4
于 2023/4/134d757
- feat(types)!: 将 MaybeComputedRef
重命名为 MaybeRefOrGetter
0a72b
- feat(toValue): 将 resolveUnref
重命名为 toValue