vue 实现滚动到底部翻页效果(pc端)


Posted in Javascript onJuly 31, 2019

pc端vue 滚动到底部翻页 效果,具体内容如下所示:

html:

<div class="list" ref="scrollTopList">
                <div class="listsmall" v-for="(item,index) of list" :key="index" @click="getDeviceInfo(item.id)">
                  <span class="state" :class="{'state1':item.status==1,'state0':item.status==0,'state2':item.status==2,'state3':item.status==3}"></span>
                  <span class="text textcolor">【{{item.code||item.name}}】</span>
                  <span class="text">{{item.name}}</span>
                </div>
              </div>

js:

先写滚动事件

handleScroll(){
        let scrollTop = this.$refs.scrollTopList.scrollTop, 
        clientHeight = this.$refs.scrollTopList.clientHeight, 
        scrollHeight = this.$refs.scrollTopList.scrollHeight,
        height = 50; //根据项目实际定义
        if(scrollTop +clientHeight >= scrollHeight - height){
          if(this.pageSize > this.total){
            return false
          }else{
            this.pageSize = this.pageSize +10 //显示条数新增
            this.getpageList() //请求列表list 接口方法
          } 
        }else{
          return false
        }
      },

method中写节流函数

throttle(func, wait) {
        let lastTime = null
        let timeout
        return () => {
          let context = this;
          let now = new Date();
          let arg = arguments;
          if (now - lastTime - wait > 0) {
            if (timeout) {
              clearTimeout(timeout)
              timeout = null
            }
            func.apply(context, arg)
            lastTime = now
          } else if (!timeout) {
            timeout = setTimeout(() => {
              func.apply(context, arg)
            }, wait)
          }
        }
      },

mounted中调用

mounted(){
this.$refs.scrollTopList.addEventListener("scroll",this.throttle(this.handleScroll,500),true)
},

//-------------------------------------------------------------------------------------------第二种写法

html:

添加滚动事件

<div class="tablelist-box" @scroll="scrollEvent($event)">
        <div
         class="tablelist"
         :class="{'active':listDevicesDetailIndex==index}"
         v-for="(item,index) of deviceList"
         :key="index"
         v-if="deviceList.length !== 0"
         @click="deviceDetail(item,index)"
        >
         <span class="tablelist-status">
          <i
           :class="{zx:item.status==1,lx:item.status==2, wjh:item.status==0,gj:item.status==3}"
          ></i>
         </span>
         <span class="tablelist-bg">{{item.code != null ?item.code:"/"}}</span>
        </div>
        <div class="list-more" v-show="!deviceListIsFinish">{{deviceTip}}</div>
        <div class="list-more" v-show="deviceListIsFinish">{{deviceTip}}</div>
       </div>

 css:

tablelist-box{
 height: //根据实际项目取
 overflow:auto //必须 不然判断有问题
}

css 定义

js

写入滚动事件

