代码
...省略...
mounted() {
window.addEventListener("scroll", this.scrolling);
},
methods: {
scrolling() {
let scrollTop =
window.pageYOffset ||
document.documentElement.scrollTop ||
document.body.scrollTop;
let scrollStep = scrollTop - this.oldScrollTop;
console.log("header 滚动距离 ", scrollTop);
this.oldScrollTop = scrollTop;
let windowHeight =
document.documentElement.clientHeight || document.body.clientHeight;
let scrollHeight =
document.documentElement.scrollHeight || document.body.scrollHeight;
if (scrollTop + windowHeight == scrollHeight) {
console.log("header 你已经到底部了");
}
if (scrollStep < 0) {
console.log("header 滚动条向上滚动了!");
} else {
console.log("header 滚动条向下滚动了!");
}
if (scrollTop <= 0) {
console.log("header 到了最顶部")
}
},
}
beforeDestroy() {
window.removeEventListener("scroll", this.scrolling);
},
...省略 ...
效果