记一个随机点名的单页面服务

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>课堂随机点名</title>
    <style>
        /* 全局样式 */
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            /* 使用渐变背景 */
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }

        /* 容器样式 */
        .container {
            text-align: center;
            background-color: rgba(255, 255, 255, 0.9);
            padding: 30px;
            border-radius: 20px;
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
            width: 400px;
        }

        /* 标题样式 */
        h1 {
            color: #333;
            font-size: 32px;
            margin-bottom: 20px;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
        }

        /* 文件输入框样式 */
        input[type="file"] {
            display: none;
        }

        /* 自定义文件选择按钮样式 */
        .file-label {
            display: inline-block;
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        .file-label:hover {
            background-color: #45a049;
        }

        /* 按钮样式 */
        button {
            padding: 12px 24px;
            margin: 15px 5px;
            font-size: 16px;
            cursor: pointer;
            border: none;
            border-radius: 5px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            transition: background-color 0.3s ease, transform 0.2s ease;
        }

        /* 开始/暂停按钮样式 */
        #startStopBtn {
            background-color: #2196F3;
            color: white;
        }

        #startStopBtn:hover {
            background-color: #1976D2;
            transform: translateY(-2px);
        }

        /* 显示区域样式 */
        #display {
            margin-top: 30px;
            font-size: 28px;
            font-weight: bold;
            color: #333;
            background-color: #fff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            animation: fadeIn 0.5s ease;
        }

        /* 文件名显示样式 */
        #fileNameDisplay {
            margin-top: 10px;
            font-size: 14px;
            color: #666;
        }

        /* 淡入动画效果 */
        @keyframes fadeIn {
            from {
                opacity: 0;
            }

            to {
                opacity: 1;
            }
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>课堂随机点名</h1>
        <!-- 自定义文件选择按钮 -->
        <label for="fileInput" class="file-label">选择CSV文件</label>
        <input type="file" id="fileInput" accept=".csv">
        <!-- 新增文件名显示区域 -->
        <div id="fileNameDisplay"></div>
        <!-- 合并后的开始/暂停按钮 -->
        <button id="startStopBtn" disabled>开始</button>
        <div id="display"></div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const fileInput = document.getElementById('fileInput');
            const startStopBtn = document.getElementById('startStopBtn');
            const display = document.getElementById('display');
            const fileNameDisplay = document.getElementById('fileNameDisplay');
            const fileLabel = document.querySelector('.file-label');

            let students = [];
            let intervalId = null;
            let isRunning = false;

            // 读取CSV文件
            fileInput.addEventListener('change', (event) => {
                const file = event.target.files[0];
                if (file) {
                    const reader = new FileReader();
                    reader.onload = (e) => {
                        const text = e.target.result;
                        // 按行分割文本内容
                        const lines = text.split('\n');
                        // 跳过第一行(表头)
                        students = lines.slice(1).map(line => {
                            const [name, info] = line.split(',');
                            return { name, info };
                        });
                        startStopBtn.disabled = false;
                        // 显示文件名
                        fileNameDisplay.textContent = `已选择文件: ${file.name}`;
                        // 修改按钮文字为重新上传
                        fileLabel.textContent = '重新上传';
                    };
                    reader.readAsText(file);
                }
            });

            // 开始/暂停随机点名
            startStopBtn.addEventListener('click', () => {
                if (students.length === 0) return;
                if (isRunning) {
                    // 暂停点名
                    clearInterval(intervalId);
                    startStopBtn.textContent = '开始';
                    isRunning = false;
                } else {
                    // 开始点名
                    startStopBtn.textContent = '暂停';
                    isRunning = true;
                    intervalId = setInterval(() => {
                        const randomIndex = Math.floor(Math.random() * students.length);
                        const student = students[randomIndex];
                        display.textContent = `${student.name} - ${student.info}`;
                    }, 100);
                }
            });
        });
    </script>
</body>

</html>

数据样例

学号,姓名,性别,班级
2025001,王浩宇,男,1班
2025002,李欣怡,女,2班
2025003,张梓涵,女,3班
2025004,陈子轩,男,1班
2025005,刘雨婷,女,2班
2025006,黄俊杰,男,3班
2025007,周雅静,女,1班
2025008,吴博文,男,2班
2025009,徐诗琪,女,3班
2025010,孙浩然,男,1班
2025011,马雪怡,女,2班
2025012,朱明辉,男,3班
2025013,胡瑾瑜,女,1班
2025014,林宇航,男,2班
posted @ 2025-03-02 23:44  厚礼蝎  阅读(54)  评论(0)    收藏  举报