useInfiniteScroll
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:
| Direction | Required CSS |
|---|---|
bottom (default) | No special settings required |
top | display: flex;flex-direction: column-reverse; |
left | display: flex;flex-direction: row-reverse; |
right | display: 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/componentspackage. 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-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>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
}