制作幸运大转盘

效果图

 代码如下:

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

        .a {
            width: 615px;
            height: 615px;
            margin: 0 auto;
        }

        li {
            list-style: none;
            width: 200px;
            height: 200px;
            float: left;
            background-color: aquamarine;
            line-height: 200px;
            text-align: center;
            margin-right: 5px;
            margin-bottom: 5px;
        }

        button {
            width: 80px;
            height: 50px;
            margin-left: 6px;
            background-color: red;
            color: white;
        }

        .new {
            width: 200px;
            height: 200px;
            background: rgb(238, 84, 84);
        }
    </style>
</head>

<body>
    <div class="a">
        <ul>
            <li class="new">10元红包</li>
            <li>20元红包</li>
            <li>30元红包</li>
            <li>40元红包</li>
            <li><button id="a">开始抽奖</button> <button id="b">结束抽奖</button></li>
            <li>60元红包</li>
            <li>70元红包</li>
            <li>80元红包</li>
            <li>90元红包</li>
        </ul>
    </div>
</body>

</html>
<script>
    //获取开始 和 停止按钮
    var but1 = document.getElementById('a');
    var but2 = document.getElementById('b');
    //先把参与抽奖的li下标 放入数组中
    var arr = [0, 1, 2, 5, 8, 7, 6, 3]
    var num = 0;//arr初始的下标
    //获取所有的li
    var li_ = document.getElementsByTagName('li');

    var time = null;
    but1.addEventListener('click', function () {
        clearInterval(time);//每次点击清除一下定时器
        time = setInterval(function () {  //添加定时器
            //遍历数组arr
            for (var i = 0; i < arr.length; i++) {
                li_[arr[i]].className = '';
            }

            if (num > 7) {
                num = 0;
            }
            li_[arr[num]].className = 'new';
            num++;
        }, 100)
    })
    but2.addEventListener('click', function () {
        clearInterval(time);//清除定时器
        alert('恭喜你获得 ' + li_[arr[num]].innerHTML);
    })
</script>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值