Vuejs2窗口滚动高度

时间:2018-10-06 18:20:41

标签: javascript html vue.js

我试图在用户向下滚动时向sticky添加navbar类,并在用户滚动至顶部时删除该类。

所以基本上我想找到滚动的高度。我有:

mounted(){
    this.setUpRoutes();
    window.addEventListener('scroll', this.handleScroll);
},

destroyed () {
    window.removeEventListener('scroll', this.handleScroll);
},

methods:{
    handleScroll(event){
        console.log("handling event scroll here", event.height());
    }
}

但是遇到错误:event.height() is not a function

如何确定滚动的高度?

1 个答案:

答案 0 :(得分:2)

您需要找到scrollTop,而不仅是offsetHeight。

例如:

Get scroll position and direction with vanilla JS

mounted(){
    this.setUpRoutes();
    window.addEventListener('scroll', this.handleScroll);
},
destroyed () {
    window.removeEventListener('scroll', this.handleScroll);
},

methods:{
 handleScroll(event){
    // I think   document.body.scrollTop is not FF compatible, but for example.
    console.log("handling event scroll here", {top: document.body.scrollTop, height: window.innerHeight });
  }

}