基于jquery的上拉加载更多

一个简易的基于jquery的上拉加载更多demo,上拉加载的原理就是容器的高度+容器的scrollTop>=容器内容的高度,整个demo还加上了回弹的动效。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>loadmore</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style type="text/css">
        html,body{
            height:100%;
            overflow: auto;
            -webkit-overflow-scrolling: touch;
        }
        html,body,div,ul,li,img,p{
            padding: 0;
            margin: 0;
        }
        .container{
            position: relative;
            width: 100%;
            height: 100%;
            background-color: #eee;
            overflow: auto;
            -webkit-overflow-scrolling: touch;
            box-sizing: border-box;
            --webkit-box-sizing: border-box;
        }
        ul,li{
            list-style-type: none;
        }
        ul{
            height: auto;
        }
        ul li{
            height: 50px;
            padding: 10px;
            border-bottom: 1px solid #eee;
            line-height: 50px;
        }
        .loadmore{
            text-align: center;
            height: 0px;
            line-height: 30px;
            background-color: #999;
            display: none;
            transition: height 1s;
            -moz-transition: height 1s; /* Firefox 4 */
            -webkit-transition: height 1s;  /* Safari 和 Chrome */
            -o-transition: height 1s;   /* Opera */

        }
        .loadmore.show{
            display: block;
            height: 30px;
        }
    </style>
</head>
<body>
    <div class="container">
        <ul>
            <li>1</li>
            <li>4</li>
            <li>3</li>
            <li>4</li>
            <li>4</li>
            <li>2</li>
            <li>gff</li>
            <li>tggf</li>
            <li>1</li>
            <li>4</li>
            <li>3</li>
            <li>4</li>
            <li>4</li>
            <li>2</li>
            <li>gff</li>
            <li>tggf</li>
            <li>1</li>
            <li>4</li>
            <li>3</li>
            <li>4</li>
            <li>4</li>
            <li>2</li>
            <li>gff</li>
            <li>tggf</li>
        </ul>
        <div class="loadmore">加载更多</div>
    </div>

    <script src="http://code.jquery.com/jquery-2.2.4.js"></script>
    <script type="text/javascript">
        $(function(){
            var loadFlag = false;
            var _stary,_endy;
            $(".container").on("touchend",function(){
                $(".container").stop().animate({top:0},80);
                if($(this).height()+$(this).scrollTop()>=$(".container ul").height() && !loadFlag){
                    loadFlag = true;
                    $(".loadmore").addClass("show");
                    $(this).scrollTop(100000000000);
                    var str = "<li>sfsaasdfsafasf</li>";
                    for(var i = 0;i<3;i++){
                        str +=str;
                    }
                    setTimeout(function(){
                        $(".loadmore").removeClass("show");
                        $(".container ul").append(str);
                        loadFlag = false;
                    },1000);
                }
            });
            $(".container").on("touchstart",function(e){
                var _touch = e.originalEvent.targetTouches[0];
                _stary= _touch.pageY;
            });
            $(".container").on("touchmove",function(e){
                var _touch = e.originalEvent.targetTouches[0];
                _endy= _touch.pageY;
                if($(this).height()+$(this).scrollTop()>=$(".container ul").height()){
                    var current_y = _endy - _stary;
                    $(".container").animate({top:current_y/3},5);
                }
            });
        });
    </script>
</body>
</html>
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值