学无先后,达者为师

网站首页 Vue 正文

vue 中怎么监听屏幕触底事件

作者:调调啊 更新时间: 2022-11-08 Vue

项目中有一个表格  , 用原生table写的 , 要做下拉加载功能 , 原理就是当屏幕滚到底部时 , 把pageNo++  , 再触发一次请求数据 , 把请求回来的下一页数据合并到前一页数组中 this.list = this.list.concat(data);

created() {
      window.addEventListener('scroll', this.Scrollbottom);  //页面加载时监听滚动事件
   },

destroyed() {
      window.removeEventListener('scroll', this.Scrollbottom) //页面离开后销毁监听事件
   },



// methods中 
Scrollbottom() {
        let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
        let clientHeight = document.documentElement.clientHeight;
        let scrollHeight = document.documentElement.scrollHeight;
        if (scrollTop + clientHeight >= scrollHeight) {
          console.log("滚动到底部了")
          this.pageNo++
          console.log(this.pageNo);
          this.fetchData()

        }
},

原文链接:https://blog.csdn.net/weixin_49577940/article/details/123062116

栏目分类
最近更新