当前位置:首页 > 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-virtual-scroller

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 或滚动事件监听实现无限滚动。

vue实现滚动列表

<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 状态管理实现方法 使用 Vuex(官方推荐) Vuex 是 Vue 的官方状态管理库,适合中大型应用。 安装 Vuex: npm install vuex --save 创建 Store 示…

实现vue模版

实现vue模版

Vue 模板的基本结构 Vue 模板基于 HTML 语法,通过 Vue 的指令和插值实现动态渲染。以下是一个基础模板示例: <template> <div id="app"…

vue实现suspense

vue实现suspense

Vue 3 中实现 Suspense Suspense 是 Vue 3 引入的一个内置组件,用于处理异步依赖的加载状态。它允许在等待异步组件或异步数据加载时显示一个后备内容(fallback),直到异…

vue实现订单

vue实现订单

Vue 实现订单功能 订单数据结构设计 订单数据通常包含以下字段: order: { id: String, // 订单ID userId: String,…

vue tab实现

vue tab实现

Vue Tab 实现方法 使用动态组件实现 Tab 在 Vue 中可以通过动态组件 <component :is="currentTab"> 结合 v-for 和 v-bind 实现 Ta…

vue实现radio

vue实现radio

Vue 实现 Radio 按钮 在 Vue 中实现 Radio 按钮可以通过 v-model 进行数据绑定,结合 input 元素的 type="radio" 属性。以下是一个基本示例: <t…