简易h5下拉刷新插件

前言:第一次写H5页面由于页面中的列表数据需要做分页,搜索了许多相关的下拉刷新插件使用起来都比较复杂,索性自己写了一个较为简单的分页加载相关的插件;
(ps:由于使用过程中考虑的局限性可能会存在一些问题,欢迎大家使用并提出问题,空闲时间我会优化问题)

1.使用pulltorefresh.js

/**
 * Created by yunzao on 2017/12/27.
 */
(function (window) {
    var u = {};
    var slideView = null;
    var moveMaxHeight = 50;//移动多少距离触发
    var tempHeight = 0;
    var scrollTop = 0;//滚动条距离顶部的位置
    var touchStartY = 0;
    var touchMoveY = 0;
    var touchEndY = 0;
    var addHeader = false;//头部是否已添加了
    var addFooter = false;//底部是否已添加了
    var loadMoreSuc = true;//默认加载更多成功
    var loadRefreshSuc = true;//默认下拉刷新加载成功
    var isEmpty = false;//默认是否为空数据页面
    var isNoMore = false;//是否没有更多了
    var emptyMsg = '暂无数据~';//默认的空显示文字

    var loadingHtml = '<img class="pull_loading" src="static/images/loading.png">';

    var refreshHeadHtml =
        "<li id='pullHeader' style='height: " + tempHeight + "px;" +
        "max-height:.8rem;display:flex;align-items: center;justify-content: center;border: none;'>" +
        "<div id='pullHeader_tip' style='margin-left: 10px;'>下拉立即刷新</div>" +
        "</li>";

    var loadMoreHtml =
        "<div id='pullFooter' style='height:.8rem;display:flex;"
        + "align-items: center;justify-content: center;border: none;'>" +
        loadingHtml +
        "<div id='pullHeader_tip' style='margin-left: 10px;'>正在加载...</div>" +
        "</div>";

    var emptyHtml =
        "<div class='emptyView' style='height: 100%;width: 100%;display: flex;flex-direction: column;justify-content: center;align-items: center;border: none;'>" +
        "<img class='emptyImg' src='' style='display: none;width: 3.2rem;height: 2.4rem;'>" +
        "<div class='emptyDiv' style='margin-top: .32rem;margin-left: .6rem;margin-right: .6rem; text-align: center;'>暂无数据~</div>" +
        "</div>";

    u.slide = function (slide) {
        slideView = slide;
        slide.viewObj.on('touchstart', function (e) {
            var _touch = e.originalEvent.targetTouches[0];
            var _y = _touch.pageY;
            touchStartY = _y;

        });

        slide.viewObj.on('touchmove', function (e) {
            var _touch = e.originalEvent.targetTouches[0];
            var _y = _touch.pageY;
            touchMoveY = _y;
            var temp = touchMoveY - touchStartY;
            scrollTop = slide.viewObj.scrollTop();
            // console.log("移动中:" + scrollTop);
            if (scrollTop == 0 && (touchMoveY - touchStartY) > 10 && !isEmpty) {
                if (!addHeader) {
                    addHeader = true;
                    slide.viewObj.children('ul').before(refreshHeadHtml);
                } else {
                    $("#pullHeader").height(temp);
                    if ((touchMoveY - touchStartY) > moveMaxHeight) {
                        $("#pullHeader_tip").text('松手即可刷新');
                    } else if ((touchMoveY - touchStartY) < (moveMaxHeight - 1)) {
                        $("#pullHeader_tip").text('下拉立即刷新');
                    }
                }
            }
        });

        slide.viewObj.on('touchend', function (e) {
            var _touch = e.originalEvent.changedTouches[0];
            var _y = _touch.pageY;
            touchEndY = _y;
            // console.log(touchStartY + "/" + touchEndY)
            //下拉刷新
            if (scrollTop == 0 && !isEmpty) {
                //如果用户下拉出现了正在刷新 然后又往上推 就不执行刷新
                if (touchEndY > touchStartY) {
                    if ((touchEndY - touchStartY) > moveMaxHeight) {
                        if (loadRefreshSuc) {
                            isNoMore = false;
                            $("#pullHeader_tip").text('正在刷新中...');
                            $("#pullHeader_tip").before(loadingHtml);
                            //刷新的时候如果底部有 就需要删除并重置
                            if ($("#pullFooter") != null || $("#pullFooter") != undefined) {
                                $("#pullFooter").remove();
                                addFooter = false;
                                loadMoreSuc = true;
                            }
                            //使用双回调 这里回调给页面开始执行加载更多 页面加载完毕回调到这里
                            slide.refresh(function () {
                                //回调到这里 刷新完毕
                                $("#pullHeader").remove();
                                addHeader = false;
                                loadRefreshSuc = true;
                                // slide.viewObj.removeChild();
                            });
                            loadRefreshSuc = false;
                        }
                    } else {
                        $("#pullHeader").remove();
                        addHeader = false;
                    }
                } else {
                    $("#pullHeader").remove();
                    addHeader = false;
                }
            } else if (scrollTop >= 0) {
                $("#pullHeader").remove();
                addHeader = false;
            }
        });

        //上拉加载
        slide.viewObj.scroll(function () {
            if (!isNoMore) {
                if (!isEmpty) {
                    if (loadMoreSuc) {
                        var scrollTop = $(this).scrollTop();//滚动条的高度,即滚动条的当前位置到div顶部的距离
                        var scrollHeight = $(this)[0].scrollHeight;//滚动的高度
                        var wiewHeight = $(this).height();//div可视区域的高度
                        // console.log(scrollTop + "/" + wiewHeight + "/" + scrollHeight);
                        if (scrollTop + wiewHeight == scrollHeight) {
                            if (!addFooter) {
                                slide.viewObj.children('ul').children('#pullFooter').remove();
                                slide.viewObj.children('ul').append(loadMoreHtml);
                                addFooter = true;
                                loadMoreSuc = false;
                            }
                            //使用双回调 这里回调给页面开始执行加载更多 页面加载完毕回调到这里
                            slide.loadMore(function (isEndLoadMore) {
                                //回调结果为true表示没有更多了 并执行回调完毕
                                if (!isEndLoadMore) {
                                    $("#pullFooter").remove();
                                    addFooter = false;
                                    setTimeout(function () {
                                        loadMoreSuc = true;
                                    }, 1000)
                                } else {
                                    $("#pullFooter").empty();
                                    $("#pullFooter").text('没有更多了~');
                                }
                            });
                        }
                    }
                }
            }
        });
    }

    /**
     * 设置空
     * @param sView         父view
     * @param emptyTxt      空显示文字 默认显示
     * @param emptyImgSrc   空显示图片 默认不显示
     */
    u.slide.empty = function (sView, emptyTxt, emptyImgSrc) {
        if (sView != null) {
            slideView = sView;
        }

        //先将li全部隐藏掉
        slideView.viewObj.children('ul').children('li').hide();
        //如果空显示存在就不在往ul中添加 而是直接更新文字和图片
        if (!$('div').hasClass('emptyView')) {
            slideView.viewObj.children('ul').append(emptyHtml);
        }

        //更新文字和图片
        if (emptyTxt != null && emptyTxt != '') {
            $('.emptyDiv').empty().append(emptyTxt);
        }

        if (emptyImgSrc != null && emptyImgSrc != '') {
            $('.emptyImg').show();
            $('.emptyImg').attr('src', emptyImgSrc);

        }
        
        isEmpty = true;
    }

    //设置不为空的时候
    u.slide.noEmpty = function () {
        $('.emptyView').remove();
        slideView.viewObj.children('ul').children('li').show();
    }

    //设置没有更多数据了
    u.slide.noMore = function (sView) {
        isNoMore = true;
        if (sView != null) {
            slideView = sView;
        }
        slideView.viewObj.children('ul').append(loadMoreHtml);
        $("#pullFooter").empty();
        $("#pullFooter").text('没有更多了~');
    }

    /*end*/
    window.$pulltorefresh = u;
})(window);

