【发布时间】:2020-05-29 00:28:21
【问题描述】:
我正在使用nuxt.js 并有一个滚动条,它可以滚动并停止在我页面上的固定点。
当我点击下一页时,该方法仍在寻找 $ref=nav 并返回 undefined 因为它不再存在 Cannot read property 'getBoundingClientRect' of undefined
我可以添加 eventListener 但不能删除 eventListener。
听众
mounted(){
window.addEventListener('scroll', this.stickyScroll);
},
beforeDestroy(){
window.removeEventListener('scroll', this.stickyScroll);
}
滚动条
stickyScroll(){
window.document.onscroll = () => {
let navBar = this.$refs.nav;
let navBarXPosition = navBar.getBoundingClientRect().x;
let navScrollSpy = this.$refs.navScrollSpy;
let navScrollSpyXPosition = navScrollSpy.getBoundingClientRect().bottom;
if(window.scrollY > navBarXPosition && navScrollSpyXPosition > 25){
this.active = true;
} else {
this.active = false;
}
}
},
【问题讨论】:
标签: javascript vue.js nuxt.js