抽奖的功能实现html,css,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>
    <style>
        .container {
            width: 600px;
            height: 600px;
            border: 1px solid;
            display: flex;
            flex-wrap: wrap;
            margin: 100px auto;
        }

        .block {
            width: 33.33%;
            height: 33.33%;
            outline: 1px solid black;
            text-align: center;
            line-height: 200px;
            font-size: 26px;
        }

        .block5 {
            background-color: skyblue;
            cursor: pointer;
        }
    </style>

</head>

<body>
    <button onclick="resetStart()">重置抽奖</button>
    <div class="container">
        <div class="block1 block">iphone13</div>
        <div class="block2 block">谢谢参与</div>
        <div class="block3 block">华为p40</div>
        <div class="block4 block">谢谢参与</div>
        <div class="block5 block" id="start">开始抽奖</div>
        <div class="block6 block">谢谢参与</div>
        <div class="block7 block">美的空调</div>
        <div class="block8 block">谢谢参与</div>
        <div class="block9 block">macbook 2020</div>
    </div>
</body>
<script>
    const btn = document.getElementById('start');
    const container = document.getElementsByClassName('container')[0].children
    let random = Math.floor(Math.random() * 10) + 20 // 随机生成的次数
    const speed = 100; // 抽奖滚动速度
    const list = [0, 1, 2, 5, 8, 7, 6, 3] //顺时针依次滚动的数组坐标
    let timer;
    let isClick = false; // 是否可点击


    btn.onclick = function () {
        if (!isClick) {
            changeColor()
            isClick = true
        }
    }

    // 重置抽奖
    function resetStart() {
        isClick = false;
        timer = null;
        random = Math.floor(Math.random() * 10) + 20;
        resetStyle()
    }

    function resetStyle() {
        for (let i = 0; i < container.length; i++) {
            container[list[i] || 0].style.background = '#fff'
        }
    }


    function changeColor() {
        clearInterval(timer)
        let count = 0;
        let num = list[count];
        timer = setInterval(() => {
            resetStyle()
            container[num].style.background = 'pink';
            count++;
            num = list[count % list.length];
            if (random === count) {
                clearInterval(timer)
            }
        }, speed)
    }
</script>

</html>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值