Vue.js 無限滾動列表性能優(yōu)化方案
問題
大家都知道,Web 頁面修改 DOM 是開銷較大的操作,相比其他操作要慢很多。這是為什么呢?因為每次 DOM 修改,瀏覽器往往需要重新計算元素布局,再重新渲染。也就是所謂的重排(reflow)和重繪(repaint)。尤其是在頁面包含大量元素和復(fù)雜布局的情況下,性能會受到影響。那對用戶有什么實際的影響呢?
一個常見的場景是大數(shù)據(jù)量的列表渲染。通常表現(xiàn)為可無限滾動的無序列表或者表格,當(dāng)數(shù)據(jù)很多時,頁面會出現(xiàn)明顯的滾動卡頓,嚴(yán)重影響了用戶體驗。怎么解決呢?
解決方案
既然問題的根源是 DOM 元素太多,那就想辦法限制元素數(shù)量。
- 限制列表對用戶可見的元素數(shù)量。我們把可見區(qū)域稱為 ViewPort
- 當(dāng)列表滾動時,列表種的其他元素怎么由不可見變?yōu)榭梢姡?/li>
- 監(jiān)聽列表容器元素的滾動事件,當(dāng)列表里的元素進入可視區(qū)域,則添加到DOM中
- 問題是如果一直這么滾下去,列表會越來越大。所以需要在列表元素離開 ViewPort 的時候從DOM中移除
- 問題又來了,由于 ViewPort 剛好是一屏的大小,滾動的時候元素還沒來得及渲染,會出現(xiàn)一段時間的空白。解決辦法就是上下增加一部分?jǐn)?shù)據(jù)渲染。

無限滾動的性能優(yōu)化方案基本思路就是這樣。
在實際項目中,我們可能不需要自己從頭實現(xiàn)一個無限滾動列表組件,Vue.js 就有一個現(xiàn)成的輪子:vue-virtual-scroller。
在項目中安裝這個插件:
$ npm install -D vue-virtual-scroller
項目入口文件 main.js 引入這個插件:
import "vue-virtual-scroller/dist/vue-virtual-scroller.css"; import Vue from "vue"; import VueVirtualScroller from "vue-virtual-scroller"; Vue.use(VueVirtualScroller);
案例一:VirtualList
我們來看一個簡單的例子,用vue-virtual-scroller渲染一個包含大量數(shù)據(jù)的列表。 先用JSON-Generator 生成 5000 條數(shù)據(jù)的 JSON 對象,并保存到 data.json 文件。可以用下面的規(guī)則:
[
'{{repeat(5000)}}',
{
_id: '{{objectId()}}',
age: '{{integer(20, 40)}}',
name: '{{firstName()}} {{surname()}}',
company: '{{company().toUpperCase()}}'
}
]
新建一個 VirtualList.vue 文件,引入data.json,并將它賦值給組件的items屬性。然后套一個 <virtual-scroller>組件:
VirtualList.vue:
<template>
<virtual-scroller :items="items" item-height="40" content-tag="ul">
<template slot-scope="props">
<li :key="props.itemKey">{{props.item.name}}</li>
</template>
</virtual-scroller>
</template>
<script>
import items from "./data.json";
export default {
data: () => ({ items })
};
</script>
virtual-scroller 組件必須設(shè)置 item-height 。另外,由于我們要創(chuàng)建一個列表,可以設(shè)置content-tag="ul",表示內(nèi)容渲染成 <ul>標(biāo)簽。
vue-virtual-scroller 支持使用 scoped slots,增加了內(nèi)容渲染的靈活性。通過使用slot-scope="props",我們可以訪問 vue-virtual-scroller 暴露的數(shù)據(jù)。
props 有一個itemKey屬性,出于性能考慮,我們應(yīng)該在內(nèi)容部分的根元素上綁定 :key="props.itemKey"。然后我們就可以通過 props.item 拿到 JSON 里的原始數(shù)據(jù)了。
如果你要給列表設(shè)置樣式,可以給 virtual-scroller 設(shè)置 class屬性:
<template>
<virtual-scroller class="virtual-list" ...></virtual-scroller>
</template>
<style>
.virtual-list ul {
list-style: none;
}
</style>
或者也可以用scoped 樣式,用 /deep/選擇器:
<style scoped>
.virtual-list /deep/ ul {
list-style: none;
}
</style>
案例二: VirtualTable
類似 VirtualList,我們再看一個表格組件VirtualTable: VirtualTable.vue:
<template>
<virtual-scroller :items="items" item-height="40" content-tag="table">
<template slot-scope="props">
<tr :key="props.itemKey">
<td>{{props.item.age}}</td>
<td>{{props.item.name}}</td>
<td>{{props.item.company}}</td>
</tr>
</template>
</virtual-scroller>
</template>
<script>
import items from "./data.json";
export default {
data: () => ({ items })
};
</script>
這里有個小問題,我們需要增加一個 <thead>標(biāo)簽,用于顯示列名: Age, Name 和 Company
幸好 virtual-scroller 支持 slot,可以定制各部分內(nèi)容:
<main> <slot name="before-container"></slot> <container> <slot name="before-content"></slot> <content> <!-- Your items here --> </content> <slot name="after-content"></slot> </container> <slot name="after-container"></slot> </main>
這些 slot 都可以放置自定義內(nèi)容。container 會被 container-tag 屬性值替換,默認(rèn)是div,content 被 content-tag 值替換。
這里用 before-content slot 加一個thead 就行了:
<template>
<virtual-scroller
:items="items"
item-height="40"
container-tag="table"
content-tag="tbody"
>
<thead slot="before-content">
<tr>
<td>Age</td>
<td>Name</td>
<td>Company</td>
</tr>
</thead>
<template slot-scope="props">
<tr :key="props.itemKey">
<td>{{props.item.age}}</td>
<td>{{props.item.name}}</td>
<td>{{props.item.company}}</td>
</tr>
</template>
</virtual-scroller>
</template>
請注意,我們把content-tag="table" 改成了content-tag="tbody",因為我們設(shè)置了container-tag="table",這是為了構(gòu)造table 標(biāo)簽的常規(guī)結(jié)構(gòu)。
如果要加一個 tfoot,應(yīng)該知道怎么做了吧。
總結(jié)
我們了解了無限滾動列表的性能優(yōu)化原理,以及利用vue-virtual-scroller Vue 插件創(chuàng)建了 VirtualList 和 VirtualTable 組件。如果用它們來展示前面生成的 5000 條數(shù)據(jù),應(yīng)該可以比較流暢地渲染和滾動了。更多用法可以參考 vue-virtual-scroller 文檔 。
文章涉及的源碼 戳這里。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解element-ui動態(tài)限定的日期范圍選擇器代碼片段
這篇文章主要介紹了element-ui動態(tài)限定的日期范圍選擇器代碼片段,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
vue中如何使用embed標(biāo)簽PDF預(yù)覽
這篇文章主要介紹了vue中如何使用embed標(biāo)簽PDF預(yù)覽,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
如何使用Vuex+Vue.js構(gòu)建單頁應(yīng)用
這篇文章主要教大家如何使用Vuex+Vue.js構(gòu)建單頁應(yīng)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
Vue包大小優(yōu)化的實現(xiàn)(從1.72M到94K)
這篇文章主要介紹了Vue包大小優(yōu)化的實現(xiàn)(從1.72M到94K),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
vue項目使用.env文件配置全局環(huán)境變量的方法
這篇文章主要介紹了vue項目使用.env文件配置全局環(huán)境變量的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10

