css实现节流demo

68 阅读1分钟

原理: 动画控制按钮‘可点击、被禁止’的时间段,就形成了在一段时间只允许点击一次的效果.

  • 具体代码如下
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .search-btn {
      animation: throttle 2s step-end forwards;
    }

    .search-btn:active {
      animation: none;
    }

    @keyframes throttle {
      from {
        color: blue; 
        pointer-events: none;
      }

      to {
        color: greenyellow;
        pointer-events: all;
      }
    }
  </style>
</head>

<body>
  <div>
    <label for="input">
      <input id="input" type="text">
    </label>
    <button class="search-btn">查询</button>
  </div>
</body>
<script>
  let btn = document.getElementsByClassName('search-btn')[0]
  console.log(btn)
  btn.onclick = function(e) {
    console.log('点击了,在执行呢')
  }
  // btn.addEventListener('click', function () {
  // console.log('点击了,在执行呢')
  // })
</script>

</html>