2.使用pulltorefresh.css

/*滑动内容框*/
.slide_content {
    width: 100%;
    height: 100%;
    overflow-y: scroll;
}

/*下拉刷新loading的样式*/
.loadEffect {
    width: 20px;
    height: 20px;
    position: relative;
    margin: 0 auto;
    margin-top: 100px;
}

.pull_loading {
    width: 18px;
    height: 18px;
    -webkit-transition-property: -webkit-transform;
    -webkit-transition-duration: 1s;
    -moz-transition-property: -moz-transform;
    -moz-transition-duration: 1s;
    -webkit-animation: rotate 3s linear infinite;
    -moz-animation: rotate 3s linear infinite;
    -o-animation: rotate 3s linear infinite;
    animation: rotate 3s linear infinite;
}

@-webkit-keyframes rotate {
    from {
        -webkit-transform: rotate(0deg)
    }
    to {
        -webkit-transform: rotate(360deg)
    }
}

@-moz-keyframes rotate {
    from {
        -moz-transform: rotate(0deg)
    }
    to {
        -moz-transform: rotate(359deg)
    }
}

@-o-keyframes rotate {
    from {
        -o-transform: rotate(0deg)
    }
    to {
        -o-transform: rotate(359deg)
    }
}

@keyframes rotate {
    from {
        transform: rotate(0deg)
    }
    to {
        transform: rotate(359deg)
    }
}

