需求分析
在上方块中展示全员名单
随机点名之后出现滚动条,出现人员性名
点击结束之后出现选中人员
原理实现
布局上方人员名单以及下方按钮
点击点名之后人员名单的display消失,滚动display显现,利用数组存贮人员名单,产生随机数,将数组【随机数】打印至滚动条中
结束时,计时器结束,保留人员名字
效果展示
随机点名
<!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>
*{
margin: 0;
padding: 0;
list-style: none;
}
.mingzi{
height: 500px;
width: 100%;
display: flex;
flex-wrap: wrap;
background-color: antiquewhite;
}
ul{
display: flex;
flex-wrap: wrap;
}
.mingzi li{
height: 100px;
width: 200px;
margin-left: 50px;
margin-top: 50px;
text-align: center;
line-height: 100px;
background-color: aqua;
display: block;
}
.bottom{
width: 100%;
height: 100px;
display: flex;
font-size: 30px;
font-weight: 500;
line-height: 100px;
text-align: center;
background-color: blanchedalmond;
}
.start{
width: 200px;
height: 100px;
margin-left: 400px;
background-color: gray;
}
.end{
height: 100px;
width: 200px;
background-color: gray;
margin-left: 300px;
}
.checked{
position: absolute;
left: 650px;
top: 200px;
height: 100px;
width: 200px;
text-align: center;
color: white;
background-color: brown;
line-height: 100px;
display: none;
}
</style>
</head>
<body>
<div class="mingzi">
<ul>
<li>02010101</li>
<li>02010102</li>
<li>02010103</li>
<li>02010104</li>
<li>02010105</li>
<li>02010106</li>
<li>02010107</li>
<li>02010108</li>
<li>02010109</li>
<li>02010110</li>
<li>02010111</li>
<li>02010112</li>
</ul>
</div>
<div class="bottom">
<div class="start">开始</div>
<div class="end">结束</div>
</div>
<div class="checked"></div>
<script>
var start=document.querySelector('.start');
var end=document.querySelector('.end');
var ul=document.querySelector('ul');
var checked=document.querySelector('.checked');
var shuzu=document.getElementsByTagName('li');
var name;
start.onclick=function(){
ul.style.display="none";
checked.style.display="block";
run=setInterval(function(){
var number=Math.floor(Math.random()*12);
name=shuzu[number].textContent
checked.innerHTML=shuzu[number].textContent
},100)
}
end.onclick=function(){
clearInterval(run);
checked.innerHTML="选中的是"+name
}
</script>
</body>
</html>