vue虚拟列表实现(简单详细)项目优化必备

首先定义一下概念!

Vue虚拟列表(Virtual List)可以在大量数据的场景下,提高页面渲染性能和用户体验。其实现方式是只渲染当前可视区域内的数据部分,而非全部渲染数据,可以通过监听滚动事件,动态计算当前可视区域的起始索引和结束索引,并只渲染这一部分的数据。

简单来说就是在固定数量的dom节点上去对大量的返回数据进行渲染(dom节点具体有多少还是取决于前端列表需要展示的数量),例如,一个列表,需要接收后端返回的10w条数据,此时我们只需要10个dom节点来展示,那么这10w条数据,会根据滚动事件在这10个dom节点上来进行视图层的刷新渲染,从而实现虚拟列表的展示

代码放下面!(下方是组件哈,可以自己引用一下,非常简单)

1. 在模板中

<div class="list-container">
  <div class="list-item" 
       v-for="(item, index) in displayedItems" 
       :key="startIndex + index"
       :style="{ height: itemHeight + 'px' }">
    <!-- 渲染每一项的内容 -->
  </div>
</div>

2. 列表组件中,定义如下 props 和 data

props: {
  // 列表每一项数据的固定高度
  itemHeight: {
    type: Number,
    required: true,
  },
  // 列表展示的数据源
  items: {
    type: Array,
    required: true,
  },
  // 可视区域的高度
  viewHeight: {
    type: Number,
    required: true,
  },
},
data() {
  return {
    // 当前可视区域首位元素的索引
    startIndex: 0,
    // 当前可视区域末尾元素的索引
    endIndex: 0,
    // 实际渲染的数据
    displayedItems: [],
  }
},

3. 组件的mountedwatch钩子中,初始化和更新当前可视区域数据

mounted() {
  this.updateVisibleData()
  window.addEventListener('scroll', this.updateVisibleData)
},
watch: {
  items() {
    this.startIndex = 0
    this.endIndex = 0
    this.updateVisibleData()
  },
},
methods: {
  // 计算当前可视区域的起始索引和结束索引
  updateVisibleData() {
    const scrollTop = window.pageYOffset
    const visibleHeight = this.viewHeight + scrollTop
    const startIndex = Math.floor(scrollTop / this.itemHeight)
    const endIndex = Math.ceil(visibleHeight / this.itemHeight)

    if (startIndex !== this.startIndex || endIndex !== this.endIndex) {
      this.startIndex = startIndex
      this.endIndex = endIndex
      this.displayedItems = this.items.slice(startIndex, endIndex)
    }
  },
}

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
由于虚拟滚动需要考虑的因素较多,包括滚动容器的高度、每条数据的高度、滚动位置等,因此需要编写较为复杂的代码。以下是一个简单Vue虚拟滚动组件,供参考: ```vue <template> <div class="scroll-container" ref="container" @scroll="handleScroll"> <div class="scroll-content" :style="contentStyle"> <div v-for="(item, index) in visibleData" :key="item.id" class="scroll-item"> {{ item.text }} </div> </div> </div> </template> <script> export default { name: "VirtualScroll", props: { data: { type: Array, required: true, default: () => [], }, itemHeight: { type: Number, required: true, default: 30, }, visibleItemCount: { type: Number, required: true, default: 10, }, }, data() { return { startIndex: 0, endIndex: this.visibleItemCount - 1, scrollTop: 0, }; }, computed: { visibleData() { return this.data.slice(this.startIndex, this.endIndex + 1); }, contentHeight() { return this.data.length * this.itemHeight + "px"; }, contentStyle() { return { height: this.contentHeight, paddingTop: this.startIndex * this.itemHeight + "px", }; }, }, methods: { handleScroll() { this.scrollTop = this.$refs.container.scrollTop; this.startIndex = Math.floor(this.scrollTop / this.itemHeight); this.endIndex = this.startIndex + this.visibleItemCount - 1; }, }, }; </script> <style scoped> .scroll-container { height: 300px; overflow-y: auto; } .scroll-content { position: relative; } .scroll-item { height: 30px; line-height: 30px; } </style> ``` 在上述代码中,我们首先定义了一个滚动容器和一个滚动内容,滚动容器设置了固定的高度和滚动条,滚动内容则根据数据的高度和数量来设置高度和内边距。 在组件的数据中,我们定义了起始索引、结束索引和滚动位置,起始索引和结束索引用于计算当前可见数据的范围,滚动位置用于计算当前滚动的位置。 在计算属性中,我们使用起始索引和结束索引来计算当前可见的数据,使用数据的高度和数量来计算滚动内容的高度和内边距。 在方法中,我们通过监听滚动事件来更新起始索引、结束索引和滚动位置,从而实现虚拟滚动的效果。 以上代码只是一个简单的示例,实际应用中还需要根据具体情况进行调整和优化,例如添加缓存机制、使用虚拟列表等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值