<script>
export default {
data() {
return {
oldv: 0,
};
},
mounted() {
this.$nextTick(() => {
// 进入nexTick
var bady = document.getElementById("iii"); // 在html中设置id为iii的div
// console.log("距顶部"+scrollTop+"可视区高度"+windowHeight+"滚动条总高度"+scrollHeight);
bady.onscroll = () => {
// 获取距离顶部的距离
var scrollTop = bady.scrollTop;
if (scrollTop !== 0 && this.oldv == 0) {
this.oldv = scrollTop;
}
// 获取可视区的高度
var windowHeight = bady.clientHeight;
// 获取滚动条的总高度
var scrollHeight = bady.scrollHeight;
console.log(
"距顶部" +
scrollTop +
"可视区高度" +
windowHeight +
"滚动条总高度" +
scrollHeight
);
if (scrollTop + windowHeight >= scrollHeight) {
console.log("到达底部了");
}
if (scrollTop == 0 && this.oldv !== 0) {
console.log("到达头部了");
}
};
});
},
};
</script>
vue监听滚动条滚动到头部或者底部
最新推荐文章于 2024-04-28 16:24:30 发布