vue 鼠标到达底部后自动分页查询

在需要是的盒子上添加样式

//添加@scroll="handleScroll"
<div class="wonderful" @scroll="handleScroll"></div>

添加数据

data() {
    return {
      isLoading:false,
      tableFrom: {
        page: 1,
        limit: 4,
      },
}

js文档

method:{
 handleScroll() {
      console.log("进入自动执行分页查询")
      //const container = this.$el; // 获取容器元素
      const container = this.$refs.scrollContainer // 获取容器元素
      const scrollHeight = container.scrollHeight; // 容器内容的总高度
      const scrollTop = container.scrollTop; // 容器顶部隐藏的内容高度
      const clientHeight = container.clientHeight; // 容器可视区域的高度

      // 判断是否滚动到底部
     /* if (scrollTop + clientHeight >= scrollHeight && !this.isLoading) {
        // 加载下一页数据
        this.loadNextPage();
      }*/
      if (scrollHeight - scrollTop - clientHeight <= 100 && !this.isLoading) {
        this.loadNextPage();
      }
    },
    loadNextPage() {
      if(this.wonderfulList.length===4) {
        this.isLoading = true; // 开始加载数据
        console.log("加载下一页")
        // 发起请求获取下一页数据,根据分页逻辑更新currentPage和items列表
        this.tableFrom.page = parseInt(this.tableFrom.page) + parseInt(1);//页数+1
        // 模拟异步加载数据的延迟
        console.log(this.tableFrom.page + '自动加载页数变化')
        setTimeout(() => {
          // 将获取到的数据追加到items列表中
          console.log(this.tableFrom.limit + "自动加载的条件")
          getInformationList(this.tableFrom).then((res) => {
            console.log(res.data.list + "自动加载的数据")
            const newData = res.data.list;
            this.wonderfulList = [...this.wonderfulList, ...newData];
          })
          // 更新currentPage的值
          this.isLoading = false; // 加载完成
        }, 1000);
      }
    },
}

后续添加监视器

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值