最近做的一个需求,当列表大概有2万条数据,又不让做成分页,如果页面直接渲染2万条数据,在一些低配电脑上可能会照成页面卡死,基于这个需求,我们来手写一个虚拟列表

思路

  1. 列表中固定只显示少量的数据,比如60条
  2. 在列表滚动的时候不断的去插入删除dom
  3. startIndex、endIndex,不断的改变这个值来获取最新的显示列表
  4. paddingTop、paddingBottom撑开容器的滚动区域

首先看一下当直接插入2万条列表时,页面的性能



可以看到火焰图中已经有了红色的部分了,dom渲染也耗时也有1s多

再来看一下当使用虚拟列表时页面的性能



从火焰图中可以看出,火焰图中一篇绿油油的,这就证明,通过虚拟列表来进行渲染使页面性能得到了极大的提升

简单的虚拟列表demo实现

我们假设有一个容器,高度为600px,列表项每个高度为30px,那么根据列表的length我们就可以计算出滚动容器的总高度,也可以知道显示60条数据的高度,我们此时可以给容器加一个paddingBottom,来撑开容器,来模拟页面应该滚动的高度

this.paddingBottom = this.allHeight - this.scrollList.length * 30

容器同时还需要paddingTop用做当容器滚动顶部数据移除后撑起scrollTop

最后我们需要监听容器的滚动事件来不断的修改paddingTop、paddingBottom、startIndex、endIndex

最终效果

最后附上所有代码

<!doctype html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.container {
width: 300px;
height: 600px;
overflow: auto;
border: 1px solid;
margin: 100px auto;
}
.item {
height: 29px;
line-height: 30px;
border-bottom: 1px solid #aaa;
padding-left: 20px;
}
</style>
</head>
<body>
<div id="app">
<button @click="add">增加</button>
<div class="container" ref="container">
<div class="scroll-wrapper" :style="style">
<div v-for="(item, index) in scrollList" :key="index" class="item">{{item}}</div>
</div>
</div>
</div>
<script src="./vue.js"></script>
<script>
new Vue({
el: '#app',
data: {
list: [
'测试数据'
],
startIndex: 0,
endIndex: 60,
paddingTop: 0,
paddingBottom: 0,
allHeight: 0
},
computed: {
scrollList() {
return this.list.slice(this.startIndex, this.endIndex)
},
style() {
return {
paddingTop: this.paddingTop + 'px',
paddingBottom: this.paddingBottom + 'px'
}
}
},
watch: {
list(val) {
const valLen = val.length
this.allHeight = valLen * 30
this.paddingBottom = this.allHeight - this.scrollList.length * 30
}
},
mounted() {
const container = this.$refs.container
container.addEventListener('scroll', () => {
const top = container.scrollTop
this.startIndex = Math.floor(top / 30)
this.endIndex = this.startIndex + 60 this.paddingTop = top
if (this.endIndex >= this.list.length - 1) {
this.paddingBottom = 0
return
}
this.paddingBottom = this.allHeight - 600 - top
})
},
methods: {
add() {
let arr = new Array(50000).fill(0)
arr = arr.map((item, index) => {
return index
})
this.list = [
...this.list,
...arr
]
}
}
})
</script>
</body>
</html>

最新文章

  1. Java学习笔记之方法重载
  2. 跟随 Web 标准探究DOM -- Node 与 Element 的遍历
  3. 使用多文档接口(Multiple Document Interface) 一
  4. Win7下Eclipse中文字体太小
  5. Bootstrap系列 -- 41. 带表单的导航条
  6. Git.Framework 框架随手记--准备工作
  7. 利用COPYDATASTRUCT传递命令行参数给驻留内存的进程(SendMessage应用)
  8. XSLT 处理程序是如何工作的
  9. Linux 根文件系统制作
  10. Delphi 调试BPL包中引用另外的BPL的方法。
  11. char与unsigned char 差别
  12. 本地访问服务器上的wamp
  13. [APIO2010]
  14. 2111 ACM 贪心 水题
  15. LVM基础详细说明及动态扩容lvm逻辑卷的操作记录
  16. window如何一键关闭所有进程程序
  17. 数组操作方法中的splice()和concat() 以及slice()
  18. Project://STARK
  19. 微信小程序 request域名配置好之后,还是提示报错配置的域名不在request合法域名中
  20. Realm数据库的使用

热门文章

  1. Elasticsearch核心技术(1)--- Docker容器中运行ES、Kibana、Cerebro
  2. 解决ionic 上拉加载组件 ion-infinite-scroll自动调用多次的问题
  3. HDU 6299
  4. Java多线程之Runnable与Thread
  5. 使用golang对海康sdk进行业务开发
  6. JUC包实现的同步机制,原理以及简单用法总结
  7. 前端架构师亲述:前端工程师成长之路的 N 问 及 回答
  8. cogs2823求组合数(lucas定理
  9. POJ-1847 Tram( 最短路 )
  10. Disruptor框架中生产者、消费者的各种复杂依赖场景下的使用总结