复制以下代码查看效果
(注:记得导入jquery.js代码)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#box{
width: 500px;
/* height: 400px; */
/* border: 1px solid red; */
margin: 100px auto;
position: relative;
}
#box1{
width: 498px;
height: 75px;
text-align: center;
border: 1px solid red;
}
#ul{
width: 498px;
/* height: 100px; */
/* background: red; */
text-align: center;
border: 1px solid red;
}
#ul li{
list-style: none;
}
.current{
width: 65px;
height: 20px;
line-height: 20px;
border-width: 1px;
border-style: solid;
border-radius: 5px;
text-align: center;
display: inline-block;
margin: 10px;
}
</style>
<script src="./jquery-3.4.1.min.js"></script>
</head>
<body>
<div id="box">
<div id="box1">
<button id="btn1">开始点名</button>
<button id="btn2">停下来</button>
<h3>随机点名</h3>
<p id="p1"></p>
</div>
<ul id="ul"></ul>
</div>
<script type="text/javascript">
$(function(){
var arr=['***','***','小**','**','二狗','***','老帅','貂蝉','妲己','吕布','宫啊','香香','套猴子','你爷爷','奶奶的','爷爷','程咬金','你大爷','还是','你大爷'];
//随机颜色rgb
function color(){
var r=Math.floor(Math.random()*255);
var g=Math.floor(Math.random()*255);
var b=Math.floor(Math.random()*255);
var rgb='rgb('+r+','+g+','+b+')';
return rgb;
}
$.each(arr,function(index,e){
//创建li
var li=$('<li>');
li.html(arr[index]);
li.css({
'background':color(),
})
li.addClass('current');
$('ul').append(li);
})
$('#btn1').click(function(){
timer=setInterval(function(){
random=Math.floor(Math.random()*arr.length);
$.each(arr,function(index,e){
$('#p1').html();
$('li').eq(index).css('background',color());
$('li').eq(random).css('background','hsl(255,75%,80%)');
})
},50)
})
$('#btn2').click(function(){
clearInterval(timer);
$('#p1').html($('ul li').eq(random).html())
})
})
</script>
</body>
</html>