1、在mounted生命周期函数注册滚动条事件

 

mounted() {
  window.addEventListener('scroll', this.windowScroll)
}

2、在methods方法里使用

 

methods: {
  windowScroll() {
     // 滚动条距离页面顶部的距离
     // 以下写法原生兼容
     let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
      console.log(scrollTop);
  }
}

3、实例销毁之前,解绑事件

 

  beforeDestroy() {
      window.removeEventListener('scroll', this.windowScroll)
  }

 

相关文章:

  • 2022-12-23
  • 2021-11-14
  • 2021-08-25
  • 2021-07-10
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
相关资源
相似解决方案