跳到主要内容

useInfiniteScroll

分类
导出大小
2.86 kB
上次更改
8 个月前

元素的无限滚动。

演示

用法

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 (默认)无需特殊设置
topdisplay: flex;
flex-direction: column-reverse;
leftdisplay: flex;
flex-direction: row-reverse;
rightdisplay: 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-scro
ll="
onLoadMore
">
<
div
v-for="
item
in
data
"
:key
="
item
">
{{
item
}}
</
div
>
</
div
>
<!-- with options --> <
div
v-infinite-scro
ll="
[
onLoadMore
, {
distance
: 10,
canLoadMore
}]
">
<
div
v-for="
item
in
data
"
:key
="
item
">
{{
item
}}
</
div
>
</
div
>
</template>

类型声明

显示类型声明
ts
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
NoiseFan
IlyaL
Alan North
Chris
schelmo
丶远方
Toni Engelhardt
erikwu
wonderl17
Scott Bedard
Sarwan Nizamani
Hawtim
sand4rt
Enzo Innocenzi
wheat
Melih Altıntaş

更新日志

8c521 - feat(components)!: 重构组件并使其保持一致 (#4912)
v12.8.0
7432f - feat(types): 废弃 MaybeRefMaybeRefOrGetter,转而使用 Vue 的原生类型 (#4636)
v12.3.0
59f75 - feat(toValue): 废弃 @vueuse/shared 中的 toValue,转而使用 Vue 的原生函数
0a9ed - feat!: 放弃对 Vue 2 的支持,优化打包并清理 (#4349)
v11.1.0
f30cc - fix: 卸载时停止 watch (#4110)
aefb6 - feat: 添加 reset 方法 (#3892)
v10.7.0
e780f - feat: 添加 canLoadMore 选项 (#3558)
v10.3.0
5ce61 - fix: 改进可见性检查 (#3212)
v10.2.1
a4dfa - fix: 当 v-show 设置为 false 时防止无限加载 (#3143)
v10.1.1
a88fe - fix: 当异步无限滚动解决时重新测量到达状态 (#3030)

根据 MIT 许可证发布。