原生js实现九宫格抽奖动画

原生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>Document</title>
  <script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
  <style>
    .wrap {
      display: flex;
      flex-wrap: wrap;

    }

    .wrap li {
      width: 33.33%;
      height: 100px;
      background: pink;
      list-style: none;
      display: flex;
      justify-content: center;
      align-items: center;
      border: 3px solid white;
      box-sizing: border-box;
    }

    .ck {
      width: 33.33%;
      height: 100px;
      background: pink;
      list-style: none;
      display: flex;
      justify-content: center;
      align-items: center;
      border: 3px solid red !important;
      box-sizing: border-box;
    }
  </style>
</head>

<body>
  <div class="wrap">
    <li class="item">1</li>
    <li class="item">2</li>
    <li class="item">3</li>
    <li class="item">4</li>
    <li class="item-5">抽奖</li>
    <li class="item">6</li>
    <li class="item">7</li>
    <li class="item">8</li>
    <li class="item">9</li>
  </div>

  <script>
    $(function () {
      //点击开始抽奖
      var item = document.querySelectorAll(".item");
      var isStart = true;
      var time = null;
      var speed = 100;
      var index = 0; //随机奖项的索引
      var save = null;
      var count = 0;
      var num = 0;
      var arr = item.length;
      $('.item-5').on('click', function () {
        if (isStart) {
          isStart = false;
          index = Math.floor(Math.random() * arr);
          console.log('index: ', index);
          Rotate();
        }
      })

      function Rotate() {
        if (num >= item.length) {
          num = 0;
          count++;
        }
        if (save) {
          save.classList.remove("ck");
        }
        item[num].classList.add("ck");
        save = item[num];
        //对speed速度进行设置
        if (count < 6) {
          speed -= 2; //提速
        } else {
          //大于等于5圈,开始减速
          speed += 30;
        }
        //限制speed最小值
        if (speed <= 10) {
          speed = 10;
        }
        if (count >= 8 && num == index) {
          clearTimeout(time);
          return;
        }
        num++;
        time = setTimeout(Rotate, speed);
      }

    })
  </script>

</body>

</html>
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值