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>谁是幸运儿</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }
    /* 名字和图片总显示区域 */
    #box {
        width: 1000px;
        height: 500px;
        line-height: 500px;
        text-align: center;
        margin: auto;
        color: red;
        font-size: 100px;
        display: flex;
        justify-content: space-between;
    }

    /* 右配图 */
    #box>div:nth-child(1) {
        width: 200px;
        height: 500px;
    }

    /* 名字显示区域 */
    #box>div:nth-child(2) {
        width: 400px;
        height: 500px;
        font-size: 55px;
    }

    /* 狗狗图 */
    #box>div:nth-child(3) {
        width: 200px;
        height: 500px;
    }

    /* 整个按钮区域 */
    .buttom {
        width: 1000px;
        height: 200px;
    }

    /* 按钮设置 */
    form {
        display: flex;
        margin: auto;
        align-items: center;
        justify-content: space-around;
        margin-top: 50px;
    }

    /* 开始按钮 */
    .buttom>form>input:nth-child(1) {
        width: 200px;
        height: 100px;
        margin-top: 20px;
        background-color: blanchedalmond;
        font-size: 50px;
        color: royalblue;
        border-radius: 100px;
    }

    /* 刷新按钮 */
    #button {
        width: 200px;
        height: 100px;
        margin-top: 20px;
        background-color: rgb(205, 225, 255);
        font-size: 50px;
        color: rgb(225, 65, 118);
        border-radius: 100px;
    }

    /* 停止按钮 */
    .buttom>form>input:nth-child(3) {
        width: 200px;
        height: 100px;
        margin-top: 20px;
        font-size: 50px;
        border-radius: 100px;
        background-color: aqua;
        color: yellowgreen;
    }

    /* 整个主体 */
    .xaun {
        width: 1000px;
        margin: auto;
        margin-top: 100px;
    }
</style>

<script src="随机函数.js"></script>
<body>
    <div class="xaun">
        <div id="box">
            <div>
                <img src="./1.jpg" alt="" style="width: 200px;height: 300px;margin-top: 50%;">
            </div>
            <div id="Render">准备好了吗?</div>
            <div>
                <img src="./2.gif" alt="" style="width: 200px;height: 500px;">
            </div>
        </div>
        <div class="buttom">
            <form>
                <input type="button" value='开始' id='startBtn'>
                <input type="submit" value="刷新" id="button">
                <input type="button" value='停止' id='stopBtn'>
            </form>
        </div>
    </div>
    <script>
        function Random() {
            let str = '戴拿奥特曼,盖亚奥特曼,阿古茹奥特曼,纳依斯奥特曼,奈欧斯奥特曼,赛文21奥特曼,高斯奥特曼,杰斯提斯奥特曼,雷杰多奥特曼,奈科斯特奥特曼,奈克瑟斯奥特曼,诺亚奥特曼,麦克斯奥特曼,杰诺奥特曼,梦比优斯奥特曼,希卡利奥特曼,赛文X奥特曼,赛罗奥特曼';
            let arr = str.split(",");   //先把str字符串转换成数组
            let puke = "";         //初始值为空
            let number = "";          //储存名字
            let splite = "";          //储存名字对应的下标
            startBtn.onclick = function () {             //获取点击
                if (arr.length != 0) {                  //判断当名字删除完过后,提示刷新名单
                    if (puke) {               //puke为ture的时候
                        return;           //当puke已经有值的之后,停止执行,决绝重复层叠执行
                    } else {
                        puke = setInterval(function () {                   //获取点击后,开始循环执行
                            splite = [randomNum(0, arr.length - 1)];         //把随机的下标传给数组
                            number = arr[splite];                         //拿着对应的下标找到对应的名字,把名字传入到对应数组
                            Render.innerHTML = number;    //把数组的随机下标对应的名字,放到页面标签中
                        }, 50)
                    };
                } else {
                    Render.innerHTML = "请刷新名单";
                }
            };
            stopBtn.onclick = function () {           //点击停止按钮
                if (puke) {                      //判断puke是否正在执行,如果在执行才能停止,没有执行,就不能点击停止
                    clearInterval(puke);                     //点击过后取消时钟执行
                    puke = null;             //点击过后,清空puke的值 
                    arr.splice(splite, 1);            //删除抽到过的名字
                }
            }
        }
        Random();
    </script>
</body>
</html>

在这里插入图片描述

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旧梦星轨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值