JS+HTML+CSS 实现课堂随机点名系统

课堂随机点名系统

最近在学习前端的一些基础,就使用js+html实现了一个比较简单的课堂随机点名系统。效果图如下


在这里插入图片描述

源码主要分为三个部分:html、css、js

1、包含HTML文件的代码,如下
<!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>
    <link rel="stylesheet" href="./css/randomEnhance.css">
</head>

<body>
    <div class="contain">
        <ul id="parent">
            <li id="para" class="mc">赵云</li>
        </ul>
        <div>
            <button id="btn1">开始点名</button>
            <button id="btn2">停止</button>
            <button id="btn3">重置</button>
        </div>
    </div>
    <script src="./js/randomEnhance.js"></script>
</body>

</html>
2、包含css代码。如下
* {
    margin: 0;
    padding: 0;
}

ul,
ol,
dl,
li {
    list-style: none;
}

body {
    background-image: linear-gradient(to right, skyblue, rgb(243, 163, 120));
}

.contain {
    margin: 100px auto;
    width: 800px;
    height: 400px;
    position: relative;
    text-align: center;
}

.contain>ul {
    float: left;
}

li {
    border: 1px solid black;
    border-radius: 5px;
    float: left;
    margin: 5px;
    padding: 5px 10px;
    background: #918597;
    text-align: center;
    color: white;
    width: 48px;
    box-shadow: 0px -2px 2px 0px #ccc,
        -2px 0px 2px 0px #ccc,
        2px 0px 2px 0px #ccc,
        0px 2px 2px 0px #ccc;
}

.contain>div {
    position: absolute;
    width: 380px;
    height: 60px;
    left: 0;
    right: 0;
    bottom: 0px;
    margin: auto;
}

.contain>div>button {
    width: 100px;
    height: 40px;
    margin-left: 20px;
    font-size: 20px;
    border-radius: 10px;
    background: rgb(222, 153, 97);
}
3、包含js代码。如下
let flag = false;
let btn1 = document.getElementById('btn1');
let btn2 = document.getElementById('btn2');
let btn3 = document.getElementById('btn3');

window.addEventListener('load', function () {
    let arr = [
        '张三',
        '李四',
        '赵六',
        '凤翔忠',
        '张三',
        '李四',
        '赵六',
        '凤翔忠',
        '张三',
        '李四',
        '赵六',
        '凤翔忠',
        '张三',
        '李四',
        '赵六',
        '凤翔忠',
        '张三',
        '李四',
        '赵六',
        '凤翔忠',
        '张三',
        '李四',
        '赵六',
        '凤翔忠',
        '张三',
        '李四',
        '赵六',
        '凤翔忠',
        '张三',
        '李四',
        '赵六',
        '凤翔忠',
        '张三',
        '李四',
        '赵六',
        '凤翔忠',
        '张三',
        '李四',
        '赵六',
        '凤翔忠',
        '张三',
        '李四',
        '赵六',
        '凤翔忠',
        '张三',
        '李四',
        '赵六',
        '凤翔忠',
        '凤翔忠',
        '张三',
        '李四',
        '赵六',
        '赵六',
        '凤翔忠',
        '张三',
        '李四',
        '赵六',
        '凤翔忠',
        '张三',
        '李四',
        '赵六',
        '凤翔忠',
        '张三',
        '李四',
        '赵六',
        '凤翔忠',
        '张三',
        '李四',
        '赵六'
    ];
    let content = document.getElementById('parent');
    let child = document.getElementById('para');

    for (let i = 0; i < arr.length; i++) {
        var span = document.createElement("li");
        span.id = "spanid" + i;
        span.style.color = "white";
        span.innerHTML = arr[i];
        child.parentNode.insertBefore(span, child);
    }
})

function fn(a, b) {
    return Math.floor(Math.random() * (Math.abs(a - b) + 1)) + Math.min(a, b)
}

function myTimer() {
    let myNodelist = document.querySelectorAll("li");
    let rd = fn(0, myNodelist.length);
    console.log(rd);
    myNodelist[rd].style.color = "red";
    myNodelist[rd].style.background = "#faa755";
    for (let i = 0; i < myNodelist.length; i++) {
        if (i === rd) continue;
        myNodelist[i].style.color = "white";
        myNodelist[i].style.background = '#918597';
    }
}

function clearMyTimer() {
    let myNodelist = document.querySelectorAll("li");
    let rd = fn(0, myNodelist.length);
    for (let i = 0; i < myNodelist.length; i++) {
        if (i === rd) continue;
        myNodelist[i].style.color = "white";
        myNodelist[i].style.background = '#918597';
    }
}

var myVar;
btn1.onclick = function myStopFunction1() {
    clearInterval(myVar);
    myVar = setInterval(function () { myTimer() }, 100);
}

btn2.onclick = function myStopFunction2() {
    clearInterval(myVar);
}

btn3.onclick = function myStopFunction3() {
    clearInterval(myVar);
    clearMyTimer();
}

以上是三个文件的源码内容。注意复制下来后要更改相应的文件内容才能正确运行。

如果看不懂源码的,也可以联系作者。希望能启示一起学习的同学。
源码链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

博扬java张

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

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

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

打赏作者

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

抵扣说明:

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

余额充值