自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Element 穿梭框性能優(yōu)化

開發(fā) 前端
穿梭框處理大數(shù)據(jù)量時,由于渲染的 DOM 節(jié)點過多,造成頁面卡頓的問題。在盡量不改變組件原有邏輯的前提下,進(jìn)行優(yōu)化。

[[415475]]

本文轉(zhuǎn)載自微信公眾號「微醫(yī)大前端技術(shù)」,作者陳建波。轉(zhuǎn)載本文請聯(lián)系微醫(yī)大前端技術(shù)公眾號。

element 穿梭框性能優(yōu)化

背景

穿梭框處理大數(shù)據(jù)量時,由于渲染的 DOM 節(jié)點過多,造成頁面卡頓的問題。在盡量不改變組件原有邏輯的前提下,進(jìn)行優(yōu)化。

解決思路

懶加載 - InfiniteScroll 組件

先從 packages/transfer 中將原組件拷出(或者改源碼重新打包維護(hù)私有庫使用

  1. v-infinite-scroll="pageDown" 
  2. :infinite-scroll-immediate="false" 

添加到

  1. <el-checkbox-group 
  2.         v-show="!hasNoMatch && data.length > 0" 
  3.         v-model="checked" 
  4.         :size="size" 
  5.         :class="{ 'is-filterable': filterable }" 
  6.         class="el-transfer-panel__list" 
  7.         v-infinite-scroll="pageDown" 
  8.         :infinite-scroll-immediate="false" 
  9.       > 
  10.         <el-checkbox 
  11.           class="el-transfer-panel__item" 
  12.           :label="item[keyProp]" 
  13.           :disabled="item[disabledProp]" 
  14.           :key="item[keyProp]" 
  15.           v-for="item in filteredData"
  16.             <option-content :option="item"></option-content> 
  17.         </el-checkbox> 
  18. </el-checkbox-group

 

 

在data中定義pageSize: 20 用來表示每頁數(shù)據(jù)個數(shù)showData: [] 僅用來展示使用,替換上述代碼中實際需要操作的數(shù)據(jù) filteredData

  1. v-for="item in showData"

同時在watch中相應(yīng)的處理

  1. data (data) { 
  2.     const checked = []; 
  3.     this.showData = data.slice(0, this.pageSize); 
  4.  
  5.     const filteredDataKeys = this.filteredData.map( 
  6.     (item) => item[this.keyProp] 
  7.     ); 
  8.     this.checked.forEach((item) => { 
  9.     if (filteredDataKeys.indexOf(item) > -1) { 
  10.         checked.push(item); 
  11.     } 
  12.     }); 
  13.     this.checkChangeByUser = false
  14.     this.checked = checked; 
  15. }, 
  16. filteredData (filteredData) { 
  17.     this.showData = filteredData.slice(0, this.pageSize); 
  18.  } 

初始化展示數(shù)量隨意這里取 20。

最后添加滾動到底部時調(diào)用的方法

  1. pageDown () { 
  2.     const l = this.showData.length; 
  3.     const totalLength = this.filteredData.length 
  4.     l < totalLength &&  
  5.     (this.showData = this.filteredData.slice(0, l + this.pageSize > totalLength ? 
  6.     totalLength : l + this.pageSize)); 
  7. }, 

往下滾動的時候 展示的數(shù)據(jù)長度增加 20(數(shù)量隨意), 超出時展示最大長度。

由此基本解決大數(shù)據(jù)量操作卡頓的問題。由于展示和邏輯層分開,組件的所有操作邏輯無須修改,最小程度減少差異。

新問題

手動滾動到列表末端,再進(jìn)行搜索操作依然存在卡頓問題。

進(jìn)階

在滾動過程中,實際上頂端的數(shù)據(jù)依舊無法看見,該數(shù)據(jù)不展示,對用戶體驗也沒有影響, 所以只需展示當(dāng)前頁的 20 條數(shù)據(jù)。我們?yōu)閑l-checkbox-group添加一個 ref=scrollContainer 以便操作滾動條,

在data中定義當(dāng)前頁數(shù) curIndex: 1

