当前位置:首页 > VUE

vue原生实现无限滚动

2026-02-24 08:57:24VUE

vue实现无限滚动的原生方案

在Vue中实现无限滚动可以通过监听滚动事件和计算元素位置来实现,以下是几种常见方法:

监听滚动事件实现

<template>
  <div class="scroll-container" @scroll="handleScroll">
    <div v-for="item in visibleItems" :key="item.id">
      {{ item.content }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      allItems: [], // 所有数据
      visibleItems: [], // 当前显示的数据
      pageSize: 10,
      currentPage: 1
    }
  },
  methods: {
    handleScroll(event) {
      const container = event.target
      const scrollBottom = container.scrollHeight - container.scrollTop - container.clientHeight

      if (scrollBottom < 50) {
        this.loadMore()
      }
    },
    loadMore() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      const newItems = this.allItems.slice(start, end)

      if (newItems.length) {
        this.visibleItems = [...this.visibleItems, ...newItems]
        this.currentPage++
      }
    }
  },
  mounted() {
    // 初始化数据
    this.loadMore()
  }
}
</script>

<style>
.scroll-container {
  height: 500px;
  overflow-y: auto;
}
</style>

使用Intersection Observer API

更现代的解决方案是使用Intersection Observer API,性能更好:

<template>
  <div class="scroll-container">
    <div v-for="item in visibleItems" :key="item.id">
      {{ item.content }}
    </div>
    <div ref="loader" class="loader"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      allItems: [],
      visibleItems: [],
      pageSize: 10,
      currentPage: 1,
      observer: null
    }
  },
  methods: {
    loadMore() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      const newItems = this.allItems.slice(start, end)

      if (newItems.length) {
        this.visibleItems = [...this.visibleItems, ...newItems]
        this.currentPage++
      }
    }
  },
  mounted() {
    this.observer = new IntersectionObserver((entries) => {
      if (entries[0].isIntersecting) {
        this.loadMore()
      }
    })

    this.observer.observe(this.$refs.loader)
    this.loadMore()
  },
  beforeDestroy() {
    this.observer.disconnect()
  }
}
</script>

<style>
.scroll-container {
  height: 500px;
  overflow-y: auto;
}
.loader {
  height: 20px;
}
</style>

性能优化建议

  1. 使用虚拟滚动技术处理大量数据
  2. 添加防抖函数避免频繁触发滚动事件
  3. 实现数据缓存减少重复请求
  4. 添加加载状态提示提升用户体验

虚拟滚动实现思路

对于超长列表,可以考虑虚拟滚动方案:

vue原生实现无限滚动

<template>
  <div class="virtual-scroll" @scroll="handleScroll" ref="container">
    <div class="scroll-content" :style="{ height: totalHeight + 'px' }">
      <div 
        v-for="item in visibleItems" 
        :key="item.id"
        :style="{ transform: `translateY(${item.offset}px)` }"
        class="item"
      >
        {{ item.content }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      allItems: [],
      visibleItems: [],
      itemHeight: 50,
      visibleCount: 0,
      startIndex: 0
    }
  },
  computed: {
    totalHeight() {
      return this.allItems.length * this.itemHeight
    }
  },
  methods: {
    handleScroll() {
      const scrollTop = this.$refs.container.scrollTop
      this.startIndex = Math.floor(scrollTop / this.itemHeight)
      this.updateVisibleItems()
    },
    updateVisibleItems() {
      const endIndex = Math.min(
        this.startIndex + this.visibleCount,
        this.allItems.length - 1
      )

      this.visibleItems = this.allItems
        .slice(this.startIndex, endIndex)
        .map((item, index) => ({
          ...item,
          offset: (this.startIndex + index) * this.itemHeight
        }))
    }
  },
  mounted() {
    this.visibleCount = Math.ceil(
      this.$refs.container.clientHeight / this.itemHeight
    )
    this.updateVisibleItems()
  }
}
</script>

<style>
.virtual-scroll {
  height: 500px;
  overflow-y: auto;
  position: relative;
}
.scroll-content {
  position: relative;
}
.item {
  position: absolute;
  height: 50px;
  width: 100%;
}
</style>

以上方案可以根据实际需求选择使用,纯滚动事件实现简单但性能稍差,Intersection Observer更现代化,虚拟滚动适合超大数据量场景。

标签: vue
分享给朋友:

相关文章

vue实现iframe

vue实现iframe

Vue 中实现 iframe 的方法 在 Vue 项目中,可以通过多种方式实现 iframe 的嵌入。以下是几种常见的方法: 使用原生 HTML iframe 标签 直接在 Vue 组件的模板中使用…

vue实现签章

vue实现签章

Vue 实现签章功能 签章功能通常包括手写签名、电子印章等。以下是基于 Vue 的实现方法: 使用 canvas 实现手写签名 安装依赖(如需要): npm install signature_p…

vue实现分发

vue实现分发

Vue 实现分发的方法 在 Vue 中,分发(Distribution)通常指的是将内容从父组件传递到子组件,或者通过插槽(Slots)机制让父组件控制子组件的部分渲染内容。以下是几种常见的实现分发的…

vue 分页 实现

vue 分页 实现

Vue 分页实现方法 使用第三方库(如 Element UI) Element UI 提供了现成的分页组件 el-pagination,适合快速集成。 安装 Element UI: npm ins…

vue实现 treegrid

vue实现 treegrid

Vue 实现 TreeGrid 的方法 使用现成组件库 Element UI、Ant Design Vue 等流行组件库提供了现成的 TreeTable/TreeGrid 组件,可直接使用。 Ele…

vue实现反馈

vue实现反馈

Vue 实现反馈功能的方法 在 Vue 中实现反馈功能可以通过多种方式,包括弹窗提示、Toast 消息、表单提交等。以下是几种常见的实现方法。 弹窗反馈 使用 Vue 的组件化特性创建一个弹窗组件,…