3.在需要使用的html页面中导入相应的js与css
例:

<!DOCTYPE html>
<html>

<head>
    <title>交易明细</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <meta name="apple-mobile-web-app-status-bar-style" content="black"/>
    <meta name="format-detection" content="telephone=no"/>
    <meta name="format-detection" content="email=no"/>
    <link rel="stylesheet" href="static/css/common.css"/>
    <link rel="stylesheet" href="static/css/style.css"/>
    <link rel="stylesheet" href="static/css/pullToRefresh.css"/>
</head>

<body>
<div class="wrapper white-bg" id="pageView">
    <section class="container">
        <div class="slide_content" id="slideView">
            <ul class="tranList">
                <li v-for="item in datas" class="display_column center_h">
                    <div class="display_row smallText text-black">
                        <div class="flex_1 align_left">{{item.name}}</div>
                        <div class="flex_1 align_right">¥{{item.pay_money}}</div>
                    </div>
                    <div class="display_row x_smallText" style="margin-top: 0.16rem;">
                        <div class="flex_1 align_left">{{item.pay_time}}</div>
                        <div class="flex_1 align_right">{{item.qrcode}}</div>
                    </div>
                </li>
            </ul>
        </div>
    </section>
</div>
</body>
<script type="text/javascript" src="static/js/jquery-2.2.3.min.js"></script>
<script type="text/javascript" src="static/js/layer_mobile/layer.js"></script>
<script type="text/javascript" src="static/js/rem.js"></script>
<script type="text/javascript" src="static/js/main.js"></script>
<script type="text/javascript" src="static/js/vue.js"></script>
<script type="text/javascript" src="static/js/base.js"></script>
<script type="text/javascript" src="static/js/pullToRefresh.js"></script>
</body>
<script type="text/javascript">
    $(function () {
        var appVue = new Vue({
            el: '#pageView',
            data: {
                datas: [
                    {
                        id: 10001,
                        name: '阿斌家-测试',
                        pay_time: '2017.12.12',
                        pay_money: '1.00',
                        qrcode: '1000000001'
                    },
                    {
                        id: 10002,
                        name: '阿斌家-测试',
                        pay_time: '2017.12.12',
                        pay_money: '1.20',
                        qrcode: '1000000002'
                    },{
                        id: 10002,
                        name: '阿斌家-测试',
                        pay_time: '2017.12.12',
                        pay_money: '1.20',
                        qrcode: '1000000002'
                    },{
                        id: 10002,
                        name: '阿斌家-测试',
                        pay_time: '2017.12.12',
                        pay_money: '1.20',
                        qrcode: '1000000002'
                    },{
                        id: 10002,
                        name: '阿斌家-测试',
                        pay_time: '2017.12.12',
                        pay_money: '1.20',
                        qrcode: '1000000002'
                    },{
                        id: 10002,
                        name: '阿斌家-测试',
                        pay_time: '2017.12.12',
                        pay_money: '1.20',
                        qrcode: '1000000002'
                    },{
                        id: 10002,
                        name: '阿斌家-测试',
                        pay_time: '2017.12.12',
                        pay_money: '1.20',
                        qrcode: '1000000002'
                    },{
                        id: 10002,
                        name: '阿斌家-测试',
                        pay_time: '2017.12.12',
                        pay_money: '1.20',
                        qrcode: '1000000002'
                    },{
                        id: 10002,
                        name: '阿斌家-测试',
                        pay_time: '2017.12.12',
                        pay_money: '1.20',
                        qrcode: '1000000002'
                    },{
                        id: 10002,
                        name: '阿斌家-测试',
                        pay_time: '2017.12.12',
                        pay_money: '1.20',
                        qrcode: '1000000002'
                    },{
                        id: 10002,
                        name: '阿斌家-测试',
                        pay_time: '2017.12.12',
                        pay_money: '1.20',
                        qrcode: '1000000002'
                    },{
                        id: 10002,
                        name: '阿斌家-测试',
                        pay_time: '2017.12.12',
                        pay_money: '1.20',
                        qrcode: '1000000002'
                    },{
                        id: 10002,
                        name: '阿斌家-测试',
                        pay_time: '2017.12.12',
                        pay_money: '1.20',
                        qrcode: '1000000002'
                    },
                ],
            },
            mounted() {
                $pulltorefresh.slide({
                    viewObj: $('#slideView'),
                    refresh: function (endRefresh) {
                        console.log("刷新")
                        setTimeout(function () {
                            endRefresh();

                            $pulltorefresh.slide.noMore()
                        }, 2000)
                    },
                    loadMore: function (endLoadMore) {
                        console.log("加载")
                        setTimeout(function () {
                            endLoadMore(true);
                        }, 2000)
                    }
                })

                $pulltorefresh.slide.noMore();
//                $pulltorefresh.slide.empty();
                 $pulltorefresh.slide.empty(null, '啊偶,没有符合的结果,换个词试试', 'static/images/icon_search_empty.png')
            },
            watch: {},
            methods: {}
        })
    });
</script>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 156,757评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,478评论 1 289
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,540评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,593评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,903评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,329评论 1 210
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,659评论 2 309
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,383评论 0 195
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,055评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,337评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,864评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,227评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,820评论 3 231
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,999评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,750评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,365评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,260评论 2 258

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,543评论 25 707
  • 这是我在新年开始的时候发的朋友圈,很开心,我能按照自己道路一步步的走下去。 如果你要问我,会对知识产生焦虑吗?我说...
    蜡笔丢了小新阅读 573评论 1 5
  • 直到再一次翻看S君关于诗一样的创业的文字后,我才感到“真”是那样弥足珍贵,“真”真的是一种力量,而在互联网创业圈,...
    二马行空阅读 191评论 0 1
  • “我要买电脑”我女票说,“没钱呀”我小声的回到,我不好意思看女票,我家境一般般,平时都是花父母的, 我大一女...
    椤怔阅读 122评论 0 1
  • 夜晚的月亮又圆又大 松树下、灌木丛、花廊里幽暗 隔壁睡醒的狗一声低啸 叫什么叫? 书桌的底部一只双头鹤 文集的叶脉...
    蛇神阅读 200评论 0 0