vue 实现滚动到页面底部开始加载更多

1,462 阅读1分钟

vue 实现滚动到页面底部开始加载更多

scrollTop 页面滚动的高度, clientHeight 可视区域高度 scrollHeight 可滚动内容的高度

mounted(){
  //添加滚动事件,检测滚动到页面底部
    window.addEventListener('scroll'this.scrollBottom)
},
// 滚动到页面底部时,请求内容
    scrollBottom() {
        this.loading = false
        let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
          let clientHeight = document.documentElement.clientHeight;
          let scrollHeight = document.documentElement.scrollHeight;
        let bottomOfWindow = scrollTop + clientHeight >= scrollHeight-4
        console.log(scrollHeight)
        if (scrollTop!=0&&bottomOfWindow && this.loading == false &&this.finished == false) {
          this.loading = true
          this.offset = this.offset + this.limit;
          let param = {
            offset: this.offset,
            limit: this.limit
          }
          getArea(param).then((res) => {
            if (res.code == 200) {
              this.loading = false
              this.area = this.area.concat(res.data); //追加数据
              if (res.data.length == 0) {
                //数据全部加载完成
                this.finished = true;
              } else {
                this.finished = false;
              }
            }
          })
        }
    }