JS - 点名/抽奖

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>点名1</title>
    <style>
        body {
            background-color: aliceblue;
        }

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

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

        #span {
            float: right;
            position: relative;
            top: 55px;
            right: 185px;
        }

        h1 {
            text-align: center;
        }

        .isName {
            width: 110px;
            height: 30px;
            float: left;
            background-color: antiquewhite;
            margin-left: 10px;
            margin-top: 10px;
            text-align: center;
            /* line-height: 30; */
            padding-top: 10px;
        }
    </style>
</head>

<body>
    <h1>随机点名</h1>
    <span id="span1"></span>
    <div class="box" id="box"></div>
    <input type="button" id="btn" value="点名" />
    <span id="span2"></span>

    <script>
        function idName(id) {
            return document.getElementById(id);
        };

        var arr = ["1", "2", "3", "4", "5", "6", "7", "8",
            "9", "10", "11", "12", "13", "14", "15", "16",
            "17", "18", "19", "20", "21", "22", "23", "24", "25"
        ];

        var nums = arr.length;
        for (var i = 0; i < nums; i++) {
            var div = document.createElement("div");
            div.className = "isName";
            div.innerHTML = arr[i];
            idName("box").appendChild(div);
        };

        var index;

        idName("btn").onclick = function () {

            if (this.value == "点名") {

                timer = setInterval(function () {

                    for (var i = 0; i < nums; i++) {

                        idName("box").children[i].style.background = "";

                    };
                    var index1 = parseInt(Math.random() * nums);

                    idName("box").children[index1].style.background = "red";

                    index = index1;

                }, 10);

                this.value = "暂停";

            } else {

                clearInterval(timer);

                this.value = "点名";

            };

        };
    </script>
</body>

</html>

就还比较实用,记下来哈哈~

上一篇:less 基础教学


下一篇:【无标题】