原生js实现大转盘

<!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>转盘的实现</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }
      ul {
        display: flex;
        flex-wrap: wrap;
        height: 600px;
        width: 600px;
        background-color: #333;
        margin: 100px auto;
      }
      ul li {
        list-style: none;
        height: 200px;
        width: 200px;
        background-color: #888;
        border: 2px solid pink;
        font-size: 20px;
        color: #fff;
        line-height: 200px;
        text-align: center;
      }
      li:nth-child(1) {
        background-color: red;
      }
      li:nth-child(2) {
        background-color: rgb(126, 57, 57);
      }
      li:nth-child(3) {
        background-color: rgb(107, 153, 21);
      }
      li:nth-child(6) {
        background-color: rgb(169, 63, 196);
      }
      li:nth-child(9) {
        background-color: rgb(204, 18, 173);
      }
      li:nth-child(8) {
        background-color: rgb(201, 112, 112);
      }
      li:nth-child(7) {
        background-color: rgb(51, 42, 42);
      }
      li:nth-child(4) {
        background-color: rgb(198, 212, 135);
      }
      button {
        height: 50px;
        width: 50px;
      }
      .select {
        background-color: #000 !important;
      }
    </style>
  </head>
  <body>
    <ul>
      <li>猪</li>
      <li>帅哥</li>
      <li>美女</li>
      <li>富婆</li>
      <li style="display: flex; justify-content: space-around; align-items: center">
        <button>开始</button>
        <button>暂停</button>
        <button>重新</button>
      </li>
      <li>小鲜肉</li>
      <li>大老板</li>
      <li>哈哈哈</li>
      <li>谢谢惠顾</li>
    </ul>
    <script>
      var timer
      var boxList = document.querySelectorAll('li')
      var btns = document.querySelectorAll('button')
      let sortArr = [0, 1, 2, 5, 8, 7, 6, 3]
      var i = -1
      let dd = 500
      btns[0].addEventListener('click', (e) => {
        e.target.innerHTML = '开始'
        e.target.disabled = true
        if (!timer || timer !== undefined) {
          timer = setInterval(() => {
            i++
            if (i === 8) {
              i = 0
            }
            sortArr.forEach((item) => {
              boxList[item].className = ''
            })
            boxList[sortArr[i]].className = 'select'
          }, 20)
          console.log(timer)
        }
        btns[1].addEventListener('click', () => {
          clearInterval(timer)
          e.target.disabled = false
          e.target.innerHTML = '继续'
        })
        btns[2].addEventListener('click', () => {
          e.target.disabled = false
          e.target.innerHTML = '开始'
          i = -1
          clearInterval(timer)
          sortArr.forEach((item) => {
            boxList[item].className = ''
          })
        })
      })
    </script>
  </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

做一个程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值