Vue implements scrolling monitoring to load more addEventListener('scroll', this.handleScroll)

Directly on the code:

 mounted() {
    
    
    // 监听滚动事件,然后用handleScroll这个方法进行相应的处理
    window.addEventListener('scroll', this.handleScroll)
  },
  // 离开这个页面销毁滚动条事件,不然会给每一个页面都触发
  beforeDestroy() {
    
    
    window.removeEventListener('scroll', this.handleScroll)
  },
 methods: {
    
    
    handleScroll() {
    
    
      // 设备/屏幕高度
      let clientHeight = document.documentElement.clientHeight || document.body.clientHeight
      // 滚动区域
      let scrollObj = document.getElementsByClassName('content-cent')[0]
      // 滚动区域到头部的距离
      let scrollTop = scrollObj.scrollTop
      // 滚动条的总高度
      let scrollHeight = scrollObj.scrollHeight
      // 滚动条到底部的条件
      if (scrollTop + clientHeight == scrollHeight) {
    
    
        // 滚动区域到头部的距离 + 屏幕高度 = 可滚动的总高度
        this.loadMore()
      }
    }
  }

There window.addEventListener('scroll', this.handleScroll)is a problem here . Not all of them are valid. If you find that scrolling cannot be achieved, please add this sentence:

window.addEventListener('scroll', this.handleScroll, true)

Guess you like

Origin blog.csdn.net/m0_46442996/article/details/110930713