抽奖轮盘在小程序,h5中是常见的需求,这个demo将实现一个简单的抽奖轮盘,效果如下

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 340px;
height: 340px;
border: 1px #00ffff solid;
position: relative;
margin: 100px;
}
.box div {
width: 100px;
height: 100px;
background-color: royalblue;
text-align: center;
line-height: 100px;
}
.div2 {
position: absolute;
top: 10px;
left: 10px;
}
.div3 {
position: absolute;
top: 10px;
left: 120px;
}
.div4 {
position: absolute;
top: 10px;
left: 230px;
}
.div5 {
position: absolute;
top: 120px;
left: 230px;
}
.div6 {
position: absolute;
top: 230px;
left: 230px;
}
.div7 {
position: absolute;
top: 230px;
left: 120px;
}
.div8 {
position: absolute;
top: 230px;
left: 10px;
}
.div9 {
position: absolute;
top: 120px;
left: 10px;
}
.button {
width: 100px;
height: 100px;
background-color: #62c6a3;
position: absolute;
text-align: center;
line-height: 100px;
top: 120px;
left: 120px;
}
</style>
</head>
<body>
<div class="box">
<div class="div2">短裙</div>
<div class="div3">口红</div>
<div class="div4">草莓</div>
<div class="div5">西瓜</div>
<div class="div6">奶茶</div>
<div class="div7">美甲</div>
<div class="div8">谢谢参与</div>
<div class="div9">苹果13</div>
<strong class="button">点击抽奖</strong>
</div>
<script>
let button = document.getElementsByClassName('button')[0]
let box = document.getElementsByClassName('box')[0]
let pirze = box.getElementsByTagName('div')
console.log(pirze)
let k = 0
let time = 500
let count = 0
let int
let rom = 0
button.onclick = eve
function eve() {
pirze[k].style.background = 'pink'
int = setInterval(pu, time)
rom = Math.floor(Math.random() * pirze.length)
button.onclick = null
}
function pu() {
if (k < pirze.length - 1) {
k++
pirze[k - 1].style.background = 'royalblue'
pirze[k].style.background = 'pink'
} else {
k = 0
count++
console.log('第几圈')
pirze[pirze.length - 1].style.background = 'royalblue'
pirze[k].style.background = 'pink'
}
if (count <= 5) {
if (time <= 100) {
time = 100
} else {
time -= 20
}
} else {
if (time >= 500) {
time = 500
} else {
time += 20
}
}
if (count > 7 && rom == k) {
clearInterval(int)
count = 0
rom = 0
time = 500
button.onclick = eve
let text = pirze[k].innerHTML
setTimeout(function () {
alert('恭喜您获得:' + text)
}, 300)
} else {
clearInterval(int)
int = setInterval(pu, time)
}
}
</script>
</body>
</html>