vue虛擬化列表封裝的實(shí)現(xiàn)
vue虛擬化列表封裝
將下面代碼復(fù)制一份到自己的項(xiàng)目中
<template>
<div class="scrollParent" ref="scrollContent" @scroll="handleScroll">
<div :style="blankFillStyle">
<div v-for="item,index in showDataList" :key="index">
<slot :everyVirtual="item"></slot>
</div>
</div>
</div>
</template><script>
export default {
? ? props:["oneHeight","virtualList"],
? ? data () {
? ? ? ? return {
? ? ? ? ? ? contentSize:"", //可視區(qū)域可以展示多少條數(shù)據(jù)
? ? ? ? ? ? startIndex:0, //記錄當(dāng)前滾動的第一個(gè)元素的索引
? ? ? ? ? ? currentScroll:0, ?//記錄當(dāng)前滾動的距離
? ? ? ? }
? ? },
? ? methods:{
? ? ? ? // 獲取可視區(qū)域可以展示多少條
? ? ? ? getContentSize(){
? ? ? ? ? ? // 兩次取反可以獲取到整數(shù)部分
? ? ? ? ? ? this.contentSize = ~~(this.$refs.scrollContent.offsetHeight / this.oneHeight) + 2;
? ? ? ? },
? ? ? ? // 監(jiān)聽滾動條
? ? ? ? handleScroll(){
? ? ? ? ? ? // 持續(xù)滾動 ?減少變量重新賦值 ?優(yōu)化處理 只有在到下一個(gè)元素節(jié)點(diǎn)的時(shí)候才會重新給this.statrIndex賦值 ?避免和this.startIndex相關(guān)聯(lián)的數(shù)據(jù)再次計(jì)算
? ? ? ? ? ? this.currentScroll = this.$refs.scrollContent.scrollTop;
? ? ? ? ? ? let currentIndex = ~~(this.$refs.scrollContent.scrollTop/this.oneHeight);
? ? ? ? ? ? if(this.startIndex == currentIndex){
? ? ? ? ? ? ? ? return
? ? ? ? ? ? }
? ? ? ? ? ? this.startIndex = currentIndex;
? ? ? ? ? ? if((this.startIndex + this.contentSize - 1)>this.virtualList.length-1){ //說明到達(dá)底部了
? ? ? ? ? ? ? ? this.$emit("scrollEnd")
? ? ? ? ? ? }
? ? ? ? }
? ? },
? ? activated(){
? ? ? ? this.$nextTick(()=>{
? ? ? ? ? ? this.$refs.scrollContent.scrollTop = this.currentScroll;
? ? ? ? })
? ? },
? ? computed:{
? ? ? ? endIndex(){ //獲取最后一個(gè)元素的索引
? ? ? ? ? ? let endIndex = this.startIndex + this.contentSize*2;
? ? ? ? ? ? if(endIndex>this.virtualList.length-1){
? ? ? ? ? ? ? ? endIndex = this.virtualList.length-1
? ? ? ? ? ? }
? ? ? ? ? ? return endIndex;
? ? ? ? },
? ? ? ? showDataList(){
? ? ? ? ? ? let startIndex = 0;
? ? ? ? ? ? if(this.startIndex<=this.contentSize){
? ? ? ? ? ? ? ? startIndex = 0;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? startIndex = this.startIndex - this.contentSize;
? ? ? ? ? ? }
? ? ? ? ? ? return this.virtualList.slice(startIndex,this.endIndex);
? ? ? ? },
? ? ? ? blankFillStyle(){
? ? ? ? ? ? let startIndex = 0;
? ? ? ? ? ? if(this.startIndex<=this.contentSize){
? ? ? ? ? ? ? ? startIndex = 0;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? startIndex = this.startIndex - this.contentSize;
? ? ? ? ? ? }
? ? ? ? ? ? return{
? ? ? ? ? ? ? ? paddingTop:startIndex * this.oneHeight + "px",
? ? ? ? ? ? ? ? paddingBottom:(this.virtualList.length - this.endIndex) * this.oneHeight +"px"
? ? ? ? ? ? }
? ? ? ? }
? ? },
? ? mounted(){
? ? ? ? window.onresize = this.getContentSize();
? ? ? ? window.orientationchange = this.getContentSize();
? ? }
}
</script><style scoped>
? ? .scrollParent{
? ? ? ? height: 100%;
? ? ? ? width: 100%;
? ? ? ? overflow-y: auto;
? ? }
</style>vue虛擬列表-vue-virtual-scroll-list
使用場景
因?yàn)楣咀隽祟愃朴诎俣染W(wǎng)盤的競品,所以用戶如果上傳了很多的文件,就會造成頁面DOM元素的過多,然后因?yàn)樾枰僮鱀OM元素,所以頁面會變得很卡。所以用虛擬列表來解決。

