<template>
<div>
<div :style="`height:${viewH}px;overflow-y:scroll;`" @scroll="hanldeScroll">
<div :style="{height:scorllH}">
<div class="item_box" :style="`transform:translateY(${offSetY}px); `">
<div v-for="item in list" :key="item.id" class="item">{{item.content}}</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
props: {
data: Array,
viewH: Number,
itemH: Number
},
data() {
return {
lastTime:'',
scorllH: "",
list: [],
offSetY: "",
showNum: ""
};
},
mounted() {
this.scorllH = this.itemH * this.data.length + "px";
this.showNum = this.viewH / this.itemH + 4;
this.lastTime = new Date().getTime();
this.list = this.data.slice(0, this.showNum);
},
methods: {
hanldeScroll(e) {
console.log(e.target.scrollTop);
this.offSetY = e.target.scrollTop - (e.target.scrollTop % this.itemH);
this.list = this.data.slice(
Math.floor(e.target.scrollTop / this.itemH),
Math.floor(e.target.scrollTop / this.itemH) + this.showNum
);
this.lastTime = new Date().getTime();
}
}
};
</script>