vue實現(xiàn)虛擬列表功能的代碼
當(dāng)數(shù)據(jù)量較大(此處設(shè)定為10w),而且要用列表的形式展現(xiàn)給用戶,如果我們不做處理的話,在瀏覽器中渲染10w dom節(jié)點(diǎn),是極其耗費(fèi)時間的,那我的Macbook air舉例,10w條數(shù)據(jù)渲染出來到能看到頁面,需要13秒多(實際應(yīng)該是10秒左右),如果是用戶的話肯定是不會等一個網(wǎng)頁十幾秒的

我們可以用虛擬列表解決這個問題
一步步來
首先看一下效果

這是data中的數(shù)據(jù)
data() {
return {
list: [], // 賊大的數(shù)組
li: {
// 列表項信息
height: 50,
},
container: {
// 容器信息
height: 500,
},
pos: 1, // 第一排顯示的元素的下標(biāo)
MAX_NUM: 1, // 在容器內(nèi)最多顯示幾個列表項
timer: null, // 定時器
carriedOut: true, // 能不能執(zhí)行操作
};
},
然后在mounted中創(chuàng)建一個賊大的數(shù)組,在調(diào)用test方法計算第一次的虛擬列表中有哪些
mounted() {
// 創(chuàng)建一個賊大的數(shù)據(jù)數(shù)組
for (let i = 0; i < 100000; i++) {
this.list.push(i);
}
this.test();
},
test方法
test() {
// 節(jié)流
if (this.carriedOut) {
// 容器跟里面的列表項
const { container, li } = this;
// 計算可視區(qū)域最多能顯示多少個li
this.MAX_NUM = Math.ceil(container.height / li.height);
// 獲取 overflow:scroll 的元素已滾動的高度
let scrollTop = this.$refs.container.scrollTop;
// 計算當(dāng)前處于第一排的元素的下標(biāo)
this.pos = Math.round(scrollTop / li.height);
// 下方節(jié)流操作
this.carriedOut = false;
this.timer = setTimeout(() => {
this.carriedOut = true;
clearTimeout(this.timer);
}, 50);
}
},
然后是computed
computed: {
// 用于渲染在頁面上的數(shù)組
showList() {
// 根據(jù)計算出來的 第一排元素的下標(biāo),和最多顯示多少個 用slice實現(xiàn)截取數(shù)組
let arr = this.list.slice(this.pos, this.pos + this.MAX_NUM);
return arr;
},
},
這是html,注意監(jiān)聽了div的scroll事件,并且調(diào)用的是test方法
<div class="virtual-list">
<h1>虛擬列表</h1>
<div class="container" ref="container" :style="`height:${container.height}px`" @scroll="test">
<ul :style="`height:${li.height*list.length}px;padding-top:${li.height*pos}px`">
<li :style="`height:${li.height}px`" v-for="item in 100000" :key="item">{{item}}</li>
</ul>
</div>
</div>
完整源代碼
<template>
<div class="virtual-list">
<h1>虛擬列表</h1>
<div class="container" ref="container" :style="`height:${container.height}px`" @scroll="test">
<ul :style="`height:${li.height*list.length}px;padding-top:${li.height*pos}px`">
<li :style="`height:${li.height}px`" v-for="item of showList" :key="item">{{item}}</li>
</ul>
</div>
</div>
</template>
<script>
export default {
data() {
return {
list: [], // 賊大的數(shù)組
li: {
// 列表項信息
height: 50,
},
container: {
// 容器信息
height: 500,
},
pos: 1, // 第一排顯示的元素的下標(biāo)
MAX_NUM: 1, // 在容器內(nèi)最多顯示幾個列表項
timer: null, // 定時器
carriedOut: true, // 能不能執(zhí)行操作
};
},
mounted() {
// 創(chuàng)建一個賊大的數(shù)據(jù)數(shù)組
for (let i = 0; i < 1000; i++) {
this.list.push(i);
}
this.test();
},
computed: {
// 用于渲染在頁面上的數(shù)組
showList() {
// 根據(jù)計算出來的 第一排元素的下標(biāo),和最多顯示多少個 用slice實現(xiàn)截取數(shù)組
let arr = this.list.slice(this.pos, this.pos + this.MAX_NUM);
return arr;
},
},
methods: {
test() {
// 節(jié)流
if (this.carriedOut) {
// 容器跟里面的列表項
const { container, li } = this;
// 計算可視區(qū)域最多能顯示多少個li
this.MAX_NUM = Math.ceil(container.height / li.height);
// 獲取 overflow:scroll 的元素已滾動的高度
let scrollTop = this.$refs.container.scrollTop;
// 計算當(dāng)前處于第一排的元素的下標(biāo)
this.pos = Math.round(scrollTop / li.height);
// 下方節(jié)流操作
this.carriedOut = false;
this.timer = setTimeout(() => {
this.carriedOut = true;
clearTimeout(this.timer);
}, 50);
}
},
},
};
</script>
<style lang="scss" scoped>
.virtual-list {
text-align: center;
.container {
overflow: scroll;
border: 1px solid red;
}
}
</style>
到此這篇關(guān)于vue實現(xiàn)虛擬列表功能的代碼的文章就介紹到這了,更多相關(guān)vue 虛擬列表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue 列表頁跳轉(zhuǎn)詳情頁獲取id以及詳情頁通過id獲取數(shù)據(jù)
這篇文章主要介紹了vue 列表頁跳轉(zhuǎn)詳情頁獲取id以及詳情頁通過id獲取數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
如何使用el-table+el-tree+el-select動態(tài)選擇對應(yīng)值
小編在做需求時,遇到了在el-table表格中加入多條數(shù)據(jù),并且每條數(shù)據(jù)要通過el-select來選取相應(yīng)的值,做到動態(tài)選擇,下面這篇文章主要給大家介紹了關(guān)于如何使用el-table+el-tree+el-select動態(tài)選擇對應(yīng)值的相關(guān)資料,需要的朋友可以參考下2023-01-01
解決vue無法加載文件C:\Users\Administrator\AppData\Roaming\npm\vue.ps
這篇文章主要介紹了解決vue無法加載文件C:\Users\Administrator\AppData\Roaming\npm\vue.ps1因為在此系統(tǒng)上禁止運(yùn)行腳本問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03

