效果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b19d1fdc006c823c7dd968c55bce4fd8.png#pic_center)
代码实现
<!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>
.container {
width: 600px;
height: 600px;
border: 1px solid;
display: flex;
flex-wrap: wrap;
margin: 100px auto;
}
.block {
width: 33.33%;
height: 33.33%;
outline: 1px solid black;
text-align: center;
line-height: 200px;
font-size: 26px;
}
.block5 {
background-color: skyblue;
cursor: pointer;
}
</style>
</head>
<body>
<button onclick="resetStart()">重置抽奖</button>
<div class="container">
<div class="block1 block">iphone13</div>
<div class="block2 block">谢谢参与</div>
<div class="block3 block">华为p40</div>
<div class="block4 block">谢谢参与</div>
<div class="block5 block" id="start">开始抽奖</div>
<div class="block6 block">谢谢参与</div>
<div class="block7 block">美的空调</div>
<div class="block8 block">谢谢参与</div>
<div class="block9 block">macbook 2020</div>
</div>
</body>
<script>
const btn = document.getElementById('start');
const container = document.getElementsByClassName('container')[0].children
let random = Math.floor(Math.random() * 10) + 20
const speed = 100;
const list = [0, 1, 2, 5, 8, 7, 6, 3]
let timer;
let isClick = false;
btn.onclick = function () {
if (!isClick) {
changeColor()
isClick = true
}
}
function resetStart() {
isClick = false;
timer = null;
random = Math.floor(Math.random() * 10) + 20;
resetStyle()
}
function resetStyle() {
for (let i = 0; i < container.length; i++) {
container[list[i] || 0].style.background = '#fff'
}
}
function changeColor() {
clearInterval(timer)
let count = 0;
let num = list[count];
timer = setInterval(() => {
resetStyle()
container[num].style.background = 'pink';
count++;
num = list[count % list.length];
if (random === count) {
clearInterval(timer)
}
}, speed)
}
</script>
</html>