一、结合抽奖案例完成随机点名程序
要求:
1.点击点名按钮,名字界面随机显示,按钮文字由点名变为停止
2.再次点击点名按钮,显示当前被点名学生姓名,按钮文字由停止变为点名
3.样式请参考css及html自由发挥完成。
结果展示:


代码展示:
html部分:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>随机点名程序</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>随机点名程序</h1>
<div id="nameDisplay">点击点名按钮开始</div>
<button id="randomButton">点名</button>
</div>
<script src="script.js"></script>
</body>
</html>
css部分:
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
text-align: center;
}
h1 {
color: #333;
}
#nameDisplay {
font-size: 24px;
margin-bottom: 20px;
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
js部分:
const students = ['张三', '李四', '王五', '赵六', '钱七', '孙八', '周九', '吴十'];
let intervalId;
let isRandomizing = false;
const randomizeName = () => {
const randomNumber = Math.floor(Math.random() * students.length);
document.getElementById('nameDisplay').textContent = students[randomNumber];
};
const toggleRandomizing = () => {
if (isRandomizing) {
clearInterval(intervalId);
document.getElementById('randomButton').textContent = '点名';
isRandomizing = false;
} else {
intervalId = setInterval(randomizeName, 100);
document.getElementById('randomButton').textContent = '停止';
isRandomizing = true;
}
};
document.getElementById('randomButton').addEventListener('click', toggleRandomizing);
二、使用js及html、css完成秒表计时器
要求如下:
1.界面为一个显示计时面板和三个按钮分别为:开始,暂停,重置
2.点击开始,面板开始计时,
3.点击暂停,面板停止
4.点击重置,计时面板重新为0
结果展示:



代码展示:
html部分:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>秒表计时器</title>
<link rel="stylesheet" href="styles1.css">
</head>
<body>
<div class="container">
<h1>秒表计时器</h1>
<div id="timer">00:00:00</div>
<div class="buttons">
<button id="startButton">开始</button>
<button id="pauseButton">暂停</button>
<button id="resetButton">重置</button>
</div>
</div>
<script src="script1.js"></script>
</body>
</html>
css部分:
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
text-align: center;
}
h1 {
color: #333;
}
#timer {
font-size: 48px;
margin-bottom: 20px;
}
.buttons {
margin-top: 20px;
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
margin: 0 10px;
}
js部分
let timerInterval;
let timerRunning = false;
let hours = 0;
let minutes = 0;
let seconds = 0;
const formatTime = (value) => {
return value < 10 ? `0${value}` : `${value}`;
};
const displayTime = () => {
const formattedHours = formatTime(hours);
const formattedMinutes = formatTime(minutes);
const formattedSeconds = formatTime(seconds);
document.getElementById('timer').textContent = `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
};
const startTimer = () => {
if (!timerRunning) {
timerInterval = setInterval(() => {
seconds++;
if (seconds === 60) {
seconds = 0;
minutes++;
}
if (minutes === 60) {
minutes = 0;
hours++;
}
displayTime();
}, 1000);
timerRunning = true;
}
};
const pauseTimer = () => {
clearInterval(timerInterval);
timerRunning = false;
};
const resetTimer = () => {
clearInterval(timerInterval);
timerRunning = false;
hours = 0;
minutes = 0;
seconds = 0;
displayTime();
};
document.getElementById('startButton').addEventListener('click', startTimer);
document.getElementById('pauseButton').addEventListener('click', pauseTimer);
document.getElementById('resetButton').addEventListener('click', resetTimer);
// Initialize display
displayTime();
2331




被折叠的 条评论
为什么被折叠?



