-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLuckDraw.html
87 lines (85 loc) · 3.52 KB
/
LuckDraw.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<html>
<head>
<title>抽奖</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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>
<div class="container">
<div class="block1 block">蓝色传送门</div>
<div class="block2 block">橙色传送门</div>
<div class="block3 block">金属骨头</div>
<div class="block4 block">谢谢参与</div>
<div class="block5 block">开始抽奖</div>
<div class="block6 block">地狱看门犬</div>
<div class="block7 block">秋叶原之选</div>
<div class="block8 block">巨龙传说</div>
<div class="block9 block">霓虹</div>
</div>
<script type="text/javascript">
let blocks = document.getElementsByClassName("block");
// 数组存放的是盒子的下标,按照顺时针的顺序
let arr = [0, 1, 2, 5, 8, 7, 6, 3], i = 0, count = 0, stopTimer;
let rand = Math.floor(Math.random() * 8 + 50); // 给出一个停止计时器的随机数 count的值等于这个随机数时停止计时器
let around = function () {
// 将其他盒子都变为白色
for (let j = 0; j < arr.length; j++) {
blocks[arr[j]].style.background = "white";
}
// 这里blocks的取值为 blocks[0] blocks[1] blocks[2] blocks[5] blocks[8] blocks[7] blocks[6] blocks[3]
// 将当前arr[i]值所对应的盒子修改为粉色
blocks[arr[i]].style.background = "pink";
i++;
// 重制i的值
if (i === arr.length) {
i = 0;
}
count++; // count是一个计数器 根据count的值来调整速度
// 下面的4个if根据count的值来关闭计时器和重启计时器 改变计时器的时间间隔
if (count === 5 || count === 45) {
clearInterval(stopTimer);
stopTimer = setInterval(around, 200);
}
if (count === 10 || count === 35) {
clearInterval(stopTimer);
stopTimer = setInterval(around, 100);
}
if (count === 15) {
clearInterval(stopTimer);
stopTimer = setInterval(around, 50);
}
// 当等于上面的随机数时,停止计时器
if (count === rand) {
clearInterval(stopTimer);
}
}
// 给开始按钮绑定点击事件 点击后执行 around
let start = function () {
blocks[4].removeEventListener("click", start); // 当用户点击了开始按钮后 必须要移除该事件 防止用户重复点击
stopTimer = setInterval(around, 300);
}
blocks[4].addEventListener("click", start);
</script>
</body>
</html>