jquery 滚动加载更多列表带loading效果

移动端–滚动加载更多列表带loading效果

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
  <meta name="format-detection" content="telephone=no" />
  <meta name="wpk-bid" content="dta_2_6574">
  <title>滚动加载更多列表</title>
  <style>
    *{margin: 0;padding: 0;}
    a{ text-decoration: none;color: #595959; -webkit-tap-highlight-color: rgba(0,0,0,0);tap-highlight-color: rgba(0,0,0,0);}
    a:focus {text-decoration: none;}
    html,body,.list-wrap {
      height: 100%;
    }
    .list-wrap {
      overflow-x: hidden;
      overflow-y: auto;
    }

    .cell-group .cell-item {
      display: flex;
      background-color: #fff;
      min-height: 130px;
      align-items: center;
      padding: 10px 15px;
      border-bottom: 1px solid #eee;
      color: #272727;
    }

    .cell-group .cell-item:last-child {
      border-bottom: 0;
    }

    .cell-group .cell-item .title {
      flex: 1;
    }

    .cell-group .cell-item .photo {
      width: 64px;
      height: 64px;
      border-radius: 50%;
      overflow: hidden;
    }

    .cell-group .cell-item .checkbox {
      width: 18px;
      height: 18px;
      position: relative;
    }

    .cell-group .cell-item .checkbox input {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      opacity: 0;
    }

    .cell-group .cell-item .checkbox span {
      width: 100%;
      height: 100%;
      border: 1px solid #888;
      border-radius: 50%;
      overflow: hidden;
      background: #fff;
      display: block;
      text-align: center;
      line-height: 16px;
      box-sizing: border-box;
    }

    .cell-group .cell-item .checkbox span i {
      display: none;
    }

    .cell-group .cell-item .checkbox input:checked+span i {
      display: block;
    }

    .cell-group .cell-item .checkbox input:checked+span {
      background-color: #34A737;
      border-color: #34A737;
      color: #fff;
    }

    /*loading*/
    #loading {
      display: flex;
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0);
      position: fixed;
      top: 0;
      left: 0;
      z-index: 9999;
      justify-content: center;
      align-items: center;
    }

    #loading span {
      display: flex;
      border: 2px rgba(0, 0, 0, 0.15) solid;
      border-top: 2px #313131 solid;
      border-radius: 50%;
      -webkit-animation: spCircRot .6s infinite linear;
      animation: spCircRot .6s infinite linear;
      width: 32px;
      height: 32px;
    }

    @-webkit-keyframes spCircRot {
      from {
        -webkit-transform: rotate(0deg);
      }

      to {
        -webkit-transform: rotate(359deg);
      }
    }

    @keyframes spCircRot {
      from {
        transform: rotate(0deg);
      }

      to {
        transform: rotate(359deg);
      }
    }
  </style>
</head>

<body>
  <div class="list-wrap">
    <div class="cell-group list-con">
    </div>
  </div>
  <script src="http://code.jquery.com/jquery-2.2.4.min.js"
    integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
  <script>
  //loading 开关
    const loading = {
      open: function () {
        $('body').append('<div id="loading"><span></span></div>');
      },
      close: function () {
        if (!$('#loading').length) return false
        $('#loading').remove()
      }
    }
    $(function () {
      let content = $('.list-con');
      let loadingMore = true
      let page = 1
      let pageTotal = 3;
      //防抖 (防止抖动) 定时器
      let timer = -1;
      
      // ajax数据
      // 第一次加载
      getList(content)

      // 滚动加载更多
      let windowHeight = $('.list-wrap').outerHeight(); //列表外框高度
      $('.list-wrap').bind('scroll', function () {
        let scrollHeight = $('.list-con').outerHeight(); //列表内容高度
        let scrollTop = $('.list-wrap').scrollTop();
        if (scrollHeight - (scrollTop + windowHeight) <= 20) {  //  滚动条距离触底20像素触发加载
          if (page == pageTotal) {
            loadingMore = false
            if (!$('.finish').length) {
              $('.list-wrap').append('<div class="finish" style="height:40px;text-align: center;">没有更多了</div>');
            }
            return false
          }
          loadingMore = true
          clearTimeout(timer)
          timer = setTimeout(() => {
            page++
            console.log(page)
            getList(content)
          }, 300)
        }
      })

      function getList(ul) {
        if (!loadingMore) return false;
        loading.open()
        // ajax 请求
        setTimeout(function () {
          for (let i = 0; i < 5; i++) {
            let html = '<a class="cell-item" href="javascript:;">'
              + '<div class="title">'
              + '<div>吃肉的羊</div>'
              + '<div>加入时间:2020/07/01 15:22:36</div>'
              + '</div>'
              + '<div>+¥80.00</div>'
              + '</a>'
            ul.append(html)
          }
          loading.close()
        }, 500)
      }
    })
  </script>
</body>

</html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值