H5实现下拉刷新

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>H5下拉刷新</title>
  <style>
    *{margin:0;padding:0;}
    #freshBox {position:relative;}
    #freshBox .box li {list-style:none;padding-left: 20px;border-bottom:1px solid #eee;height: 60px;line-height:60px;}
    #freshBox .box {position: relative;top: 0;transition: all .5s;background:#fff;}
    #freshBox .fresh-text {position: absolute;top:0;height:60px;width:100%;line-height:60px;text-align:center;color:#ccc;}
  </style>
</head>
<body>
  <div id="freshBox">
    <p class="fresh-text">下拉刷新</p>
    <div class="box">
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>5</li>
      <li>5</li>
      <li>5</li>
      <li>5</li>
      <li>5</li>
    </div>
  </div>
</body>
</html>
<script>
  function $(str) { return document.querySelector(str); }
  let box = $('.box');
  let startY = null;
  let endY = null;
  box.addEventListener('touchstart', (e) => {
    console.log(e);
    startY = e.touches[0].pageY;  // 一定要用pageY,相对页面滑动,不能相对屏幕滑动,相对屏幕滑动会出现bug
  }, false);
  box.addEventListener('touchmove', (e) => {
    let moveY = e.touches[0].pageY - startY;
    console.log(moveY);
    if (moveY > 0 && moveY <= 60) {
      box.style.transform = 'translateY('+ moveY + 'px)'
      moveY > 50 && ($('.fresh-text').innerText = '数据已更新');
    }
    
  }, false);
  box.addEventListener('touchend', (e) => {
    startY = null;
    $('.fresh-text').innerText = '下拉刷新';
    box.style.transform = 'translateY(0px)';
  }, false);
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值