- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > web開(kāi)發(fā) > JavaScript >
- 微信小程序虛擬列表的實(shí)現示例
大部分小程序都會(huì )有這樣的需求,頁(yè)面有長(cháng)列表,需要下拉到底時(shí)請求后臺數據,一直渲染數據,當數據列表長(cháng)時(shí),會(huì )發(fā)現明顯的卡頓,頁(yè)面白屏閃頓現象。
/* * @Descripttion: * @version: * @Author: shijuwang * @Date: 2021-07-14 16:40:22 * @LastEditors: shijuwang * @LastEditTime: 2021-07-14 17:10:13 */ //Page Object Page({ data: { list: [], // 所有數據 }, //options(Object) onLoad: function (options) { this.index = 0 const arr = [ { idx: this.index++ }, { idx: this.index++ }, { idx: this.index++ }, { idx: this.index++ }, { idx: this.index++ }, ] this.setData({ list: arr }) }, onReachBottom: function () { const arr = [ { idx: this.index++ }, { idx: this.index++ }, { idx: this.index++ }, { idx: this.index++ }, { idx: this.index++ }, ] let { list } = this.data this.setData({ list: [...list, ...arr] }) }, }); // wxml <view class="container"> <view class="item-list" wx:for="{{list}}"> <text class=""> {{item.idx}} </text> </view> </view>
每次觸底重新請求數據,合并數組并重新setData,列表負責時(shí),會(huì )出現卡頓白屏,每次setData的數據越來(lái)越大,增加了通訊時(shí)間,也渲染了過(guò)多的dom元素,如下圖:
1.將一維數組改為二位數組
// wxml <view class="container"> <block wx:for="{{list}}" wx:for-index="pageNuma"> <view class="item-list" wx:for="{{item}}"> <text class="">{{item.idx}}</text> </view> </block> </view> // wx.js Page({ data: { list: [], // 所有數據 }, //options(Object) onLoad: function (options) { this.index = 0; this.currentIndex = 0; // 當前頁(yè)數 pageNuma const arr = [ { idx: this.index++ }, { idx: this.index++ }, { idx: this.index++ }, { idx: this.index++ }, { idx: this.index++ }, ] this.setData({ [`list[${this.currentIndex}]`]: arr }) }, onReachBottom: function () { this.currentIndex++; // 觸底+1 const arr = [ { idx: this.index++ }, { idx: this.index++ }, { idx: this.index++ }, { idx: this.index++ }, { idx: this.index++ }, ] this.setData({ [`list[${this.currentIndex}]`]: arr }) }, });
這樣我們就可以看到,整個(gè)渲染是以屏為分頁(yè)來(lái)渲染,請求幾個(gè)pageNum,那么就渲染幾屏,沒(méi)屏里再進(jìn)行沒(méi)個(gè)列表渲染。
2我們可以只渲染可視區域的一屏、或者渲染可視區域附近的幾屏,其他區域用view占位,不進(jìn)行具體渲染,從而減少dom渲染,減少節點(diǎn)過(guò)多造成的問(wèn)題。
做這一步的話(huà)需要拆分以下幾步:
is.currentIndex = 0; // 當前頁(yè)數 pageNum this.pageHeight = []; // 每屏高度存儲 this.allList = []; // 獲取到的所有數據 this.systemHeight = 0; // 屏幕高度 this.visualIndex = []; // 存儲可視區域pageNum
進(jìn)入頁(yè)面時(shí),掛載相關(guān)數據:
// wxml <view wx:for="{{list}}" wx:for-index="pageNum" id="item{{pageNum}}"> <view class="item-list" wx:for="{{item}}"> <text class="">{{item.idx}}</text> </view> </view> onLoad: function (options) { this.index = 0; this.currentIndex = 0; // 當前頁(yè)數 pageNum this.pageHeight = []; // 每屏高度存儲 this.allList = []; // 獲取到的所有數據 this.systemHeight = 0; // 屏幕高度 const arr = [ { idx: this.index++ }, { idx: this.index++ }, { idx: this.index++ }, { idx: this.index++ }, { idx: this.index++ } ] this.setData({ [`list[${this.currentIndex}]`]: arr }, () => { this.setPageHeight(); }); this.getSystemInfo(); },
給每一屏的view動(dòng)態(tài)的設置id名字,方便在渲染時(shí)獲取每屏高度,并進(jìn)行存儲,為后續不在可視區域占位設置高度使用
循環(huán)pageHeight,相加每個(gè)分頁(yè)高度和當前滾動(dòng)距離進(jìn)行比較,獲取到當前渲染的pageNum,然后把當前選渲染的pageNum -+1,上一屏幕和下一屏幕放入數組中,當前三個(gè)分頁(yè)數據展示,其他屏幕的數據進(jìn)行view占位,高度為存儲高度。
// 滾動(dòng)距離計算 onPageScroll: throttle(function (e) { let pageScrollTop = e[0].scrollTop; let that = this; // 滾動(dòng)計算現在處于那一個(gè)分頁(yè)的屏 let scrollTop = 0; let currentIndex = this.currentIndex; for (var i = 0; i < this.pageHeight.length; i++) { scrollTop = scrollTop + this.pageHeight[i]; if (scrollTop > pageScrollTop + this.systemHeight - 50) { this.currentIndex = i; this.visualIndex = [i - 1, i, i + 1]; that.setData({ visualIndex: this.visualIndex }) break; } } },200)
實(shí)時(shí)監控滾動(dòng),做節流優(yōu)化處理
const throttle = (fn, interval) => { var enterTime = 0; //觸發(fā)的時(shí)間 var gapTime = interval || 300; //間隔時(shí)間,如果interval不傳值,默認為300ms return function () { var that = this; var backTime = new Date(); //第一次函數return即觸發(fā)的時(shí)間 if (backTime - enterTime > gapTime) { fn.call(that, arguments); enterTime = backTime; //賦值給第一次觸發(fā)的時(shí)間 保存第二次觸發(fā)時(shí)間 } }; }
獲取到可視區域數組,然后判斷當前pageNum是否在visualIndex里,是的話(huà)進(jìn)行渲染,不是的話(huà),view占位,高度獲取存儲高度
<wxs module='filter'> var includesList = function(list,currentIndex){ if(list){ return list.indexOf(currentIndex) > -1 } } module.exports.includesList = includesList; </wxs> <view class="container"> <view wx:for="{{list}}" wx:for-index="pageNum" id="item{{pageNum}}" wx:key="pageNum"> <block wx:if="{{filter.includesList(visualIndex,pageNum)}}"> <view class="item-list" wx:for="{{item}}"> <text class="">{{item.idx}}</text> </view> </block> <block wx:else> <view class="item-visible" style="height:{{pageHeight[pageNum]}}px"></view> </block> </view> </view>
在可視區域的數組的pageNum 則循環(huán)當前數組,如果不在的話(huà)就設置高度占位,渲染效果如下:
這種方法就大功告成了!
使用微信小程序api
//視圖監聽(tīng) observePage: function (pageNum) { const that = this; const observerView = wx.createIntersectionObserver(this).relativeToViewport({ top: 0, bottom: 0}); observerView.observe(`#item${pageNum}`, (res) => { console.log(res,'res'); if (res.intersectionRatio > 0) { that.setData({ visualIndex: [pageNum - 1, pageNum, pageNum + 1] }) } }) }
利用oberserve監聽(tīng)當前頁(yè)面是否在在可視區域內,是的話(huà)將當前pageNum -+1,pageNum放入可視區域數組visualIndex中,在wxs進(jìn)行判斷可視區域數組是否存在當前pageNum,是的話(huà)渲染,不存在的話(huà)用view占位。
方案一 滾動(dòng)計算 >>>代碼片段:()
方案二 IntersectionObserver api >>> 代碼片段:
到此這篇關(guān)于微信小程序虛擬列表的實(shí)現示例的文章就介紹到這了,更多相關(guān)小程序虛擬列表內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站