javascript - Vuejs2窗口获取滚动高度

标签 javascript html vue.js

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

所以基本上我正在尝试找到滚动的高度。我有:

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() 不是函数

如何确定滚动的高度?

最佳答案

您需要找到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 });
  }

}

关于javascript - Vuejs2窗口获取滚动高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52682068/

相关文章:

javascript - Express JS 中路由的精确长度正则表达式

javascript - 根据我的项目中的以下要求转换json

javascript - 图片 Alt 和标题属性

javascript - 如何将videojs从一个div复制到另一个div?

javascript - Jssor slider 停留在第一张图片上,直到我单击它

javascript - 如何在 Vuejs 中将类切换到 body 标签?

javascript - 如何使用 faker 和 lodash 从数组中删除项目?

javascript - 我的立方体消失了,我不知道为什么

javascript - AngularJS $http 返回未定义?

javascript - 如何从 Axios 请求中获取值?