并對 pageDown 方法進(jìn)行修改

  1. pageDown () { 
  2.   const totalLength = this.filteredData.length 
  3.   if((this.curIndex*this.pageSize) < totalLength){ 
  4.     this.curIndex ++ 
  5.     const targetLength = this.curIndex * this.pageSize  
  6.     const endPoint = targetLength > totalLength ? totalLength : targetLength 
  7.     const startPoint = endPoint - this.pageSize  > 0 ? endPoint - this.pageSize : 0 
  8.     this.showData = this.filteredData.slice(startPoint, endPoint); 
  9.     this.$refs.scrollContainer.$el.scrollTop = "1px" //滾動條到最上端,銜接下一頁,為 0 可能會觸發(fā)邊界問題 
  10.   } 

為此我們還需要添加向上翻頁的方法 InfiniteScroll 指令 只提供向下滾動,我們可以拓展該指令亦可自行添加上滑滾動監(jiān)聽

  1. mounted(){ 
  2.         this.$refs.scrollContainer.$el.addEventListener('scroll', this.pageUp) 
  3.     }, 
  4.     beforeDestroy(){ 
  5.         this.$refs.scrollContainer.$el.removeEventListener('scroll', this.pageUp) 
  6.     }, 

注冊pageUp 方法

  1. pageUp(e){ 
  2.       if(e.target.scrollTop ===0 && this.curIndex>1){ 
  3.         this.curIndex -- 
  4.         const endPoint = this.curIndex * this.pageSize  
  5.         const startPoint = (this.curIndex-1)* this.pageSize  
  6.         this.showData = this.filteredData.slice(startPoint, endPoint); 
  7.         const el = this.$refs.scrollContainer.$el 
  8.         el.scrollTop = el.scrollHeight - el.clientHeight - 1 // 滾動到最底部,銜接上一頁, -1 防止邊界問題。 
  9.       } 
  10.     }, 

當(dāng)進(jìn)行數(shù)據(jù)操作的時候,頁面內(nèi)容變化,滾動條也會隨之變化,為防止不能預(yù)知的翻頁,數(shù)據(jù)改變時,重置滾動條和當(dāng)前頁碼。

  1. initScroll(){ 
  2.         this.curIndex = 1 
  3.         this.$refs.scrollContainer.$el.scrollTop = 0 
  4.     }, 

同時地,在watch中相應(yīng)時候執(zhí)行 initScroll

  1. data(){ 
  2.       ... 
  3.       this.initScroll() 
  4.       ... 
  5.   }, 
  6.   filteredData (filteredData) { 
  7.     ... 
  8.     this.initScroll() 
  9.   } 

 

至此大數(shù)據(jù)量的穿梭框,性能大為改善。

 

責(zé)任編輯:武曉燕 來源: 微醫(yī)大前端技術(shù)
相關(guān)推薦

2014-12-10 10:12:02

Web

2009-09-08 09:45:23

App Engine性

2022-02-16 14:10:51

服務(wù)器性能優(yōu)化Linux

2009-06-16 16:10:59

Hibernate性能

2013-06-09 15:31:35

jQueryjQuery優(yōu)化性能優(yōu)化

2020-09-19 21:26:56

webpack

2017-08-08 09:45:43

Python性能優(yōu)化

2021-11-29 11:13:45

服務(wù)器網(wǎng)絡(luò)性能

2011-08-03 16:51:01

jQuery

2021-05-12 06:02:56

性能優(yōu)化工具WebPageTest

2021-07-29 14:20:34

網(wǎng)絡(luò)優(yōu)化移動互聯(lián)網(wǎng)數(shù)據(jù)存儲

2021-05-10 08:08:25

工具LightHouse性能優(yōu)化

2020-10-19 19:45:58

MySQL數(shù)據(jù)庫優(yōu)化

2010-03-02 09:53:14

MySQL性能優(yōu)化

2019-11-01 14:00:58

前端性能優(yōu)化代碼

2020-03-23 15:15:57

MySQL性能優(yōu)化數(shù)據(jù)庫

2020-10-16 09:00:12

前端開發(fā)技術(shù)

2023-11-19 23:24:21

Golang開發(fā)

2024-03-07 11:03:21

ElasticseaES索引

2017-07-14 10:51:37

性能優(yōu)化SQL性能分析
點贊
收藏

51CTO技術(shù)棧公眾號