scrollEvent(e) {
   if (e instanceof Event) {
    let el = e.target;
    let scrollTop = el.scrollTop;
    // 获取可视区的高度
    let clientHeight = el.clientHeight;
    // 获取滚动条的总高度
    let scrollHeight = el.scrollHeight;
    let height = 50;
    //到底了
    // console.log(this.deviceListIsLoad, this.deviceListIsFinish);
    // console.log(scrollTop, clientHeight, scrollHeight);
    //是否继续加载且已完成加载
    if (
     scrollTop + clientHeight >= scrollHeight &&
     this.deviceListIsLoad &&
     !this.deviceListIsFinish
    ) {
     // 把距离顶部的距离加上可视区域的高度 等于或者大于滚动条的总高度就是到达底部
     this.deviceListIsLoad = true;
     console.log("到底了");
     setTimeout(() => {
      this._deviceListPage();
     }, 1000);
    }
   }

请求列表的处理

_deviceListPage() {
   let params = {
    pageSize: this.devicePageSize,
    pageNum: this.devicePageNum,
    kw: "", //查询条件(通配查询条件)
    type: this.deviceType, //设备类型(下拉)2.1.6接口获取
    code: this.deviceCode, //设备编号
    areaId: this.deviceareaId, //位置区域
    status: this.deviceStatus, //状态 1:在线(正常),0:未激活,2已离线,3.告警
    imei: "" //imei编号
   };
   deviceListPage(params).then(res => {
    if (res.code == 200) {
     this.devicePageTotal = res.body.total;
     this.devicePageSize = res.body.pageSize;
     this.devicePageNum = res.body.pageNum;
     this.devicePageTotalPages = parseInt(
      (this.devicePageTotal + this.devicePageSize - 1) /
       this.devicePageSize
     );
     if (this.devicePageTotal == 0) {
      // console.log("没有数据");
      //没有数据
      this.deviceListnodata = true;
      this.deviceListIsLoad = false;
      this.deviceListIsFinish = true;
      this.devicePageNum = 1;
      this.deviceTip = "暂无数据";
      return false;
     }
     this.deviceList = this.deviceList.concat(res.body.datas);
     // console.log(this.devicePageNum, this.devicePageTotalPages);
     if (this.devicePageNum == this.devicePageTotalPages) {
      //没有更多
      this.deviceListIsLoad = false;
      this.deviceListIsFinish = true;
      this.devicePageNum = 1;
      this.deviceTip = "没有更多了~";
      // console.log("没有更多了");
     } else {
      // console.log("下一页");
      //下一页
      this.deviceListIsLoad = true;
      this.deviceListIsFinish = false;
      this.devicePageNum++;
      this.deviceTip = "正在加载中~";
     }
     // console.log("deviceList", this.deviceList);
    } else {
     // this.deviceList = [];
     this.deviceListIsLoad = false;
     this.deviceListIsFinish = true;
     this.devicePageNum = 1;
     this.deviceTip = "数据加载失败~";
    }
   });
  },

return中的定义

devicePageSize: 10, //每页显示
   devicePageNum: 1, //当前页
   devicePageTotal: 0, //总条数
   devicePageTotalPages: 0, //总页数
   deviceListIsFinish: false, //是否加载完成
   deviceListIsLoad: false, //是否加载更多
   deviceListnodata: false, //是否有数据
   deviceTip: "",

总结

以上所述是小编给大家介绍的vue 实现滚动到底部翻页效果(pc端),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

Javascript 相关文章推荐
jquery实现的美女拼图游戏实例
May 04 Javascript
实例详解jQuery表单验证插件validate
Jan 18 Javascript
Bootstrap中文本框的宽度变窄并且加入一副验证码图片的实现方法
Jun 23 Javascript
JS中动态创建元素的三种方法总结(推荐)
Oct 20 Javascript
Vue.js双向绑定操作技巧(初级入门)
Dec 27 Javascript
原生js获取浏览器窗口及元素宽高常用方法集合
Jan 18 Javascript
JS表单提交验证、input(type=number) 去三角 刷新验证码
Jun 21 Javascript
简单实现jQuery手风琴效果
Aug 18 jQuery
vue登录注册及token验证实现代码
Dec 14 Javascript
JS合并两个数组的3种方法详解
Oct 24 Javascript
vue 导航菜单刷新状态不消失,显示对应的路由界面操作
Aug 06 Javascript
浅谈JS的二进制家族
May 09 Javascript
js实现一个简易计算器
Mar 30 #Javascript
微信小程序iBeacon测距及稳定程序的实现解析
Jul 31 #Javascript
JS获取动态添加元素的方法详解
Jul 31 #Javascript
JS/jQuery实现超简单的Table表格添加,删除行功能示例
Jul 31 #jQuery
详解Vuex下Store的模块化拆分实践
Jul 31 #Javascript
ES6 Iterator接口和for...of循环用法分析
Jul 31 #Javascript
Vuex 模块化使用详解
Jul 31 #Javascript
You might like
php几个预定义变量$_SERVER用法小结
2014/11/07 PHP
php实现对两个数组进行减法操作的方法
2015/04/17 PHP
Thinkphp5行为使用方法汇总
2017/12/21 PHP
JavaScript在IE和Firefox(火狐)的不兼容问题解决方法小结
2010/04/13 Javascript
获取非最后一列td值并将title设为该值的方法
2013/10/30 Javascript
jquery 快速回到页首的方法
2013/12/05 Javascript
js 判断js函数、变量是否存在的简单示例代码
2014/03/04 Javascript
javascript页面倒计时实例
2015/07/25 Javascript
javascript+canvas实现刮刮卡抽奖效果
2015/07/29 Javascript
浅谈jQuery before和insertBefore的区别
2016/12/04 Javascript
jQuery实现判断控件是否显示的方法
2017/01/11 Javascript
xmlplus组件设计系列之分隔框(DividedBox)(8)
2017/05/02 Javascript
快速解决vue-cli在ie9+中无效的问题
2018/09/04 Javascript
微信小程序环境下将文件上传到OSS的方法步骤
2019/05/31 Javascript
jquery+ajax实现上传图片并显示上传进度功能【附php后台接收】
2019/06/06 jQuery
基于Node.js搭建hexo博客过程详解
2019/06/25 Javascript
JavaScript 空间坐标的使用
2020/08/19 Javascript
Python中用pycurl监控http响应时间脚本分享
2015/02/02 Python
Python使用pygame模块编写俄罗斯方块游戏的代码实例
2015/12/08 Python
Python读写Json涉及到中文的处理方法
2016/09/12 Python
python实现的二叉树定义与遍历算法实例
2017/06/30 Python
Python 结巴分词实现关键词抽取分析
2017/10/21 Python
Django自定义用户认证示例详解
2018/03/14 Python
快速解决pandas.read_csv()乱码的问题
2018/06/15 Python
pyQT5 实现窗体之间传值的示例
2019/06/20 Python
Django中信号signals的简单使用方法
2019/07/04 Python
Python requests及aiohttp速度对比代码实例
2020/07/16 Python
vue.js刷新当前页面的实例讲解
2020/12/29 Python
魔幻般冒泡背景的CSS3按钮动画
2016/02/27 HTML / CSS
HTML5在手机端实现视频全屏展示方法
2020/11/23 HTML / CSS
工商管理实习生自我鉴定范文
2013/12/18 职场文书
上班玩游戏检讨书
2014/02/07 职场文书
讲座通知范文
2015/04/23 职场文书
浅谈react useEffect闭包的坑
2021/06/08 Javascript
JVM钩子函数的使用场景详解
2021/08/23 Java/Android
服务器间如何实现文件共享
2022/05/20 Servers