挪动端用下拉革新的体式格局完成上拉加载

完成上拉加载最广泛的体式格局就是监听转动条的转动事宜,而挪动端的下拉革新应用的是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)
    }

道理也是很简单,监听手势事宜推断是不是间隔充足,充足就能够增加数据啦~~~

    原文作者:DisplayNone
    原文地址: https://segmentfault.com/a/1190000017311325
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