mounted() {
window.addEventListener("scroll", this.handleScroll);
},
beforeDestroy() {
window.removeEventListener("scroll",this.handleScroll)
},
handleScroll() {
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
offsetTop = this.$refs.searchBar.offsetTop;
problemTop = this.$refs.problem.offsetTop;
claimsTop = this.$refs.claims.offsetTop;
if(scrollTop > offsetTop) {
this.searchBarFixed = true;
} else {
this.searchBarFixed = false;
}
if(scrollTop >= problemTop && scrollTop < claimsTop) {
this.current = 1;
} else if(scrollTop >= claimsTop) {
this.current = 2;
} else {
this.current = 0;
}
},
说明
报offsetTop这个错误,是因为我在页面加载的时候给window添加了scroll事件,但是一定要记得销毁,要不然所有的页面都会添加这个scroll事件,在这个页面销毁前移除这个事件就解决了