Vue实现虚拟列表

Vue中的虚拟列表是一种渲染大量数据的优化方式。通常,渲染大量数据时,会出现页面卡顿的情况。虚拟列表通过仅渲染当前可见的数据来避免这种卡顿。

虚拟列表的实现方式是将数据分成一个个可视区域,而非全部渲染。当用户滚动列表时,只有当前可见区域的数据被渲染。当用户滚动到列表的另一个区域时,虚拟列表会根据需要重新渲染可视区域的数据。

虚拟列表的原理和使用方法就是这样。使用虚拟列表可以提高Vue应用程序的性能,让用户可以流畅地浏览大量数据。

vue虚拟列表

需要的参数:

      total: 1000, // 模拟数据条数

      contentHeight: 400, // 可视化区域高度

      showList: [], // 可视区域展示用的列表

      startIndex: 0, // 开始位置下标

      endIndex: null, // 结束下标

      itemHeight: 50, // 默认每条的高度

      scrollTop: 0, // 滚动条高度 顶部

      showNum: 0, // 当前展示列表的高度

      top: 0, // 可视区域距离顶部的距离

<template>
  <div ref="contentBox" class="content-box"
    :style="{ height: contentHeight + 'px' }"
    @scroll="scrollListener">
    <div
    :style="{
        position:'relative',
        height: showBoxHeight
      }"
    >
      <div
      :style="{
          position: 'absolute',
          top: top+'px'
        }"
      >
        <div class="box-item"
        :style="{
          height: itemHeight + 'px',
          lineHeight: itemHeight + 'px'
        }"
        v-for="(item,index) in showList" :key="index"
        >
          {{ item }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Hello',
  data() {
    return {
      total: 1000, // 模拟数据条数
      contentHeight: 400, // 可视化区域高度
      showList: [], // 可视区域展示用的列表
      list: [],
      startIndex: 0, // 开始位置下标
      endIndex: null, // 结束下标
      itemHeight: 50, // 默认每条的高度
      scrollTop: 0, // 滚动条高度 顶部
      showNum: 0, // 当前展示列表的高度
      top: 0, // 可视区域距离顶部的距离
    };
  },
  computed: {
    showBoxHeight() {
      return this.itemHeight * this.list.length + "px";
    },
  },
  mounted() {
    this.getList();
    this.scrollListener();
  },
  methods: {
    scrollListener() {
      this.scrollTop = this.$refs.contentBox.scrollTop
      this.showNum = Math.ceil(this.contentHeight / this.itemHeight)
      this.startIndex = Math.floor(this.scrollTop / this.itemHeight)
      this.endIndex = this.startIndex + this.showNum
      this.showList = this.list.slice(this.startIndex , this.endIndex)
      this.top = this.startIndex * this.itemHeight
    },
    getList() {
      this.list = Array.from({ length: this.total }).map(
        (_, index) => `我是第${index}条数据信息`,
      );
    },
  },
};
</script>

<style scoped>
.content-box {
  margin: 30px auto 0;
  position: relative;
  width: 400px;
  border: 1px solid #e4e7e4;
  overflow-y: auto;
  box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.12);
  border-radius: 4px;
  padding: 10px 0;
}

.box-item {
  padding: 0 32px 0 20px;
  color: #606266;
}

.content-box::-webkit-scrollbar {
  width: 10px;
  height: 1px;
}

.content-box::-webkit-scrollbar-thumb {
  border-radius: 10px;
  background: #e5e5e5;
}

.content-box::-webkit-scrollbar-track {
  border-radius: 10px;
  background: #ffffff;
}
</style>

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
虚拟列表是一种优化长列表性能的技术,它只渲染页面中可见的部分,而不是全部渲染。在 Vue 中,可以通过以下几个步骤来实现大数据量虚拟列表: 1. 确定列表项高度 在实现虚拟列表之前,需要先确定列表项的高度。这是因为虚拟列表需要根据列表项高度来计算可见区域的数量和位置。如果列表项高度不确定,可能会导致虚拟列表无法正确计算可见区域的位置。 2. 计算可见区域 虚拟列表需要计算出可见区域的位置和数量。可以通过以下公式来计算: ```js const visibleCount = Math.ceil(containerHeight / itemHeight) const startIndex = Math.floor(scrollTop / itemHeight) ``` 其中,`visibleCount` 表示可见区域的数量,`containerHeight` 表示列表容器的高度,`itemHeight` 表示列表项的高度,`startIndex` 表示可见区域的起始索引,`scrollTop` 表示列表容器的滚动距离。 3. 动态渲染列表项 根据可见区域的位置和数量,可以动态渲染列表项。可以使用 `v-for` 指令来遍历列表数据,并使用 `v-show` 指令来控制列表项是否显示。例如: ```html <div class="list-container" ref="listContainer" @scroll="handleScroll"> <div class="list" :style="{ height: listHeight + 'px' }"> <div v-for="(item, index) in visibleData" :key="item.id" :style="{ top: (startIndex + index) * itemHeight + 'px' }" v-show="index < visibleCount"> {{ item.text }} </div> </div> </div> ``` 其中,`listContainer` 表示列表容器的引用,`listHeight` 表示列表的总高度,`visibleData` 表示可见的列表数据,`startIndex` 表示可见区域的起始索引,`itemHeight` 表示列表项的高度,`visibleCount` 表示可见区域的数量。 4. 监听滚动事件 为了实现列表的滚动效果,需要监听列表容器的滚动事件,并根据滚动距离来计算可见区域的位置和数量。可以使用 `ref` 属性来获取列表容器的引用,并使用 `@scroll` 指令来监听滚动事件。例如: ```js methods: { handleScroll() { const scrollTop = this.$refs.listContainer.scrollTop const visibleCount = Math.ceil(this.$refs.listContainer.offsetHeight / this.itemHeight) const startIndex = Math.floor(scrollTop / this.itemHeight) this.visibleData = this.data.slice(startIndex, startIndex + visibleCount) } } ``` 其中,`scrollTop` 表示列表容器的滚动距离,`visibleCount` 表示可见区域的数量,`startIndex` 表示可见区域的起始索引,`visibleData` 表示可见的列表数据,`data` 表示完整的列表数据。 通过以上步骤,就可以实现大数据量虚拟列表了。需要注意的是,虚拟列表只渲染可见区域的部分,因此在列表项中使用计算属性等复杂操作可能会影响性能。如果需要对列表项进行复杂操作,可以考虑使用 `watch` 监听列表数据的变化,然后重新计算可见区域并更新列表

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

来都来了_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值