固定名单有随机点名动画的html
时间: 2023-11-06 08:06:56 浏览: 37
以下是一个固定名单有随机点名动画的HTML示例。你可以将学生姓名添加到数组中,并使用JavaScript来随机选择一个学生进行点名。
```html
<!DOCTYPE html>
<html>
<head>
<title>随机点名</title>
<style>
#name {
font-size: 3em;
text-align: center;
margin-top: 50px;
}
#button {
display: block;
margin: 0 auto;
margin-top: 20px;
padding: 10px 20px;
font-size: 1.5em;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
</style>
</head>
<body>
<div id="name"></div>
<button id="button" onclick="randomName()">点名</button>
<script>
var students = ["小明", "小红", "小刚", "小芳", "小华"]; // 将学生姓名添加到数组中
function randomName() {
var randomIndex = Math.floor(Math.random() * students.length); // 随机选择一个学生进行点名
document.getElementById("name").innerHTML = students[randomIndex];
}
</script>
</body>
</html>
```
在这个示例中,我们使用了一个`<div>`元素来显示被点名的学生姓名,并使用一个`<button>`元素来触发随机点名的函数。CSS样式用于美化页面。
JavaScript函数`randomName()`使用`Math.random()`方法生成一个0到1之间的随机数,并将其乘以学生数组的长度,然后使用`Math.floor()`方法将结果向下取整,得到一个0到学生数组长度减1之间的整数索引。最后,函数将选定的学生姓名显示在页面上。