watch: {
scrolNumber: {
handler(newName, oldName) {
console.log("新的", newName);
console.log("旧的", oldName);
if (newName > oldName) {
// console.log("原页面消失,新页面出现");
this.newHeader = true;
}
if (newName < oldName) {
// console.log("新页面出现,原页面消失");
this.newHeader = false;
this.oldHeader = true;
}
},
},
},
mounted() {
window.addEventListener("scroll", this.scrollToTop);
},
methods: {
scrollToTop() {
var scrollTop =
window.pageYOffset ||
document.documentElement.scrollTop ||
document.body.scrollTop;
// console.log("6666666", scrollTop);
this.scrolNumber = scrollTop; //scrolNumber 是在data中定义的一个字段
},
},
此时当鼠标向下向上滚动的时候或向上滚动的时候,就会被watch监听到