当前位置:首页 > VUE

vue虚拟滚动列表实现

2026-01-20 03:20:10VUE

虚拟滚动列表的实现原理

虚拟滚动(Virtual Scrolling)通过仅渲染可视区域内的元素来优化长列表性能。核心思想是根据滚动位置动态计算可见项,减少DOM节点数量。

基于vue-virtual-scroll-list的实现

安装依赖库:

npm install vue-virtual-scroll-list --save

基础用法示例:

<template>
  <virtual-list 
    :size="50"
    :remain="8"
    :items="items"
  >
    <template v-slot="{ item }">
      <div class="item">{{ item.content }}</div>
    </template>
  </virtual-list>
</template>

<script>
import VirtualList from 'vue-virtual-scroll-list'

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

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

自定义实现方案

手动实现的基本逻辑:

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

<script>
export default {
  props: {
    items: Array,
    itemHeight: {
      type: Number,
      default: 50
    },
    visibleCount: {
      type: Number,
      default: 10
    }
  },
  data() {
    return {
      startIndex: 0,
      offset: 0
    }
  },
  computed: {
    totalHeight() {
      return this.items.length * this.itemHeight
    },
    endIndex() {
      return Math.min(
        this.startIndex + this.visibleCount, 
        this.items.length
      )
    },
    visibleItems() {
      return this.items.slice(
        this.startIndex, 
        this.endIndex
      )
    }
  },
  methods: {
    handleScroll() {
      const scrollTop = this.$refs.viewport.scrollTop
      this.startIndex = Math.floor(scrollTop / this.itemHeight)
      this.offset = this.startIndex * this.itemHeight
    }
  }
}
</script>

<style>
.viewport {
  height: 500px;
  overflow-y: auto;
  position: relative;
}
.scroll-space {
  position: relative;
}
.visible-items {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
}
</style>

性能优化技巧

设置适当的key属性避免不必要的重渲染,使用稳定的唯一标识符而非数组索引

添加滚动节流处理:

import { throttle } from 'lodash'

methods: {
  handleScroll: throttle(function() {
    // 计算逻辑
  }, 16)
}

对于动态高度项目,需要实现尺寸测量和位置缓存。可以使用ResizeObserver API监测元素实际高度并更新位置计算

动态高度处理方案

安装依赖:

npm install vue-virtual-scroller

动态高度示例:

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

<script>
import { RecycleScroller } from 'vue-virtual-scroller'

export default {
  components: { RecycleScroller },
  data() {
    return {
      items: generateDynamicItems()
    }
  }
}
</script>

<style>
.scroller {
  height: 400px;
}
.dynamic-item {
  padding: 10px;
}
</style>

注意事项

滚动容器需要明确设置高度,否则无法正确计算可视区域。对于服务端渲染(SSR)场景,需要特殊处理初始渲染

vue虚拟滚动列表实现

大数据量情况下,建议配合分页加载策略。可通过Intersection Observer API实现滚动到底部自动加载更多数据

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

相关文章

vue实现switch

vue实现switch

Vue 实现 Switch 开关组件 在 Vue 中实现 Switch 开关组件可以通过自定义组件或使用现有 UI 库完成。以下是几种常见实现方式: 自定义 Switch 组件 创建一个基础 Sw…

vue实现token

vue实现token

Vue 中实现 Token 认证的方法 在 Vue 中实现 Token 认证通常涉及前端与后端的交互,包括 Token 的获取、存储、发送和刷新等操作。以下是具体实现步骤: 获取 Token 用户登…

vue实现popup

vue实现popup

Vue 实现 Popup 弹窗 使用 Vue 原生组件 创建一个基本的 Vue 组件作为弹窗,通过 v-if 或 v-show 控制显示隐藏。 <template> <div&…

vue 实现工具

vue 实现工具

Vue 实现工具的方法 使用 Vue 实现工具功能可以通过多种方式完成,具体取决于工具的类型和需求。以下是几种常见的方法: 组件化实现 创建一个独立的 Vue 组件来封装工具功能。例如,实现一个计…

vue实现布局

vue实现布局

Vue 实现布局方法 Vue 提供了多种方式实现页面布局,可以通过组件化、CSS 框架或自定义样式完成。以下是几种常见方法: 使用 CSS Flexbox 或 Grid 通过 Vue 单文件组件结合…

vue ref实现

vue ref实现

Vue 中 ref 的实现 在 Vue 中,ref 是一个用于创建响应式引用的 API,适用于基本类型和对象。以下是 ref 的核心实现原理和使用方法。 基本实现原理 ref 通过 Reactive…