当前位置:首页 > VUE

vue实现滚动列表

2026-03-10 09:48:33VUE

Vue 实现滚动列表的方法

使用 CSS 实现基础滚动

通过 CSS 的 overflow 属性可以快速实现滚动效果。将列表容器的高度固定,并设置 overflow-y: auto

<template>
  <div class="scroll-container">
    <div v-for="item in items" :key="item.id" class="list-item">
      {{ item.text }}
    </div>
  </div>
</template>

<style>
.scroll-container {
  height: 300px;
  overflow-y: auto;
  border: 1px solid #ddd;
}
.list-item {
  padding: 10px;
  border-bottom: 1px solid #eee;
}
</style>

使用第三方库实现高性能滚动

对于大数据量的列表,推荐使用 vue-virtual-scrollerbetter-scroll 等库优化性能。

vue实现滚动列表

安装 vue-virtual-scroller

vue实现滚动列表

npm install vue-virtual-scroller

示例代码:

<template>
  <RecycleScroller
    class="scroller"
    :items="items"
    :item-size="50"
    key-field="id"
  >
    <template v-slot="{ item }">
      <div class="item">
        {{ item.text }}
      </div>
    </template>
  </RecycleScroller>
</template>

<script>
import { RecycleScroller } from 'vue-virtual-scroller'
import 'vue-virtual-scroller/dist/vue-virtual-scroller.css'

export default {
  components: { RecycleScroller },
  data() {
    return {
      items: Array(1000).fill().map((_, i) => ({ id: i, text: `Item ${i}` }))
    }
  }
}
</script>

<style>
.scroller {
  height: 300px;
}
.item {
  height: 50px;
  padding: 10px;
  border-bottom: 1px solid #eee;
}
</style>

实现无限滚动加载

结合 IntersectionObserver API 或滚动事件监听实现无限滚动。

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

<script>
export default {
  data() {
    return {
      items: [],
      page: 1,
      loading: false,
      hasMore: true
    }
  },
  computed: {
    visibleItems() {
      return this.items.slice(0, this.page * 20)
    }
  },
  methods: {
    async loadMore() {
      if (this.loading || !this.hasMore) return
      this.loading = true
      const newItems = await fetchItems(this.page)
      this.items = [...this.items, ...newItems]
      this.page++
      this.loading = false
      this.hasMore = newItems.length > 0
    },
    handleScroll(e) {
      const { scrollTop, clientHeight, scrollHeight } = e.target
      if (scrollHeight - (scrollTop + clientHeight) < 50) {
        this.loadMore()
      }
    }
  },
  mounted() {
    this.loadMore()
  }
}
</script>

注意事项

  • 大数据量列表务必使用虚拟滚动技术
  • 滚动事件监听需要节流处理
  • 移动端考虑使用 touch 事件优化体验
  • 无限滚动需要合理设置触发阈值

标签: 列表vue
分享给朋友:

相关文章

vue实现付款

vue实现付款

Vue 实现付款功能 在 Vue 中实现付款功能通常需要集成第三方支付网关(如支付宝、微信支付、Stripe 等)。以下是常见的实现方法: 集成支付宝/微信支付 安装必要的依赖(如 axios 用于…

vue实现排序

vue实现排序

Vue 实现排序的方法 在 Vue 中实现排序可以通过多种方式完成,以下是一些常见的方法: 使用计算属性排序数组 计算属性非常适合对数据进行排序,因为它会在依赖的数据变化时自动更新。以下是一个示例:…

vue实现注册

vue实现注册

Vue 实现注册功能 注册功能通常涉及前端表单、数据验证、与后端 API 的交互等步骤。以下是一个完整的 Vue 实现注册功能的方案。 创建注册表单 使用 Vue 的模板语法创建注册表单,包含用户名…

vue实现白板

vue实现白板

Vue实现白板功能 使用Vue实现白板功能可以通过HTML5的Canvas API结合Vue的响应式特性来完成。以下是实现白板功能的关键步骤: 安装依赖 在Vue项目中安装必要的依赖,如vue-dr…

vue实现刷卡

vue实现刷卡

Vue 实现刷卡效果 在 Vue 中实现刷卡效果可以通过 CSS 动画和 Vue 的过渡系统结合完成。以下是一种常见的实现方式: 基础实现方法 安装 Vue 过渡依赖(如果尚未安装): npm i…

vue  实现分页

vue 实现分页

实现分页的基本思路 在Vue中实现分页通常需要结合后端API返回的数据和前端的分页组件。分页的核心逻辑包括计算总页数、处理当前页码变化以及触发数据请求。 分页组件示例 使用Element UI的分页…