其他分享
首页 > 其他分享> > 点击转换按钮位置特效

点击转换按钮位置特效

作者:互联网

 先看效果

 上代码

<!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>
    * {
      margin: 0;
      padding: 0;
    }

    #container {
      position: relative;
      overflow: hidden;
      margin: 100px auto;
      width: 200px;
      height: 23px;
    }

    .btn {
      position: absolute;
      width: 80px;
      text-align: center;
      background-color: black;
      border-radius: 25px;
      cursor: pointer;
      transition: 1s ease;
      color: white;
      box-sizing: border-box;
      user-select: none;
    }

    .check {
      background-color: white;
      border: 1px solid black;
      color: black;
    }

    .btn2 {
      margin-left: 120px;
    }
  </style>
</head>

<body>
  <div id="container" draggable="true">
    <div class="btn btn1">审核成功</div>
    <div class="btn btn2 check">审核失败</div>
  </div>
</body>

<script>
  let btn1 = document.querySelector(".btn1")
  let btn2 = document.querySelector(".btn2")
  btn1.addEventListener('click', function () {
    btn1.setAttribute('style', 'margin-left:120px');
    btn2.setAttribute('style', 'margin-left:0px');
    setTimeout(() => {
      btn1.setAttribute('style', 'right:120px;transition:none');
      btn2.setAttribute('style', 'right:0px;transition:none');
      btn1.classList.toggle('check')
      btn2.classList.toggle('check')
    }, 990)
  })
  btn2.addEventListener('click', function () {
    btn1.setAttribute('style', 'margin-left:120px');
    btn2.setAttribute('style', 'margin-left:0px');
    setTimeout(() => {
      btn1.setAttribute('style', 'right:120px;transition:none');
      btn2.setAttribute('style', 'right:0px;transition:none');
      btn1.classList.toggle('check')
      btn2.classList.toggle('check')
    }, 990)
  })
</script>

</html>

随手写着玩的 

 

标签:特效,style,setAttribute,check,点击,按钮,margin,btn1,btn2
来源: https://blog.csdn.net/qq_42044542/article/details/117953732