Vue.js 無限滾動列表性能優(yōu)化方案
問題
大家都知道,Web 頁面修改 DOM 是開銷較大的操作,相比其他操作要慢很多。這是為什么呢?因為每次 DOM 修改,瀏覽器往往需要重新計算元素布局,再重新渲染。也就是所謂的重排(reflow)和重繪(repaint)。尤其是在頁面包含大量元素和復雜布局的情況下,性能會受到影響。那對用戶有什么實際的影響呢?
一個常見的場景是大數(shù)據(jù)量的列表渲染。通常表現(xiàn)為可無限滾動的無序列表或者表格,當數(shù)據(jù)很多時,頁面會出現(xiàn)明顯的滾動卡頓,嚴重影響了用戶體驗。怎么解決呢?
解決方案
既然問題的根源是 DOM 元素太多,那就想辦法限制元素數(shù)量。
- 限制列表對用戶可見的元素數(shù)量。我們把可見區(qū)域稱為 ViewPort
- 當列表滾動時,列表種的其他元素怎么由不可見變?yōu)榭梢姡?/li>
- 監(jiān)聽列表容器元素的滾動事件,當列表里的元素進入可視區(qū)域,則添加到DOM中
- 問題是如果一直這么滾下去,列表會越來越大。所以需要在列表元素離開 ViewPort 的時候從DOM中移除
- 問題又來了,由于 ViewPort 剛好是一屏的大小,滾動的時候元素還沒來得及渲染,會出現(xià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 組件必須設置 item-height 。另外,由于我們要創(chuàng)建一個列表,可以設置content-tag="ul",表示內(nèi)容渲染成 <ul>標簽。
vue-virtual-scroller 支持使用 scoped slots,增加了內(nèi)容渲染的靈活性。通過使用slot-scope="props",我們可以訪問 vue-virtual-scroller 暴露的數(shù)據(jù)。
props 有一個itemKey屬性,出于性能考慮,我們應該在內(nèi)容部分的根元素上綁定 :key="props.itemKey"。然后我們就可以通過 props.item 拿到 JSON 里的原始數(shù)據(jù)了。
如果你要給列表設置樣式,可以給 virtual-scroller 設置 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>標簽,用于顯示列名: 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 屬性值替換,默認是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",因為我們設置了container-tag="table",這是為了構(gòu)造table 標簽的常規(guī)結(jié)構(gòu)。
如果要加一個 tfoot,應該知道怎么做了吧。
總結(jié)
我們了解了無限滾動列表的性能優(yōu)化原理,以及利用vue-virtual-scroller Vue 插件創(chuàng)建了 VirtualList 和 VirtualTable 組件。如果用它們來展示前面生成的 5000 條數(shù)據(jù),應該可以比較流暢地渲染和滾動了。更多用法可以參考 vue-virtual-scroller 文檔 。
文章涉及的源碼 戳這里。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Vuejs對象常用操作之取對應的值、取key和value值、轉(zhuǎn)數(shù)組及合并等
最近在學Vue和javascript感覺js的好多方法都不太清楚,這里徹底總結(jié)下,這篇文章主要給大家介紹了關于Vuejs對象常用操作之取對應的值、取key和value值、轉(zhuǎn)數(shù)組及合并等的相關資料,需要的朋友可以參考下2024-01-01在vue項目中使用axios發(fā)送post請求出現(xiàn)400錯誤的解決
這篇文章主要介紹了在vue項目中使用axios發(fā)送post請求出現(xiàn)400錯誤的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09解決ElementUI中tooltip出現(xiàn)無法顯示的問題
這篇文章主要介紹了解決ElementUI中tooltip出現(xiàn)無法顯示的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03