<div id="scrollDiv" ref="scrollPosition">
</div>
<script>
data() {
return {
isScrollBottom: false,
isScrollTop: true,
pages:10,
currentPage:0,
}
},
mounted() {
const scrollview = this.$refs.scrollPosition;
scrollview.addEventListener("scroll", this.handleScroll, true);
},
beforeDestroy() {
const scrollview = this.$refs.scrollPosition;
scrollview.removeEventListener("scroll", this.handleScroll, false);
},
methods: {
handleScroll() {
let scrollTop = document.querySelector("#scrollDiv").scrollTop;
var windowHeight =
document.documentElement.clientHeight || document.body.clientHeight;
let scrollHeight = document.querySelector("#scrollDiv").scrollHeight;
if (scrollTop + windowHeight === scrollHeight && this.isScrollBottom) {
if (this.currentPage>this.pages) return;
this.currentPage++;
this.isScrollBottom = false;
this.find(0);
}
if (scrollTop === 0 && this.isScrollTop) {
if (this.currentPage =<1) return;
this.currentPage++;
this.isScrollTop = false;
this.find(1); // 滚动到顶部加载
}
},
find(check){
setTimeout(() => {
// 底部加载数据
if (check === 0) {
this.$refs.scrollPosition.scrollTop -=
70 * res.data.data.records.length; //根据自身设定加载数据后需要离底部多远
this.isScrollBottom = true;
}
//顶部加载数据
if (check === 1) {
this.$refs.scrollPosition.scrollTop += /根据自身设定加载数据后需要离定部多远
70 * res.data.data.records.length;
this.isScrollTop = true;
}
}, 200);
}
}
</script>