Update select.html

main
luoyonghuang 2 months ago
parent 1fa4bf0c55
commit 285bcf0f92

@ -1,153 +1,153 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>随机点名</title> <title>随机点名</title>
<style> <style>
body { body {
font-family: 'Arial', sans-serif; font-family: 'Arial', sans-serif;
height: 100vh; height: 100vh;
margin: 0; margin: 0;
display: flex; display: flex;
justify-content: center; justify-content: center;
align-items: center; align-items: center;
background: linear-gradient(135deg, #6e45e2, #88d3ce); background: linear-gradient(135deg, #6e45e2, #88d3ce);
color: white; color: white;
} }
.container { .container {
text-align: center; text-align: center;
background-color: rgba(255, 255, 255, 0.1); background-color: rgba(255, 255, 255, 0.1);
padding: 30px; padding: 30px;
border-radius: 15px; border-radius: 15px;
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.2); box-shadow: 0 8px 30px rgba(0, 0, 0, 0.2);
} }
#nameDisplay { #nameDisplay {
font-size: 2.5em; font-size: 2.5em;
margin-bottom: 20px; margin-bottom: 20px;
padding: 20px; padding: 20px;
border-radius: 10px; border-radius: 10px;
background-color: rgba(255, 255, 255, 0.2); background-color: rgba(255, 255, 255, 0.2);
transition: all 0.3s ease; transition: all 0.3s ease;
} }
#nameDisplay.active { #nameDisplay.active {
transform: scale(1.1); transform: scale(1.1);
color: #ffeb3b; color: #ffeb3b;
} }
button { button {
padding: 12px 30px; padding: 12px 30px;
font-size: 1.1em; font-size: 1.1em;
border: none; border: none;
border-radius: 50px; border-radius: 50px;
background-color: #ff5722; background-color: #ff5722;
color: white; color: white;
cursor: pointer; cursor: pointer;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1); box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
transition: background-color 0.3s ease, box-shadow 0.3s ease; transition: background-color 0.3s ease, box-shadow 0.3s ease;
} }
button:hover { button:hover {
background-color: #ff3d00; background-color: #ff3d00;
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3); box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
} }
button:disabled { button:disabled {
background-color: #999; background-color: #999;
cursor: not-allowed; cursor: not-allowed;
} }
button + button { button + button {
margin-left: 15px; margin-left: 15px;
} }
</style> </style>
</head> </head>
<body> <body>
<div class="container"> <div class="container">
<div id="nameDisplay">准备开始提问</div> <div id="nameDisplay">准备开始提问</div>
<button id="startButton">开始</button> <button id="startButton">开始</button>
<button id="stopButton" disabled>停止</button> <button id="stopButton" disabled>停止</button>
<button class="back-button" onclick="window.location.href='index.html'">返回主页面</button> <button class="back-button" onclick="window.location.href='index.html'">返回主页面</button>
</div> </div>
<script> <script>
let names = []; let names = [];
let intervalId = null; let intervalId = null;
// 获取 DOM 元素 // 获取 DOM 元素
const nameDisplay = document.querySelector('#nameDisplay'); const nameDisplay = document.querySelector('#nameDisplay');
const startButton = document.querySelector('#startButton'); const startButton = document.querySelector('#startButton');
const stopButton = document.querySelector('#stopButton'); const stopButton = document.querySelector('#stopButton');
// 页面加载时获取学生名单 // 页面加载时获取学生名单
window.addEventListener('DOMContentLoaded', fetchStudents); window.addEventListener('DOMContentLoaded', fetchStudents);
// 获取学生名单 // 获取学生名单
async function fetchStudents() { async function fetchStudents() {
try { try {
const response = await fetch('/api/get-students'); const response = await fetch('/api/get-students');
if (!response.ok) { if (!response.ok) {
throw new Error('Network response was not ok'); throw new Error('Network response was not ok');
} }
const data = await response.json(); const data = await response.json();
names = data.map(student => student.student_name); names = data.map(student => student.student_name);
} catch (error) { } catch (error) {
console.error('Error fetching student list:', error); console.error('Error fetching student list:', error);
} }
} }
// 动态更新显示效果 // 动态更新显示效果
const updateNameDisplay = (name) => { const updateNameDisplay = (name) => {
nameDisplay.textContent = `点到: ${name}`; nameDisplay.textContent = `点到: ${name}`;
nameDisplay.classList.add('active'); nameDisplay.classList.add('active');
setTimeout(() => { setTimeout(() => {
nameDisplay.classList.remove('active'); nameDisplay.classList.remove('active');
}, 300); }, 300);
}; };
// 开始滚动显示 // 开始滚动显示
startButton.addEventListener('click', () => { startButton.addEventListener('click', () => {
startButton.disabled = true; startButton.disabled = true;
stopButton.disabled = false; stopButton.disabled = false;
intervalId = setInterval(() => { intervalId = setInterval(() => {
// 从 names 数组中随机选择一个名字进行滚动显示 // 从 names 数组中随机选择一个名字进行滚动显示
const randomIndex = Math.floor(Math.random() * names.length); const randomIndex = Math.floor(Math.random() * names.length);
const selectedName = names[randomIndex]; const selectedName = names[randomIndex];
// 更新显示选中的名字 // 更新显示选中的名字
updateNameDisplay(selectedName); updateNameDisplay(selectedName);
}, 100); // 每100毫秒滚动显示一次 }, 100); // 每100毫秒滚动显示一次
}); });
// 停止滚动并通过后端决定最终选择的学生 // 停止滚动并通过后端决定最终选择的学生
stopButton.addEventListener('click', () => { stopButton.addEventListener('click', async () => {
clearInterval(intervalId); clearInterval(intervalId);
startButton.disabled = false; startButton.disabled = false;
stopButton.disabled = true; stopButton.disabled = true;
// 调用后端接口获取最终选中的学生 try {
fetch('/api/random-call') const response = await fetch('/api/random-call');
.then(response => {
if (!response.ok) { if (!response.ok) {
throw new Error('Network response was not ok'); throw new Error('Network response was not ok');
} }
return response.json();
}) const data = await response.json();
.then(data => { // 从后端返回的数据中获取学生名字
// 从后端返回的数据中获取学生名字 const selectedName = data.student_name; // student_name 属性
if (data.student && data.student.name) {
const selectedName = data.student.name; if (selectedName) {
// 更新显示选中的学生 // 更新显示选中的学生
updateNameDisplay(selectedName); updateNameDisplay(selectedName);
// 跳转到新页面并传递被选中的名字 // 跳转到新页面并传递被选中的名字
window.location.href = `choice.html?name=${encodeURIComponent(selectedName)}`; window.location.href = `choice.html?student_name=${encodeURIComponent(data.student_name)}&student_id=${encodeURIComponent(data.student_id)}`;
} else { } else {
console.error('未返回有效学生数据'); console.error('未返回有效学生数据');
} }
}) } catch (error) {
.catch(error => { console.error('请求失败:', error);
console.error('请求失败:', error); }
}); });
});
</script> </script>
</body> </body>
</html> </html>

Loading…
Cancel
Save