点名_小程序

页面点名小程序

方式一

<!DOCTYPE html>

<head>
    <meta charset="UTF-8">
    <title>点名器</title>
    <style>
        body{
            background-color: gray;
        }
        .box{
            width: 1000px;
            height: 280px;
            margin: 0 auto;
            margin-top: 100px;
            clear: both;
        }
        #btn,#btn2,#btn3,#btnStop{
            width: 150px;
            height: 50px;
            
            margin-top: 50px;
            font-size: 18px;
        }
        .name{
            width: 100px;
            height: 30px;
            float: left;
            background-color: antiquewhite;
            margin-left: 10px;
            margin-top: 10px;
            text-align: center;
            line-height: 30px;
        }
        #span{
            float: right;
            position: relative;
            top: 55px;
            right: 185px;
        }
        h1{
            text-align: center;
        }
		.high{
			background-color: #FFDEAD;
			font-weight:500;
		}
    </style>

</head>
<body>
<h1>随机点名系统</h1>
<span id="span"></span>
<!-- 存放生成的名单div -->
<div class="box" id="box"></div>
<!-- 存放开始和停止按钮 -->
<div style="text-align:center">
	<input type="button" id="btn" value="点名"/>
	<input type="button" id="btnStop" value="停止"/>
</div>
</body>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
    var arrs = [
        "张恒",
        "李伟",
        "文子昂",
        "李彦松",
        "廖彬",
        "金鑫",
        "夏华伶",
        "邓洪",
        "陈紫桥",
        "罗继财",
        "陈治豪",
        "李坤耀",
        "母天鑫",
        "冯思皓",
        "谷康杰",
        "李辉",
        "李先进",
        "米俊杰",
        "彭小平",
        "唐旭",
        "万云松",
        "向星宇",
        "张全鑫",
        "邬建科",
        "徐江涛",
        "李连辉",
        "肖云龙",
        "徐浪",
        "马俊杰",
        "欧阳平",
        "周雨凡"
    ];
    // 生成数组中的名单div并添加到box中
    window.onload = function() {
        // for(let i = 0; i < arrs_full.length; i++) {
        //     //
        //     //2. 设置div内容
        //     //3. 添加到box中
        // }

        // 获取box父元素
        boxNode = document.getElementById("box");
        boxNode.innerHTML = "";
        for (var i = 0; i < arrs.length; i++) {
            //1.创建div元素  $("
")
var divNode = document.createElement("div"); //var divNode = $("
");
// 2. 设置div内容, 名字 divNode.innerHTML=arrs[i]; // 设置div样式 divNode.className="name"; // 3.添加到box中 boxNode.appendChild(divNode); } } let time = null; //开始点名逻辑 // 点击开始按钮的时候开始定时器 $("#btn").click(function() { clearInterval(time); //开始定时器 time = setInterval(function(){ //在所有的名单div中随机选择一个div设置背景颜色为红色 let index = Math.floor(Math.random() * arrs.length);//0 - 9.99 //清楚之前的颜色 $("#box div").css("background-color", ""); // 找到生成的名单div中的index位的div let mySelector = "#box div:eq(" + index +")"; $(mySelector).css("background-color", "green"); }, 100) }) $("#btnStop").click(function() { clearInterval(time); }) </script>

方式二

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>点名小程序</title>
    <style>
        .cor {
			background-color: #6083cd;
		}
		#box {
			width: 800px;
			border: 2px solid black;
			margin: 0 auto;
			height: 800px;
		}
		ul {
			list-style: none;
		}
		li {
			width: 50px;
			height: 50px;
			margin: 20px;
			float: left;
			line-height: 50px;
			text-align: center;
		}
		.center {
			width: 156px;
			height: 40px;
			line-height: 40px;
			margin: 0 auto;
		}
		#pp {
			display: block;
			text-align: center;
			margin-top: 40px;
			font-size: 35px;
 
		}
    </style>
</head>
<body>
   <div id="box">
        <h1 style="text-align: center;">点名系统</h1>
        <div class="center">
            <input type="button" id="start" value="开始">
            <input type="button" id="end" value="停止">
        </div>
        <span id="pp"></span>
        <div id="dv"></div>
   </div>  
</body>
<script src="js/jquery-3.3.1.min.js"></script>
<script>
    function my(id) {
        return document.getElementById(id);
    }
    //定义数组
    let str = "文鑫,向贞好,胥员员,李勇,熊明,杜凯,熊国良,付帅,龚文曦,吴世林,李鹏,宋小明,黄海,曾光鹏,墙世川,幸勇,彭清亮,杨崇鑫,王一舟,汪家鹏,高茂鑫,张松恒,杨海峰,付杨恒,唐浩翔,张攀"
    let arr = str.split(",");
    // for (let index = 0; index < arr.length; index++) {
        // document.write(index+":"+arr[index]);
        // 
    // }
    let oul = document.createElement("ul");
    my("dv").appendChild(oul);
    for (let i = 0; i < arr.length; i++) {
       let oli = document.createElement("li");
        oul.appendChild(oli);
        oli.innerHTML = arr[i];       
    }

     olis = oul.getElementsByTagName("li");
    let timer;
    my("start").onclick = function(){
        clearInterval(timer);

        timer = setInterval(function(){
            let num = parseInt(Math.random()*arr.length);
            console.log(num);
            for (let j = 0; j < olis.length; j++) {
               olis[j].className = "";               
            }
            olis[num].className = "cor";
            my("pp").innerHTML = arr[num];

        },100)
    }

     my("end").onclick = function(){
         clearInterval(timer);

     }
</script>
</html>

你可能感兴趣的:(点名_小程序)