移动端开发之怎么用下拉刷新的方式实现上拉加载

发布时间:2021-02-07 10:52:58 作者:小新
来源:亿速云 阅读:223

这篇文章给大家分享的是有关移动端开发之怎么用下拉刷新的方式实现上拉加载的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

实现上拉加载最普遍的方式就是监听滚动条的滚动事件,而移动端的下拉刷新利用的是transform属性来进行位移,那用下拉刷新的方式实现上拉加载怎么样?

html结构

<div class="main-box" id="box1">
  <div class="popup-box">
  </div>
</div>
<div class="main-box" id="box2">
  <div class="popup-box">
  </div>
</div>

这里我们做了两个主要的盒子,在两个盒子内实现上拉加载。结构很简单。

css样式

* {
      margin: 0;
      padding: 0;
    }
    .main-box {
      background: skyblue;
      width: 100%;
      height: 300px;
      overflow: hidden;
    }
    .popup-box {
      width: 100%;
    }
    .item {
      width: 100%;
      line-height: 40px;
      text-align: center;
      padding: 20px;
      box-sizing: border-box;
    }
    .tips{
      text-align: center;
    }
    #box2 {
      margin-top: 50px;
    }

最外面的盒子设置overflow: hidden;中间盒子不设置高度,靠子盒子item撑起。

js代码

  /*下拉加载*/
  function tDscroll(obj) {
    this.key = true;         //防止重复的请求
    this.dom = obj.dom;        //传入的dom
    this.fn = obj.fn;         //回调函数
    this.outDom = this.dom.querySelector(".popup-box"); //获取内容盒子
    this.showHeight = dom.offsetHeight;         //显示的高度
    this.actualHeight = this.outDom.offsetHeight;       //获取实际高度的内容
    this.startY = 0;                //起始点击位置
    this.changedY = 0;               //手指移动的距离
    this.originY = 0;               //偏移量
    var that = this;
    this.dom.addEventListener("touchstart",function (ev) {
      that.onStart(ev);
    });
    this.dom.addEventListener("touchmove",function (ev) {
      that.onMove(ev);
    });
    this.dom.addEventListener("touchend",function (ev) {
      that.onEnd(ev);
    });
    this.fn.call(this,this.outDom);
  };

  tDscroll.prototype.onStart = function (ev) {
    this.startY = ev.targetTouches[0].clientY;
    var tempArr = window.getComputedStyle(this.outDom).transform.split(",");
    if (tempArr.length > 2) {
      this.originY = parseInt(tempArr[tempArr.length - 1]) || 0;
    }
  };

  tDscroll.prototype.onMove = function (ev) {
    this.changedY = ev.touches[0].clientY - this.startY;
    var changNum = (this.originY + this.changedY);
    var scrollHeight = -changNum + this.showHeight;
    if (changNum > 50)return;
    if (scrollHeight > this.actualHeight + 50)return;
    if (scrollHeight > this.actualHeight - 50 && this.key) {
      this.fn.call(this,this.outDom);
    }
    this.outDom.style.cssText = "transform: translateY(" + changNum + "px);";
  };

  tDscroll.prototype.onEnd = function() {
    if ((this.originY + this.changedY) > 50 ) {
      this.outDom.style.cssText = "transform: translateY(0px);transition:all .3s";
    }
    if (-(this.originY + this.changedY) + this.showHeight > this.actualHeight + 50) {
      this.outDom.style.cssText = "transform: translateY(-"+(this.actualHeight - this.showHeight)+"px);transition:all .3s";
    }
  };


  var dom = document.querySelector("#box1");  //获取dom
  var dom2 = document.querySelector("#box2");  //获取dom
  var obj = {
    dom : dom,
    fn : add
  };
  var obj2 = {
    dom : dom2,
    fn : add
  };
  new tDscroll(obj);
  new tDscroll(obj2);
  var page = 0;          //当前的页数(模拟用)

  // 模拟ajax
  function add(outDom) {
    var that = this;
    this.key = false;
    var str = "";
    for (var i = 1;i < 11;i++) {
      str+="<div class='item'>"+(i+((page)*10))+"</div>"
    }
    page++;
    setTimeout(function () {
      var tips = outDom.querySelector(".tips");       //获取提升
      tips && outDom.removeChild(tips); //如果不是第一次 添加
      str += "<div class='tips'>加载更多</div>";
      outDom.innerHTML += str;
      that.actualHeight = outDom.offsetHeight;
      that.key = true;
    },2000)
  }

原理也是很简单,监听手势事件判断是否距离足够,足够就可以添加数据啦~~~

感谢各位的阅读!关于“移动端开发之怎么用下拉刷新的方式实现上拉加载”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. Android如何实现下拉刷新和上拉加载
  2. vueScroll实现移动端下拉刷新、上拉加载

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

移动端开发

上一篇:怎么编写Vue.js的单元测试

下一篇:怎样抛出Spring Boot注解的异常

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》