通过简单js来实现抽奖系统,其中最重要的是周期定时器的使用
<!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>
.box {
width: 600px;
height: 600px;
/* background: red; */
margin: 0px auto;
}
ul li {
width: 180px;
height: 200px;
float: left;
list-style: none;
text-align: center;
line-height: 200px;
border: 2px solid #f4c7c7;
margin-right: 5px;
margin-bottom: 5px;
}
ul li:nth-child(3n) {
margin-right: 0px;
}
button {
background: #b3f9a4d6;
border: none;
width: 60px;
height: 60px;
}
.ok {
background: red;
color: #fff;
}
</style>
</head>
<body>
<div class="box">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>
<button id="start">开始</button>
<button id="stop">停止</button>
</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
</ul>
</div>
<script>
// 获取开始和停止按钮
var start = document.querySelector("#start");
var stop = document.querySelector("#stop");
// 获取所有的li标签
var lis = document.querySelectorAll("li");
let num=0;
// console.log(start,stop,lis);
start.onclick = function () {
this.disabled = true;//按钮禁用
//设置周期定时器
time = setInterval(function () {
if (num > 0) {
lis[num - 1].className = ""
} else {
lis[num].className = "";
}
if (num > 8) {
num = 0;
}
if(num==4){
num++;
}
lis[num].className = "ok";
num++;
}, 500)
}
stop.onclick = function () {
start.disabled = false;
clearInterval(time);
}
</script>
</body>
</html>