@damisgarcia/v-infinite-scroll

`v-infinite-scroll` is a Vue 3 directive based on the Intersection Observer API. It provides a simple and performance-focused solution for loading new items in a `v-for` list as the user scrolls. By leveraging this directive, you can efficiently manage la
1,238
Install
npm install @damisgarcia/v-infinite-scroll
Latest Version:0.8.0
Vue:3
License:MIT
Last Updated:Jul 16, 2024
Links: GitHub  ·  npm
Maintainer: damisgarcia

Enhanced Description

v-infinite-scroll is a Vue 3 directive based on the Intersection Observer API. It provides a simple and performance-focused solution for loading new items in a v-for list as the user scrolls. By leveraging this directive, you can efficiently manage large lists by loading items incrementally, which enhances the user experience and reduces the initial load time.

Installation

To install the v-infinite-scroll directive, you can use the following command with the Bun package manager:

bun add @damisgarcia/v-infinite-scroll

Usage with Options

Here is an example of how to use the v-infinite-scroll directive with additional options such as rootMargin, threshold, wait, and onComplete:

<template>
  <div
    v-infinite-scroll="{ rootMargin: '0px', threshold: 0.15, wait: 400, onComplete: loadMoreItems }"
  >
    <div v-for="item in list" :key="item.id">
      {{ item.name }}
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { vInfiniteScroll } from '@damisgarcia/v-infinite-scroll'

const list = ref([
  { id: 1, name: 'Item 1' },
  { id: 2, name: 'Item 2' }
])

const loadMoreItems = () => {
  // Function to load more items and add them to the list
  const newItems = [
    { id: list.value.length + 1, name: `Item ${list.value.length + 1}` },
    { id: list.value.length + 2, name: `Item ${list.value.length + 2}` }
  ]
  list.value.push(...newItems)
}
</script>

Options

  1. rootMargin: This option sets a margin around the root container for the IntersectionObserver. A value of '0px' means the observation will occur exactly at the edge of the container.

  2. threshold: This option defines the percentage of the target that must be visible before triggering the callback. A value of 0.15 means the callback will be triggered when 15% of the target is visible.

  3. wait: This option sets the wait time (in milliseconds) to block new interactions, similar to a debounce functionality. This helps prevent multiple callback triggers in a short period.

  4. onComplete: This option specifies the function that will be called when the scroll reaches the limit defined by the IntersectionObserver.

Complete Example

<template>
  <div
    v-infinite-scroll="{ rootMargin: '0px', threshold: 0.15, wait: 400, onComplete: loadMoreItems }"
  >
    <div v-for="item in list" :key="item.id">
      {{ item.name }}
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { vInfiniteScroll } from '@damisgarcia/v-infinite-scroll'

const list = ref([
  { id: 1, name: 'Item 1' },
  { id: 2, name: 'Item 2' }
])

const loadMoreItems = () => {
  // Function to load more items and add them to the list
  const newItems = [
    { id: list.value.length + 1, name: `Item ${list.value.length + 1}` },
    { id: list.value.length + 2, name: `Item ${list.value.length + 2}` }
  ]
  list.value.push(...newItems)
}
</script>

With these options configured, you can have more precise control over when and how new items are loaded, improving both user experience and application performance.

Related Packages

@jfm/vue-infinite-scroll

A bidirectional infinite scroll component for Vue.js

1,565 2
vue-infinite-iscroll

A vue infinite scroll

1,037 4
@tygr/vue-infinite-scroll

Infinite scroll component for VueJS

1,461 1
vue-quick-loadmore

基于vue的移动端下拉刷新上拉无限滚动加载支持自定义文案自定义gif图的插件

919 132
vue-mugen-scroll

Infinite scroll component for Vue.js

130,014 529

Version History

Version Released Vue License
0.8.0 ^3.4.29 MIT
0.7.9 ^3.4.29
0.7.8 ^3.4.29
0.7.7 ^3.4.29
0.7.5 ^3.4.29