web移动端左滑显示操作按钮

2023-09-23 15:13:58 浏览数 (1)

体验地址 https://codepen.io/klren0312/full/PoYWJgM

示例

2

代码

代码语言:javascript复制
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    body {
      padding: 0;
      margin: 0 auto;
    }
    .container {
      padding: 20px;
      height: 100vh;
      background: #efefef;
    }
    .item {
      position: relative;
      height: 50px;
      margin-bottom: 20px;
      overflow: hidden;
      background: #fff;
    }
    .item .content {
      position: absolute;
      left: 0;
      width: 100%;
      height: 100%;
      padding-left: 20px;
      line-height: 50px;
      z-index: 1;
      box-sizing: border-box;
      background: #fff;
      border: 1px solid #ddd;
      transition: all .5s ease;
    }
    .item .button-group {
      position: absolute;
      right: 0;
      z-index: 0;
    }
    .item .button-group .del-btn {
      height: 50px;
      width: 60px;
      border: 0;
      color: #fff;
      background: #da3d3d;
    }
    .item.active .content {
      left: -60px;
    }
  </style>
</head>
<body>
  <div class="container">
    <!-- 0 默认状态, 1 左滑状态 -->
    <div class="item" data-status='0'>
      <div class="content">测试内容测试内容</div>
      <div class="button-group">
        <button class="del-btn">删除</button>
      </div>
    </div>
    <div class="item" data-status='0'>
      <div class="content">测试内容测试内容</div>
      <div class="button-group">
        <button class="del-btn">删除</button>
      </div>
    </div>
    <div class="item" data-status='0'>
      <div class="content">测试内容测试内容</div>
      <div class="button-group">
        <button class="del-btn">删除</button>
      </div>
    </div>
    <div class="item" data-status='0'>
      <div class="content">测试内容测试内容</div>
      <div class="button-group">
        <button class="del-btn">删除</button>
      </div>
    </div>
  </div>
  <script>
    const items = document.querySelectorAll('.item')
    items.forEach(item => {
      let touchStart, touchEnd
      // 滑动开始, 记录开始位置
      item.addEventListener('touchstart', function (e) {
        touchStart = e.touches[0].clientX
      })
      // 滑动结束
      item.addEventListener('touchend', function (e) {
        let currentEle = e.currentTarget
        touchEnd = e.changedTouches[0].clientX
        // 向左滑, 显示删除按钮
        if (currentEle.dataset.status === '0' && touchEnd - touchStart < -30) {
          currentEle.dataset.status = 1
          currentEle.classList.add('active')
        }
        // 向右滑, 隐藏删除按钮
        if (currentEle.dataset.status === '1' && touchEnd - touchStart > 30) {
          currentEle.dataset.status = 0
          currentEle.classList.remove('active')
        }
      })
    })

    const btns = document.querySelectorAll('.del-btn')
    btns.forEach(btn => {
      // 删除按钮点击事件
      btn.addEventListener('click', function (e) {
        btn.parentElement.parentElement.remove()
      })
    })
  </script>
</body>
</html>

0 人点赞