安裝
安裝的話這個(gè)插件有2個(gè)版本的,一個(gè)是1版本,目前更新到2版本了,二版本功能更加的強(qiáng)大。這里使用了1版本,通俗易懂一點(diǎn)。
npm install --save vue-virtual-scroll-list@1.1.3
使用
在單頁面中導(dǎo)入
import VirtualList from "vue-virtual-scroll-list";
components: {
VirtualList,//注冊組件
}, <div class="content-timeview_box">
<!-- size代表行高 remain代表一次渲染的數(shù)量 -->
<!-- 出現(xiàn)的問題:1.在時(shí)間視圖時(shí)(文件夾視圖只有一個(gè)VirtualList不受影響) 一個(gè)日期代表一個(gè)VirtualList 怎么解決高度問題? -->
<!-- 如果統(tǒng)一高度?一個(gè)日期中的文件數(shù)量少于高度 就會出現(xiàn)VirtualList之間的空白問題 -->
<!-- 應(yīng)該根據(jù)日期下的文件數(shù)量來動態(tài)的綁定每一個(gè)VirtualList的高度 -->
<VirtualList
:size="40"
:remain="17"
:wclass="vuesrollboxviewClass"
:tobottom="toBottom"
style="padding: 0 32px 0 32px"
:style="{height:itembig.items.length>6?scrollbarheight:'200px'}"
>toBottom的方法,這個(gè)地方就很坑,因?yàn)槲抑荒茉?.1.3版本中觸發(fā)這個(gè)方法,1版本的其他版本號我沒有觸發(fā)成功,應(yīng)該還是高度的問題。toBottom:滾動到底部時(shí)觸發(fā),請求下一組數(shù)據(jù)
//滾到底部時(shí)觸發(fā)
//注:此方法在1.0高版本不兼容,只能在官方文檔1.1.3版本中使用
//@1.1.3
toBottom() {
this.infiniteHandler();
},:wclass=“vuesrollboxviewClass”
wclass是自定義的class,我這里的業(yè)務(wù)場景不是每行只有1個(gè)數(shù)據(jù),從上而下排列下來,而是每行根據(jù)分辨率不同,展示5個(gè)或6個(gè),所以得計(jì)算好一次渲染的個(gè)數(shù),需要?jiǎng)討B(tài)的綁定。


小結(jié):還是需要更熟練的掌握原生JS,雖然有各種各樣的框架插件來解決問題,但是碰到業(yè)務(wù)場景更復(fù)雜的時(shí)候呢?所以還是要掌握原生JS,具備自己寫輪子的能力才行。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue中利用prop進(jìn)行父子通信時(shí)的注意事項(xiàng)總結(jié)
這篇文章主要給大家介紹了關(guān)于vue中利用prop進(jìn)行父子通信時(shí)的注意事項(xiàng),文中通過實(shí)例介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01
前端vue如何監(jiān)聽對象或者數(shù)組某個(gè)屬性的變化詳解
這篇文章主要給大家介紹了關(guān)于前端vue如何監(jiān)聽對象或者數(shù)組某個(gè)屬性的變化的相關(guān)資料,在Vue中你可以使用watch或者computed來監(jiān)聽對象或數(shù)組某個(gè)屬性的變化,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03
vue3中watch和watchEffect實(shí)戰(zhàn)梳理
這篇文章主要介紹了vue3中watch和watchEffect實(shí)戰(zhàn)梳理,watch和watchEffect都是vue3中的監(jiān)聽器,但是在寫法和使用上是有區(qū)別的。下文介紹他們之間的方法及區(qū)別,需要的朋友可以參考一下2022-07-07
vue子路由跳轉(zhuǎn)實(shí)現(xiàn)tab選項(xiàng)卡效果
這篇文章主要為大家詳細(xì)介紹了vue子路由跳轉(zhuǎn)實(shí)現(xiàn)tab選項(xiàng)卡效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
如何使用?vxe-table?將行數(shù)據(jù)標(biāo)記為刪除狀態(tài)
vxe-table支持將數(shù)據(jù)標(biāo)記為待刪除狀態(tài),通過調(diào)用setPendingRow方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2025-01-01
你了解vue3.0響應(yīng)式數(shù)據(jù)怎么實(shí)現(xiàn)嗎
這篇文章主要介紹了你了解vue3.0響應(yīng)式數(shù)據(jù)怎么實(shí)現(xiàn)嗎,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06
vue中使用echarts以及簡單關(guān)系圖的點(diǎn)擊事件方式
這篇文章主要介紹了vue中使用echarts以及簡單關(guān)系圖的點(diǎn)擊事件方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
vue3中v-for報(bào)錯(cuò)'item'is?of?type'unknown'的
在寫vue3+ts的項(xiàng)目,得到一個(gè)數(shù)組,需要循環(huán)展示,使用v-for循環(huán),寫完之后發(fā)現(xiàn)有個(gè)報(bào)錯(cuò),接下來通過本文給大家介紹vue3中v-for報(bào)錯(cuò)?‘item‘?is?of?type?‘unknown‘的解決方法,感興趣的朋友一起看看吧2023-11-11

