vue中实现虚拟列表

什么是虚拟列表

虚拟列表是一种长列表优化方案,通过数据截取来生成数据,达到优化性能的一种方式,与传统长列表相比性能更高,速度更快,避免了页面卡顿。


实现方式:

几个初始值概览:

1、contentHeight:滚动可视区域高度

2、showList:可视区域展示用的列表

3、startIndex:起始数据位置

4、endIndex:结束数据位置

5、scrollTop:卷起高度

6、showNum:当前列表高度可展示个数

7、itemHeight:每项item高度

8、top:可视区域距离顶部距离,通过这个值使当前列表始终展示在可视区域内

实现步骤

  1. 我们给content-box类设置了滚动条,高度;并且监听了滚动事件;
  2. 给下级元素设置了总高度,总高度计算方式 = 每项item高度 * 总数据条数,用于撑起滚动条;
  3. 通过监听滚动事件,计算出showList当前展示内容;
  4. 给元素设置定位,始列表始终出现在可视区域内;

完整代码如下,可复制后查看效果:

<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) of showList"
          :key="index"
        >
          {{ item }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "virtualList",
  data() {
    return {
      total: 1000000, //模拟数据总数
      contentHeight: 400,
      showList: [],
      list: [],
      startIndex: 0,
      endIndex: null,
      itemHeight: 30,
      scrollTop: 0, //卷起高度
      showNum: 0, //当前显示列表个数
      top: 0, //距离顶部距离
    };
  },
  computed: {
    showBoxHeight() {
      return this.itemHeight * this.list.length + "px";
    },
  },
  mounted() {
    this.getList();
    this.scrollListener();
  },
  methods: {
    getList() {
      // 构造虚拟数据
      this.list = Array.from({ length: this.total }).map(
        (_, index) => `第${index + 1}条数据`
      );
    },
    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; // 距离顶部的距离,控制内容出现在可视区域
    },
  },
};
</script>

<style scoped>
.content-box {
  margin: 30px auto 0;
  position: relative;
  width: 400px;
  border: 1px solid #e4e7ed;
  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>

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vxe-table是一个基于vue的表格组件,支持增删改查、虚拟滚动、懒加载、快捷菜单、数据校验、树形结构、打印导出、表单渲染、数据分页、模态窗口、自定义模板、灵活的配置项、丰富的扩展插件等... 设计理念: 面向现代浏览器,高效的简洁 API 设计 模块化表格、按需加载、插件化扩展 为单行编辑表格而设计,支持增删改查及更多扩展,强大的功能的同时兼具性能 功能: Basic table (基础表格) Grid (高级表格) Size (尺寸) Striped (斑马线条纹) Table with border (带边框) Cell style (单元格样式) Column resizable (列宽拖动) Maximum table height (最大高度) Resize height and width (响应式宽高) Fixed column (固定列) Grouping table head (表头分组) Highlight row and column (高亮行、列) Table sequence (序号) Radio (单选) Checkbox (多选) Sorting (排序) Filter (筛选) Rowspan and colspan (合并行或列) Footer summary (表尾合计) Import (导入) Export (导出) Print (打印) Show/Hide column (显示/隐藏列) Loading (加载) Formatted content (格式化内容) Custom template (自定义模板) Context menu(快捷菜单) Virtual Scroller(虚拟滚动) Expandable row (展开行) Pager(分页) Form(表单) Toolbar(工具栏) Tree table (树形表格) Editable CRUD(增删改查) Validate(数据校验) Data Proxy(数据代理) Keyboard navigation(键盘导航) Modal window(模态窗口) Charts(图表工具) 更新日志: v4.0.20 table 修改单选框、复选框获取值错误问题 grid 修复 KeepAlive 报错问题

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值