用js写随机抽奖代码

<!-- 样式  -->
    <style>
        .title {
            width: 300px;
            height: 50px;
            background-color: orange;
            text-align: center;
            margin: 0 auto;
            margin-bottom: 20px;
            line-height: 50px;
        }
        
        #box {
            margin: 0 auto;
            width: 600px;
            display: flex;
            justify-content: space-around;
            flex-wrap: wrap;
            /* border: 1px solid black; */
        }
        
        .box-item {
            width: 23%;
            padding: 30px 0;
            box-sizing: border-box;
            /* border: 1px solid red; */
            background-color: rgb(0, 255, 221);
            display: flex;
            justify-content: center;
            align-items: center;
            margin-bottom: 20px;
        }
        
        .btn {
            margin: 0 auto;
            width: 600px;
            display: flex;
            justify-content: space-around;
        }
        
        .btn-item {
            width: 200px;
            padding: 15px 0;
            text-align: center;
            background-color: orange;
        }
    </style>
 <h3 class="title">随机抽奖</h3>
    <div id="box">
    </div>
    <div class="btn">
        <div class="btn-item" id="start">开始</div>
        <div class="btn-item" id="end">结束</div>
    </div>
<script>
        // 定义一个数组,里面输入抽奖的东西
        var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
        var _box = document.getElementById('box');
        var html = '';
        for (let i = 0; i < arr.length; i++) {
            html += '<div class="box-item" >' + arr[i] + '</div>';
        }
        _box.innerHTML = html;
        var _start = document.getElementById('start');
        var _end = document.getElementById('end');
        _start.onclick = function() {
            _time = setInterval(function() {
                let index = Math.floor(Math.random() * arr.length);
                for (let j = 0; j < _box.children.length; j++) {
                    _box.children[j].style.backgroundColor = "rgb(0, 255, 221)";
                }
                _box.children[index].style.backgroundColor = "red";
            }, 100);
        }
        _end.onclick = function() {
            clearInterval(_time);
        }
    </script>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值