JS九宫格抽奖

简介: JS九宫格抽奖

制作九宫格抽奖是一个很有意思的过程,废话不多说,直接上代码,看看你能抽到什么。

html部分

<body>
    <div id="all">
      <div class="one">M4A1-雷神</div>
      <div class="two" style="font-size: 12px;">SCAR Light-白虎</div>
      <div class="three">M200-幻神</div>
      <div class="four" style="font-size: 14px;">大礼包(一折)</div>
      <div class="five">98K-星神</div>
      <div class="six">谢谢惠顾</div>
      <div class="seven">王者-葵</div>
      <div class="eight">再来一次</div>
      <button id="button">开始抽奖</button>
    </div>
</body>

css部分

<style>
      #all {
        width: 360px;
        height: 360px;
        margin: auto;
        position: relative;
        text-align: center;
        line-height: 100px;
      }
 
      .one {
        background-color: aquamarine;
        width: 100px;
        height: 100px;
        position: absolute;
        left: 10px;
        top: 10px;
      }
 
      .two {
        background-color: aquamarine;
        width: 100px;
        height: 100px;
        position: absolute;
        left: 120px;
        top: 10px;
      }
 
      .three {
        background-color: aquamarine;
        width: 100px;
        height: 100px;
        position: absolute;
        left: 230px;
        top: 10px;
      }
 
      .four {
        background-color: aquamarine;
        width: 100px;
        height: 100px;
        position: absolute;
        left: 230px;
        top: 120px;
      }
 
      .five {
        background-color: aquamarine;
        width: 100px;
        height: 100px;
        position: absolute;
        left: 230px;
        top: 230px;
      }
 
      .six {
        background-color: aquamarine;
        width: 100px;
        height: 100px;
        position: absolute;
        left: 120px;
        top: 230px;
      }
 
      .seven {
        background-color: aquamarine;
        width: 100px;
        height: 100px;
        position: absolute;
        left: 10px;
        top: 230px;
      }
 
      .eight {
        background-color: aquamarine;
        width: 100px;
        height: 100px;
        position: absolute;
        left: 10px;
        top: 120px;
      }
 
      #button {
        display: block;
        background-color: cadetblue;
        width: 100px;
        height: 100px;
        position: absolute;
        left: 120px;
        top: 120px;
        cursor: pointer;
      }
    </style>

js部分

<script>
    let button = document.getElementById('button');//获取按钮
    let all = document.getElementById('all'); //获取所有盒子
    let prize = all.getElementsByTagName('div');//获取奖品
    console.log(prize);
    let i = 0;//声明一个变量 对应奖品
    let speed = 0;//声明一个变量对应圈数
    let time = 300;//声明时间变量
    let inter;// 声明一个变量,把定时器赋给这个变量
    let num;//声明一个变量,获得一个随机数组
    button.onclick = asb; //给转动函数赋一个名字,为了不能重复点击
    //点击
    function asb() {
      prize[i].style.background = 'blue';
      inter = setInterval(rotates, time);
      num = Math.floor(Math.random() * prize.length);
      console.log(num);
      button.onclick = null;
    }
    //转动颜色变化
    function rotates() {
      if (i < prize.length - 1) {
        i++;
        prize[i].style.backgroundColor = 'blue';
        prize[i - 1].style.backgroundColor = 'aquamarine';
      } else {
        i = 0;
        prize[i].style.backgroundColor = 'blue';
        prize[prize.length - 1].style.backgroundColor = 'aquamarine';
        speed++;
      }
      //速度变化设置
      if (speed < 2) {
        time -= 20;
        if (time < 100) {
          time = 100;
        }
      } else if (speed > 4) {
        time += 50;
        if (time > 500) {
          time = 500;
        }
      }
      //判定停止
      if (i == num && speed > 7) {
        clearInterval(inter);
        //设置一次性定时器,用来设置弹窗和恢复数据
        setTimeout(function() {
          alert("恭喜您获得" + prize[i].innerHTML);
          //重置数据
          prize[i].style.backgroundColor = 'aquamarine';
          i = 0;
          time = 100;
          speed = 0;
          num = 0;
          button.onclick = asb;
        }, 400)
      } else {
        //重置定时器
        clearInterval(inter);
        inter = setInterval(rotates, time);
      }
    }
  </script>

ヾ( ̄▽ ̄)Bye~Bye~


相关文章
|
1月前
|
移动开发 JavaScript 前端开发
分享46个JS抽奖转盘,总有一款适合您
分享46个JS抽奖转盘,总有一款适合您
83 1
|
1月前
|
JavaScript 前端开发
js制作九宫格抽奖功能
js制作九宫格抽奖功能
31 0
|
1月前
|
存储 JavaScript 前端开发
JS实现简单的九宫格抽奖攻略
JS实现简单的九宫格抽奖攻略
42 0
|
1月前
|
JavaScript 前端开发
|
1月前
|
JavaScript 计算机视觉
原生js通过年龄判断是否可以抽奖
原生js通过年龄判断是否可以抽奖
19 0
|
1月前
|
移动开发 JavaScript 前端开发
分享21个JS抽奖转盘特效,36个JS表单验证,31个JS进度条,总有一款适合您
分享21个JS抽奖转盘特效,36个JS表单验证,31个JS进度条,总有一款适合您
37 0
|
1月前
|
小程序
html+css+js实现带有转盘的抽奖小程序
html+css+js实现带有转盘的抽奖小程序
67 0
|
1月前
|
前端开发 JavaScript 容器
html+css+js写抽奖程序
html+css+js写抽奖程序
74 0
|
15天前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp小程序的校园竞赛管理系统附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp小程序的校园竞赛管理系统附带文章源码部署视频讲解等
165 63
|
15天前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp小程序的校园健康驿站管理系统附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp小程序的校园健康驿站管理系统附带文章源码部署视频讲解等
36 5