img

解决vue子组件无法监听到浏览器滚动

2020-09-29 0条评论 2.7k次阅读 JavaScript


代码如下:

<div class="desc" ref="scrollBox"></div>
  mounted() {
    window.addEventListener('scroll', this.handleScroll,true);
  },
  destroyed(){
    window.removeEventListener('scroll', this.handleScroll,true);
  },
  methods: {
    handleScroll() {
      let scrollTop = this.$refs.scrollBox.getBoundingClientRect().top
      console.log(scrollTop)
    },
    // 另一种方法
    handleScroll1() {
      let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
      if (scrollTop > 760) {
        console.log(scrollTop)
      } else {
        console.log(scrollTop)
      }
    },
  }

💬 COMMENT


🦄 支持markdown语法

👋友