上拉加载、下拉刷新

function pullinit(scrollDivId,contentDivId,pullDownDivId,pullUpDivId,pullDownFunc,pullUpFunc){
    var startpos,endpos;
    document.getElementById(scrollDivId).addEventListener("touchstart",function(ex){
        startpos=ex.touches.item(0).screenY;
    });
    document.getElementById(scrollDivId).addEventListener("touchmove",function(ex){
        var nowpos=ex.changedTouches.item(0).screenY;
        var nowheight=nowpos-startpos;
        if(nowheight>0){
            if(document.body.scrollTop==0){
                ex.preventDefault();
                if(nowheight<50){
                    $("#"+pullDownDivId).html('<img src="../../../img/arrow-down-mine.jpg">&nbsp;'+"下拉刷新");
                    $("#"+pullDownDivId).height(nowheight);
                }else{
                    $("#"+pullDownDivId).html('<img src="../../../img/arrow-up-mine.jpg">&nbsp;'+"释放立即刷新");
                    $("#"+pullDownDivId).height(nowheight);
                }
            }
        }else{
            if(document.body.scrollHeight<=document.body.offsetHeight+document.body.scrollTop+2){
                ex.preventDefault();
                $("#"+pullUpDivId).html("正在加载");
            }
        }
    });
    document.getElementById(scrollDivId).addEventListener("touchend",function(ex){
        endpos=ex.changedTouches.item(0).screenY;
        var finalHeight=endpos-startpos;
        if(finalHeight>0){
            if(document.body.scrollTop==0){
                if(finalHeight>50){
                    $("#"+pullDownDivId).html('<div style="text-align:center;vertical-align: middle;"><img src="../../../img/loading-mine.gif" style="margin-bottom:-7px;">&nbsp;'+"正在加载"+'</div>');
                    $("#"+pullDownDivId).height(3+"em");
                    $("#"+pullDownDivId).width("100%");
                    //下拉刷新
                    pullDownFunc();
                }
            }
        }else{
            if(document.body.scrollHeight<=document.body.offsetHeight+document.body.scrollTop+2){
                //上拉加载
                pullUpFunc();
            }
        }
        $("#"+pullDownDivId).html("");
        $("#"+pullDownDivId).height(1+"em");
        $("#"+pullDownDivId).width("100%");
    },false);
}

猜你喜欢

转载自blog.csdn.net/u014161595/article/details/79013313