-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotatingBoard.html
309 lines (280 loc) · 8.92 KB
/
rotatingBoard.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<!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>
.wrap {
position: relative;
width: 400px;
height: 400px;
/* margin: 20% auto; */
}
.panel {
position: absolute;
width: 100%;
height: 100%;
background-color: cadetblue;
border-radius: 50%;
}
.panel-body {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.panel-body>div {
position: absolute;
top: -155px;
left: -29px;
width: 60px;
transform-origin: bottom;
background: antiquewhite;
height: 155px;
text-align: center;
}
.panel-body>div:nth-child(1) {
transform: rotate(30deg);
}
.panel-body>div:nth-child(2) {
transform: rotate(90deg);
}
.panel-body>div:nth-child(3) {
transform: rotate(150deg);
}
.panel-body>div:nth-child(4) {
transform: rotate(210deg);
}
.panel-body>div:nth-child(5) {
transform: rotate(270deg);
}
.panel-body>div:nth-child(6) {
transform: rotate(330deg);
}
.transition {
transition: all 1s;
}
.pointer {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 80px;
height: 80px;
border-radius: 50%;
background-color: beige;
text-align: center;
line-height: 80px;
cursor: pointer;
user-select: none;
}
.pointer::before {
content: "";
display: block;
position: absolute;
background-color: red;
width: 2px;
height: 50px;
top: -50px;
left: 50%;
transform: translateX(-50%);
}
</style>
<link rel="stylesheet" href="./css/test.css">
</head>
<body>
<div>当前主题:<span id="theme-name">./css/test.css</span></div>
<button id="switchBtn">更换主题</button>
<button id="deleteBtn">删除主题</button>
<div>
<ol>
<li>1元大礼</li>
<li>5元大礼</li>
<li>未中奖</li>
<li>7元大礼</li>
<li>未中奖</li>
<li>9999元大礼</li>
</ol>
<div>结果:<span id="result"></span></div>
</div>
<div class="wrap">
<div class="panel">
<div class="panel-body">
<div class="item-1">1</div>
<div class="item-2">2</div>
<div class="item-3">3</div>
<div class="item-4">4</div>
<div class="item-5">5</div>
<div class="item-6">6</div>
</div>
</div>
<div class="pointer">抽奖</div>
</div>
<script>
const awardList = {
'6': {
name: '9999元现金券',
level: 1,
probability: 0.001
},
'5': {
name: '很遗憾,未中奖!',
level: 5,
probability: 0.3
},
'4': {
name: '恭喜获得7元现金券!',
level: 2,
probability: 0.05
},
'3': {
name: '很遗憾,未中奖!',
level: 5,
probability: 0.3
},
'2': {
name: '恭喜获得5元优惠券!',
level: 3,
probability: 0.149
},
'1': {
name: '恭喜获得1元现金!',
level: 4,
probability: 0.2
}
}
// const awardList = {
// '6': {
// name: '9999元现金券',
// level: 1,
// probability: 0.5
// },
// '5': {
// name: '很遗憾,未中奖!',
// level: 5,
// probability: 0.1
// },
// '4': {
// name: '恭喜获得7元现金券!',
// level: 2,
// probability: 0.1
// },
// '3': {
// name: '很遗憾,未中奖!',
// level: 5,
// probability: 0.1
// },
// '2': {
// name: '恭喜获得5元优惠券!',
// level: 3,
// probability: 0.1
// },
// '1': {
// name: '恭喜获得1元现金!',
// level: 4,
// probability: 0.1
// }
// }
let deg = 0
let cat = 60
let isRotating = false
const juger = getRangeJudger()
const themeName = localStorage.getItem('theme-name')
const pointer = document.querySelector('.pointer')
const panel = document.querySelector('.panel')
const result = document.querySelector('#result')
pointer.addEventListener('click', function (e) {
if (!isRotating) {
isRotating = true
panel.classList.remove('transition')
panel.style.transform = `rotate(0deg)`
setTimeout(function () {
panel.classList.add('transition')
pointer.innerHTML = '旋转中'
deg = juger(Math.floor(Math.random() * 1000))
panel.style.transform = `rotate(${deg + (360 * 10)}deg)`
setTimeout(() => {
isRotating = false
pointer.innerHTML = '抽奖'
result.innerText = getAward(deg)
}, 1000)
}, 50)
}
})
function getAward(deg) {
deg = Math.abs(deg)
let msg = ''
if (deg <= 60) {
msg = '恭喜获得1元现金!'
} else if (deg <= 120) {
msg = '恭喜获得5元优惠券!'
} else if (deg <= 180) {
msg = '很遗憾,未中奖!'
} else if (deg <= 240) {
msg = '恭喜获得7元现金券!'
} else if (deg <= 300) {
msg = '很遗憾,未中奖!'
} else if (deg <= 360) {
msg = '恭喜获得9999元现金券!'
}
return msg
}
const switchBtn = document.querySelector('#switchBtn')
switchBtn.addEventListener('click', function () {
const link = document.getElementById('theme')
if (!link) loadStyleLink('./css/test2.css')
})
const deleteBtn = document.querySelector('#deleteBtn')
deleteBtn.addEventListener('click', function () {
removeLink('theme')
})
if (themeName) loadStyleLink(themeName)
function removeLink(id) {
const link = document.getElementById(id)
if (link) {
document.body.removeChild(link)
localStorage.removeItem('theme-name')
let label = document.querySelector('#theme-name')
label.innerText = './css/test.css'
}
}
function loadStyleLink(herf) {
const link = document.createElement('link')
link.id = 'theme'
link.rel = 'stylesheet'
link.href = herf
document.body.append(link)
localStorage.setItem('theme-name', herf)
let label = document.querySelector('#theme-name')
label.innerText = herf
}
function getRangeJudger() {
let max = 1000
let count = 0
let rangeMap = {}
let keys = Object.keys(awardList)
for (let key of keys.reverse()) {
const a = awardList[key]
count = max * a.probability + count
rangeMap[count] = key
}
return function juger(range) {
let index = -1
for (let k in rangeMap) {
const value = Number(k)
if (range <= value) {
index = Number(rangeMap[k])
break
}
}
const deg = index * 60
const result = Math.floor(Math.random() * (60) + (deg - 60))
console.log(index, awardList[index].name)
return -result
}
}
</script>
</body>
</html>