<!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>Document</title>
<style>
*{text-align: center;}
#user{
width: 500px;
height: 200px;
border: 1px solid #000;
line-height: 200px;
font-size: 100px;
margin: 0 auto;
}
button{
width: 250px;
height: 100px;
font-size: 50px;
margin-top: 20px;
}
</style>
</head>
<body>
<h1>点名</h1>
<div id="user">kelly</div>
<button>开始</button>
<script>
window.onload = function() {
var arr = ['aa','bb','cc','dd','ee','ff','kk']
var timeId
// 获取按钮绑定顶点击事件
document.querySelector('button').onclick = function() {
// 切换文字,获取随机名字
if (this.innerText === '开始') {
this.innerText = '结束'
timeId = setInterval(() => {
// 获取arr中的随机索引
var index = Math.floor(Math.random()*arr.length)
var userName = arr[index]
// 替换显示的名字
document.querySelector('#user').innerText = userName
},50)
}else{
this.innerText = '开始'
clearInterval(timeId)
}
}
}
</script>
</body>
</html>
JS关于点名器的简单实现
最新推荐文章于 2024-01-09 11:47:36 发布