vue中scroll监听页面滚动、移除监听。

  1. 添加监听
  document.addEventListener("scroll", this.pageScroll); //监听页面滚动
    pageScroll() {
      if (this.timer) clearTimeout(this.timer)
      this.timer = setTimeout(this.scroll(), 300)
    },
    scroll() {
      this.$nextTick(() => {
        let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
        if (scrollTop >= 52) {
          this.background = '#fff'
        } else {
          this.background = '#F7F8FA'
        }
      })
    },
  1. 移除监听
 document.removeEventListener('scroll', this.pageScroll)
 clearTimeout(this.timer)

你可能感兴趣的:(vue,js算法,vue.js,scroll)