Skip to content

useInfiniteScroll ​

Category
Export Size
2.86 kB
Last Changed
4 days ago

Infinite scrolling of the element.

Demo ​

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 ​

Source • Demo • Docs

Contributors ​

NoiseFan

Changelog ​

No recent changes

Released under the MIT License.