HTML+CSS+JavaScript:随机点名案例

 一、需求

1、点击开始按钮,姓名随机切换

2、点击结束按钮,姓名停止切换,此时显示的姓名即为被抽中者

3、同一个人不能被重复抽中

 

二、代码素材

以下是缺失JS部分的代码,感兴趣的小伙伴可以先自己试着写一写

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>随机点名</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .box {
            position: relative;
            box-sizing: border-box;
            margin: 50px auto;
            border: 2px solid #ccc;
            padding: 20px;
            width: 500px;
            height: 200px;
            text-align: center;
        }

        .box h3 {
            line-height: 75px;
        }

        .box button {
            position: absolute;
            bottom: 30px;
            width: 60px;
            height: 30px;
        }

        .box .start {
            left: 130px;
        }

        .box .over {
            right: 130px;
        }
    </style>
</head>

<body>
    <div class="box">
        <h2>随机点名</h2>
        <h3>姓名:马嘉祺</h3>
        <button class="start">开始</button>
        <button class="over">结束</button>
    </div>
    <script>
        const arr = ['马嘉祺', '丁程鑫', '宋亚轩', '严浩翔', '刘耀文', '贺峻霖', '张真源']

    </script>
</body>

</html>

三、算法思路

算法思路非常简单,就是分别为开始按钮和结束按钮绑定点击事件,然后触发开始按钮就开启定时器,触发结束按钮就终止定时器,就这么简单……

四、完整代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>随机点名</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .box {
            position: relative;
            box-sizing: border-box;
            margin: 50px auto;
            border: 2px solid #ccc;
            padding: 20px;
            width: 500px;
            height: 200px;
            text-align: center;
        }

        .box h3 {
            line-height: 75px;
        }

        .box button {
            position: absolute;
            bottom: 30px;
            width: 60px;
            height: 30px;
        }

        .box .start {
            left: 130px;
        }

        .box .over {
            right: 130px;
        }
    </style>
</head>

<body>
    <div class="box">
        <h2>随机点名</h2>
        <h3>姓名:马嘉祺</h3>
        <button class="start">开始</button>
        <button class="over">结束</button>
    </div>
    <script>
        const arr = ['马嘉祺', '丁程鑫', '宋亚轩', '严浩翔', '刘耀文', '贺峻霖', '张真源']

        const h3 = document.querySelector('.box h3')
        const start = document.querySelector('.start')
        const over = document.querySelector('.over')

        let timer

        let rand

        start.addEventListener('click', () => {
            start.disabled = 1
            over.disabled = 0
            timer = setInterval(() => {
                rand = Math.floor(Math.random() * arr.length)
                h3.innerHTML = `姓名:${arr[rand]}`
            }, 100);
        })

        over.addEventListener('click', () => {
            start.disabled = 0
            over.disabled = 1
            clearInterval(timer)
            //数组长度为 1 时,禁用按钮
            if (arr.length === 1)
                start.disabled = over.disabled = 1
            else
                arr.splice(rand, 1)
        })

    </script>
</body>

</html>

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值