简易随机点名


<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        html {
            height: 100%;
        }

        body {
            padding: 0;
            margin: 0;
            background: url(../img/login.jpg) no-repeat;
            background-size: 100% 100%;
        }


        .box {
            width: 100%;
            height: 240px;
            margin: 0 auto;
            margin-top: 100px;
        }

        #btn {
            width: 100px;
            height: 30px;
            margin-left: 600px;
            margin-top: 50px;
        }

        .name {
            width: 100px;
            height: 30px;
            border-radius: 50px;
            float: left;
            background-color: antiquewhite;
            margin-left: 10px;
            margin-top: 10px;
            text-align: center;
            line-height: 30px;
        }


        h3 {
            text-align: center;
            font: "仿宋";
        }

    </style>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="css/Login.css" />
</head>

<body>
    <h3>简易随机点名(点击回车开始,再次点击停止)</h3>
    <span id="span"></span>
    <div class="box" id="box"></div>
    <script type="text/javascript">
        //创造虚拟后台数据
        var arrs = ["1", "2", "3", "4", "5", "6", "7", "8",
            "9", "10", "11", "12", "13", "14", "15", "16", "17", "18"
        ];
        //获取父元素
        var boxNode = document.getElementById("box");
        for(var i = 0; i < arrs.length; i++) {
            //创建新元素
            var divNode = document.createElement("div");
            divNode.innerHTML = arrs[i];
            divNode.className = "name col-md-1 ";
            boxNode.appendChild(divNode);
        }
        //点名
        var flag = 0;
        document.onkeydown = function(event) {
            var e = event || window.event;
            if(e && e.keyCode == 13) { //回车键的键值为13
                if(flag > 1) {
                    flag = 0;
                }
                if(flag == 0) {

                    //定时
                    timeId = setInterval(function() {
                        //清空所有颜色
                        for(var j = 0; j < arrs.length; j++) {
                            boxNode.children[j].style.background = "";
                        }

                        var random = parseInt(Math.random() * arrs.length);
                        boxNode.children[random].style.background = "#A6E1EC";
                    }, 100);

                }
                if(flag == 1) {
                    clearInterval(timeId)
                }
                flag++
            }
        };
    </script>
</body>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值