按住shift键实现多选

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

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>

  <style>
    ::selection {
      background: transparent;
    }

    #container {
      width: 100%;
      height: 550px;
      display: flex;
    }

    .left {
      width: 500px;
      height: 100%;
      border-right: 1px solid #ccc;
    }

    .left ul li {
      list-style: none;
      height: 50px;
      text-align: center;
      line-height: 50px;
      border-bottom: 1px solid #eee;
      transition: 0.3s all ease-out;
      cursor: pointer;
    }

    .left ul li:hover {
      background: cyan;
    }

    .right {
      flex: 1;
      height: 100%;
    }
  </style>
</head>

<body>
  <div id="container">
    <div class="left">
      <ul id="main">
        <li data-index="0">这是按住shift键多选测试0</li>
        <li data-index="1">这是按住shift键多选测试1</li>
        <li data-index="2">这是按住shift键多选测试2</li>
        <li data-index="3">这是按住shift键多选测试3</li>
        <li data-index="4">这是按住shift键多选测试4</li>
        <li data-index="5">这是按住shift键多选测试5</li>
      </ul>
    </div>
    <div class="right"></div>
  </div>

  <script>
    let selectedIds = []; //存放被选中的数据
    let preIdx = 0;   //被选中的数组的第一个值,用以比较范围

    let ul = document.getElementById("main");
    let lis = document.getElementsByTagName("li");

    ul.addEventListener("click", function (event) {
      selectedIds = [];
      let target = event.target;
      let idx = target.dataset.index;

      if (event.shiftKey) {
        let max = Math.max(preIdx, idx);
        let min = Math.min(preIdx, idx);
        for (let j = min; j <= max; j++) {
          selectedIds.push(j);
        }
        for (let i = 0; i < lis.length; i++) {
          let itemIdx = selectedIds.findIndex(c => c == i);
          if (itemIdx > -1) {
            lis[i].style.background = "cyan";
          } else {
            lis[i].style.background = "transparent";
          }
        }
      } else {
        for (let i = 0; i < lis.length; i++) {
          if (i == idx) {
            lis[idx].style.background = "cyan";
          } else {
            lis[i].style.background = "transparent";
          }
        }
        selectedIds.push(idx);
        preIdx = idx;
      }

      console.log(selectedIds)
    }, false);
  </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值