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 相关文章推荐
用javascript实现的图片马赛克后显示并切换加文字功能
Apr 21 Javascript
javascript vvorld 在线加密破解方法
Nov 13 Javascript
formvalidator验证插件中有关ajax验证问题
Jan 04 Javascript
javascipt基础内容--需要注意的细节
Apr 10 Javascript
使用iojs的jsdom库实现同步系统时间
Apr 20 Javascript
jQuery滚动条插件nanoscroller使用指南
Apr 21 Javascript
JavaScript的类型、值和变量小结
Jul 09 Javascript
深入解析AngularJS框架中$scope的作用与生命周期
Mar 05 Javascript
基于javascript制作经典传统的拼图游戏
Mar 22 Javascript
JS中正则表达式要注意lastIndex属性
Aug 08 Javascript
JS基于递归实现网页版计算器的方法分析
Dec 20 Javascript
JS基于设计模式中的单例模式(Singleton)实现封装对数据增删改查功能
Feb 06 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 编程的 5个良好习惯
2009/02/20 PHP
XAMPP安装与使用方法详细解析
2013/11/27 PHP
人脸识别测颜值、测脸龄、测相似度微信接口
2016/04/07 PHP
javascript 写类方式之一
2009/07/05 Javascript
javascript获取网页中指定节点的父节点、子节点的方法小结
2013/04/24 Javascript
兼容主流浏览器的JS复制内容到剪贴板
2014/12/12 Javascript
jQuery中:text选择器用法实例
2015/01/03 Javascript
jQuery实现拖拽效果插件的方法
2015/03/23 Javascript
浅谈javascript语法和定时函数
2015/05/03 Javascript
在AngularJS应用中实现一些动画效果的代码
2015/06/18 Javascript
轻松学习jQuery插件EasyUI EasyUI创建树形网络(1)
2015/11/30 Javascript
javascript实现全角转半角的方法
2016/01/23 Javascript
微信小程序 参数传递实例代码
2017/03/20 Javascript
基于nodejs+express4.X实现文件下载的实例代码
2017/07/13 NodeJs
如何理解Vue的render函数的具体用法
2017/08/30 Javascript
微信小程序实现日期格式化和倒计时
2020/11/01 Javascript
Vue.js计算机属性computed和methods方法详解
2019/10/12 Javascript
vue改变循环遍历后的数据实例
2019/11/07 Javascript
python做量化投资系列之比特币初始配置
2018/01/23 Python
Python 统计字数的思路详解
2018/05/08 Python
python读取excel指定列数据并写入到新的excel方法
2018/07/10 Python
Python实现对特定列表进行从小到大排序操作示例
2019/02/11 Python
详解Python 字符串相似性的几种度量方法
2019/08/29 Python
python+rsync精确同步指定格式文件
2019/08/29 Python
Python字典的概念及常见应用实例详解
2019/10/30 Python
tensorboard显示空白的解决
2020/02/15 Python
matplotlib之pyplot模块坐标轴标签设置使用(xlabel()、ylabel())
2021/02/22 Python
实例讲解CSS3中Transform的perspective属性的用法
2016/04/22 HTML / CSS
学习新党章思想汇报
2014/01/09 职场文书
会议接待欢迎词
2014/01/12 职场文书
九年级家长会邀请函
2014/01/15 职场文书
社团文化节策划书
2014/02/01 职场文书
yy婚礼司仪主持词
2014/03/14 职场文书
优秀员工推荐信
2014/05/10 职场文书
2015新学期开学寄语
2015/02/26 职场文书
Ajax异步刷新功能及简单案例
2021/11/20 Javascript