Category | Sensors |
---|---|
Export Size | 1.94 kB |
Last Changed | last week |
Infinite scrolling of the element.
<script setup lang="ts"> import { ref } from 'vue' import { useInfiniteScroll } from '@vueuse/core' const el = ref<HTMLElement>(null) const data = ref([1, 2, 3, 4, 5, 6]) useInfiniteScroll( el, () => { // load more data.value.push(...moreData) }, { distance: 10 } ) </script> <template> <div ref="el"> <div v-for="item in data"> {{ item }} </div> </div> </template>
This function also provides a directive version via the
@vueuse/components
package. Learn more about the usage.
<script setup lang="ts"> import { ref } from 'vue' import { vInfiniteScroll } from '@vueuse/components' 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)) } </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 }]"> <div v-for="item in data" :key="item"> {{ item }} </div> </div> </template>
export interface UseInfiniteScrollOptions 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 } /** * Reactive infinite scroll. * * @see https://vueuse.org/useInfiniteScroll */ export declare function useInfiniteScroll( element: MaybeRefOrGetter< HTMLElement | SVGElement | Window | Document | null | undefined >, onLoadMore: ( state: UnwrapNestedRefs<ReturnType<typeof useScroll>> ) => Awaitable<void>, options?: UseInfiniteScrollOptions ): { isLoading: ComputedRef<boolean> }
© 2019-present Anthony Fu
Licensed under the MIT License.
https://vueuse.org/core/useInfiniteScroll/