Skip to content

useInfiniteScroll

Category
Export Size
2.86 kB
Last Changed
5 months ago

Infinite scrolling of the element.

Demo

1
2
3
4
5
6
7
8
9
10

Usage

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>

Direction

Different scroll directions require different CSS style settings:

DirectionRequired CSS
bottom (default)No special settings required
topdisplay: flex;
flex-direction: column-reverse;
leftdisplay: flex;
flex-direction: row-reverse;
rightdisplay: flex;

WARNING

Make sure to indicate when there is no more content to load with canLoadMore, otherwise onLoadMore will trigger as long as there is space for more content.

Directive Usage

This function also provides a directive version via the @vueuse/components package. Learn more about the usage.

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>

Type Declarations

Show Type Declarations
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/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

Contributors

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ş

Changelog

v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v12.3.0 on
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v11.1.0 on
f30cc - fix: stop watch when unmounted (#4110)
v11.0.0-beta.2 on
aefb6 - feat: add a reset method (#3892)
v10.7.0 on
e780f - feat: add the canLoadMore option (#3558)
v10.3.0 on
5ce61 - fix: improve visibility check (#3212)
v10.2.1 on
a4dfa - fix: prevent infinite load when v-show set false (#3143)
v10.1.1 on
a88fe - fix: re-measure arrived states when async infinite scroll resolves (#3030)
v10.0.0-beta.5 on
d3a2b - fix!: improve loading strategies, close #1701, close #1685
v10.0.0-beta.4 on
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
0a72b - feat(toValue): rename resolveUnref to toValue

Released under the MIT License.

Build faster with AI
New Masterclass to help you leverage AI in your Vue workflow
Get Early Access