Pull-up loading is realized by pull-down refresh in mobile terminal

The most common way to implement pull-up loading is to listen to the scrolling events of the scroll bar, while the pull-down refresh of the mobile terminal uses the transform attribute to shift, so how about using the pull-down refresh to implement pull-up loading?

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

Here we make two main boxes, and implement pull-up loading in the two boxes. The structure is simple.

  • css Style
        * {
            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 is set for the outermost box; no height is set for the middle box, which is supported by the sub box item.

  • js code
    /*Drop-down loading*/
    function tDscroll(obj) {
        this.key = true;                  //Prevent duplicate requests
        this.dom = obj.dom;               //Incoming dom
        this.fn = obj.fn;                 //Callback function
        this.outDom = this.dom.querySelector(".popup-box");  //Get content box
        this.showHeight = dom.offsetHeight;                  //Height displayed
        this.actualHeight = this.outDom.offsetHeight;             //Get content at actual height
        this.startY = 0;                               //Start click position
        this.changedY = 0;                             //How far the fingers move
        this.originY = 0;                              //Offset
        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 "); / / get dom
    var dom2 = document.querySelector("#box2 "); / / get dom
    var obj = {
        dom : dom,
        fn : add
    };
    var obj2 = {
        dom : dom2,
        fn : add
    };
    new tDscroll(obj);
    new tDscroll(obj2);
    var page = 0;                    //Current number of pages (for simulation)

    // Simulated 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");              //Get promoted
            tips && outDom.removeChild(tips); //If not for the first time
            str += "<div class='tips'>Load more</div>";
            outDom.innerHTML += str;
            that.actualHeight = outDom.offsetHeight;
            that.key = true;
        },2000)
    }

The principle is also very simple. Monitor gesture events to determine whether the distance is enough to add data~~~

Tags: Front-end Mobile Attribute

Posted on Mon, 02 Dec 2019 23:40:21 -0800 by mona02