首页 > 其他 > 详细

vue长列表虚拟滚动封装

时间:2021-08-24 20:31:20      阅读:11      评论:0      收藏:0      [点我收藏+]
<template>
<div ref="virtual" class="virtual-empty" :style="{ height: height + ‘px‘ }">
<div class="virtual-container" :style="{ height: clacHeight + ‘px‘, minHeight: height + ‘px‘ }">
<slot :topIndex="topIndex" :bottomIndex="bottomIndex"></slot>
</div>
</div>
</template>

<script>
export default {
name: ‘VirtualScroll‘,
props: {
height: {
required: true,
type: Number,
default: 0
},
childHeight: {
required: true,
type: Number,
default: 0
},
offset: {
required: false,
type: Number,
default: 0
},
data: {
required: true,
type: Array,
default: () => []
}
},
data() {
return {
topIndex: 0,
bottomIndex: 0,
totalHeight: 0,
}
},
mounted() {
this.initScroll()
},
beforeDestroy() {
this.virtualDom.removeEventListener(‘scroll‘, this.debounceScroll, {passive: true})
this.virtualDom = null
},
computed: {
clacHeight() {
let length = this.data.length
let height = length > 0 ? length * this.childHeight > this.height ? length * this.childHeight : this.height : this.height
length = null
return height
}
},
methods: {
initScroll() {
if(!this.height) return
if(!this.childHeight) return
this.virtualDom = this.$refs[‘virtual‘]
this.debounceScroll = _.debounce(this.scroll, 16)
this.virtualDom.addEventListener(‘scroll‘, this.debounceScroll, {passive: true})
this.scroll()
},
scroll() {
let scrollTop = this.virtualDom.scrollTop
this.topIndex = Math.floor((scrollTop - this.offset > 0 ? scrollTop - this.offset : scrollTop) / this.childHeight);
this.bottomIndex = this.topIndex + Math.ceil((this.height + this.offset) / this.childHeight)
scrollTop = null
},
// 重制滚动高度
resetScrollTop() {
this.virtualDom.scrollTop = 0
}
}
}
</script>

<style lang="less">
.virtual-empty {
overflow-y: scroll;
}
.virtual-container {
position: relative;
width: 100%;
}
</style>

vue长列表虚拟滚动封装

原文:https://www.cnblogs.com/zerofan/p/15181661.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!