vue?長(zhǎng)列表數(shù)據(jù)刷新的實(shí)現(xiàn)及思考
開(kāi)篇
通過(guò) vue 進(jìn)行列表展示的時(shí)候如果數(shù)據(jù)太多可能會(huì)卡頓,這里通過(guò)滑動(dòng)計(jì)算只創(chuàng)建跟刷新可見(jiàn)部分 dom 元素,這里僅僅代表著復(fù)用思路
一、效果展示
兩列均為局部可視范圍內(nèi)數(shù)據(jù)刷新

二、代碼
實(shí)現(xiàn)的主要思路:
1、提前保留可視div的高度,計(jì)算出可視高度能填滿情況下最少的單元格條數(shù);
2、根據(jù)提供的每個(gè)單元格高度和總數(shù)據(jù)條數(shù)計(jì)算出總的可滑動(dòng)div高度,使其可以滑動(dòng);
3、在上述可滑動(dòng)div內(nèi)部再包裹一層div,此節(jié)點(diǎn)的作用就是在滑動(dòng)過(guò)程中,改變自身的 top 值,使之在父節(jié)點(diǎn)滑動(dòng)中始終出現(xiàn)在父節(jié)點(diǎn)的可視區(qū)域內(nèi)。
4、根據(jù)滑動(dòng)的距離更新vue數(shù)據(jù),但是也只是更新能填滿可視區(qū)域最小條數(shù)。
<div class="main">
<div v-for="(dom) in longListEl" :key="dom.id" class="longList">
<div :id="dom.id" class="contain">
<div style="width: calc(100% - 20px);position: relative;">
<div :style="{height:`${expectationCellHeight}px`}" v-for="(item,index) in dom.data" :key="item.id">
{{ item.title }}
</div>
</div>
</div>
</div>
</div>
export default {
data() {
return {
longListData:[],
expectationCellHeight:40,
longListEl:[{id:'contain',data:[]},{id:'contain1',data:[]}],
longListDataManages:[]
};
},
mounted() {
for(let index = 0;index < 250000;index ++){
this.longListData.push({id:index,title:('我是第' + index + '個(gè)')})
}
this.$nextTick(()=>{
this.longListEl.forEach((dom)=>{
let containEl = document.getElementById(dom.id)
if(containEl){
this.longListDataManages.push(new LongListDataManage(containEl,this.longListData,this.expectationCellHeight,(data)=>{
dom.data = data
}))
}
})
})
},
methods: {
},
};
function LongListDataManage(el,totalData,expectationCellHeight,refreshDataCallBack){
this.el = el//需要操作的dom
this.startIndex = 0//數(shù)據(jù)源的開(kāi)始索引
this.endIndex = 0//數(shù)據(jù)源的結(jié)束索引
this.containElHeight = el ? el.offsetHeight : 0//可視區(qū)域高度
this.totalData = totalData//全部數(shù)據(jù)源
this.refreshDataCallBack = refreshDataCallBack//數(shù)據(jù)刷新回調(diào)
this.expectationCellHeight = expectationCellHeight//預(yù)期的單元cell高度
this.expectationCellFullScreenNum = 0//預(yù)期可視區(qū)域展示的單元條數(shù)
this.expectationTotalHeight = 0//預(yù)期滑動(dòng)的最大高度
this.scrollTop = 0//當(dāng)前滾動(dòng)距離
this.beginRefreshDataDistance = expectationCellHeight//滑動(dòng)觸發(fā)刷新數(shù)據(jù)最小距離,這里僅為一個(gè)單元格高度
// 開(kāi)始填充數(shù)據(jù)
this.begin = function(){
this.initData()
this.getCurrentShowDatas()
this.addListenerScroll()
}
// 初始化參數(shù)
this.initData = function(){
//總高度設(shè)定
this.expectationTotalHeight = this.totalData.length * this.expectationCellHeight
this.el.style.height = this.expectationTotalHeight + 'px'
//數(shù)據(jù)源的結(jié)束索引設(shè)定(向上取整)
this.endIndex = this.expectationCellFullScreenNum = Math.ceil((this.containElHeight / this.expectationCellHeight) + 0.1)
}
// 開(kāi)始獲取當(dāng)前需要展示的區(qū)間
this.getCurrentShowDatas = function(){
//截取數(shù)據(jù)源開(kāi)始跟結(jié)束節(jié)點(diǎn)之間的數(shù)據(jù)
let currentData = this.totalData.slice(this.startIndex,this.endIndex)
//給vue提供數(shù)據(jù)
this.refreshDataCallBack(currentData)
}
// 監(jiān)聽(tīng)滾動(dòng)
this.addListenerScroll = function(){
//父節(jié)點(diǎn)
let parentNode = this.el.parentNode
//第一個(gè)子節(jié)點(diǎn)(改變其top值實(shí)現(xiàn)始終在父節(jié)點(diǎn)的可視區(qū)域內(nèi))
let firstChild = this.el.firstChild
let scrollingEvent = ()=>{
let scrollTop = parentNode.scrollTop
//滑動(dòng)的距離超過(guò)規(guī)定的閥值或者滑動(dòng)距離為0進(jìn)行數(shù)據(jù)源重置
if(Math.abs(scrollTop - this.scrollTop) >= this.beginRefreshDataDistance || scrollTop <= 0){
//更改數(shù)據(jù)源區(qū)間索引
this.startIndex = Math.ceil(scrollTop / this.expectationCellHeight)
this.endIndex = this.startIndex + this.expectationCellFullScreenNum
//上下平移可視區(qū)域展示dom,實(shí)現(xiàn)屏幕相對(duì)位置不變
firstChild.style.top = scrollTop + 'px'
//回傳數(shù)據(jù)源
this.getCurrentShowDatas()
this.scrollTop = scrollTop
}
}
//開(kāi)始監(jiān)聽(tīng)滾動(dòng)
parentNode.addEventListener("scroll", scrollingEvent)
}
}
復(fù)用其實(shí)是由vue來(lái)處理的,這里僅是將刷新數(shù)據(jù)進(jìn)行了篩選以達(dá)到刷新最小數(shù)據(jù)條數(shù)的目的,這里僅僅是簡(jiǎn)單的實(shí)現(xiàn)思路,希望對(duì)大家有幫助,更多關(guān)于vue 長(zhǎng)列表數(shù)據(jù)刷新的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue面試created中兩次數(shù)據(jù)修改會(huì)觸發(fā)幾次頁(yè)面更新詳解
這篇文章主要為大家介紹了vue面試created中兩次數(shù)據(jù)修改會(huì)觸發(fā)幾次頁(yè)面更新問(wèn)題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Vue3滑動(dòng)到最右驗(yàn)證功能實(shí)現(xiàn)
在登錄頁(yè)面需要啟動(dòng)向右滑塊驗(yàn)證功能,遇到這樣的需求怎么實(shí)現(xiàn)呢,下面小編通過(guò)示例代碼給大家分享Vue3滑動(dòng)到最右驗(yàn)證功能實(shí)現(xiàn),感興趣的朋友一起看看吧2024-06-06
vue的axios使用post時(shí)必須使用qs.stringify而get不用問(wèn)題
這篇文章主要介紹了vue的axios使用post時(shí)必須使用qs.stringify而get不用問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Vue3?封裝?Element?Plus?Menu?無(wú)限級(jí)菜單組件功能的詳細(xì)代碼
本文分別使用?SFC(模板方式)和?tsx?方式對(duì)?Element?Plus?*el-menu*?組件進(jìn)行二次封裝,實(shí)現(xiàn)配置化的菜單,有了配置化的菜單,后續(xù)便可以根據(jù)路由動(dòng)態(tài)渲染菜單,對(duì)Vue3?無(wú)限級(jí)菜單組件相關(guān)知識(shí)感興趣的朋友一起看看吧2022-09-09
Vue替代vuex的存儲(chǔ)庫(kù)Pinia詳細(xì)介紹
這篇文章主要介紹了Vue替代vuex的存儲(chǔ)庫(kù)Pinia,聽(tīng)說(shuō)pinia與vue3更配,便開(kāi)啟了vue3的學(xué)習(xí)之路,pinia 和 vuex 具有相同的功效, 是 Vue 的存儲(chǔ)庫(kù),它允許您跨組件/頁(yè)面共享狀態(tài)2022-09-09
vue.js 中使用(...)運(yùn)算符報(bào)錯(cuò)的解決方法
這篇文章主要介紹了vue.js 中使用(...)運(yùn)算符報(bào)錯(cuò)的解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
使用vue-virtual-scroller遇到的問(wèn)題及解決
這篇文章主要介紹了使用vue-virtual-scroller遇到的問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03

