(VUE)JS判断滚动条滑动到底部加载更多数据,并防止事件多次触发

  • 首先在mounted中添加滚动条scroll监视事件
  mounted() {
    //监视scroll滚动条
    window.addEventListener("scroll", this.isRefresh, true);
  },
  • 在isRefresh函数中,当第一次到达底部时我们将isRefreshBool变量变为false(禁止refresh()加载数据函数多次触发),当我们的refresh()函数中请求完成后再将isRefreshBool设为true(允许到达底部执行refresh()加载数据函数)
methods:{
	 //判断滚动条是否到底部,刷新新的数据
    isRefresh() {
      //变量scrollTop是滚动条滚动时,距离顶部的距离
      var scrollTop =
        document.documentElement.scrollTop || document.body.scrollTop;
      //变量windowHeight是可视区的高度
      var windowHeight =
        document.documentElement.clientHeight || document.body.clientHeight;
      //变量scrollHeight是滚动条的总高度
      var scrollHeight =
        document.documentElement.scrollHeight || document.body.scrollHeight;
      //滚动条到底部的条件
      // console.log(scrollTop + windowHeight + "--" + scrollHeight);
      if (
        scrollTop + windowHeight >= scrollHeight - 200 &&
        this.isRefreshBool
      ) {
        // false防止refresh()加载数据函数多次触发
        this.isRefreshBool = false;
        this.refresh();
      }
    },
     //刷新推荐文章
    refresh() {
      let that = this;
      Axios.get("/blob/getwebindex", {
        params: {
          p: that.pageNum + 1,
        },
        headers: {
          "X-Requested-With": "XMLHttpRequest",
        },
      })
        .then((Response) => {
          if (Response.data.json.code == 200) {
            that.pageNum = that.pageNum + 1;
            that.list = [...that.list, ...Response.data.json.blobList];
          }
          //refresh()加载数据函数可以触发
          that.isRefreshBool = true;
        })
        .catch((error) => {
          console.log(error);
        });
    },
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值