抽奖
<!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>
.body-div{
width: 600px;
height: 600px;
margin: auto;
display: flex;
flex-wrap: wrap;
}
.ele{
width: 200px;
height: 200px;
background-color: rgb(245, 222, 179);
line-height: 200px;
text-align: center;
color: white;
border: 2px solid white;
box-sizing: border-box;
}
.center{
background-color: brown;
}
</style>
</head>
<body>
<div class="body-div">
<div class="ele" id="item1">一折</div>
<div class="ele" id="item2">二折</div>
<div class="ele" id="item3">三折</div>
<div class="ele" id="item8">八折</div>
<div class="ele center" >开始</div>
<div class="ele" id="item4">四折</div>
<div class="ele" id="item7">七折</div>
<div class="ele" id="item6">六折</div>
<div class="ele" id="item5">五折</div>
</div>
<script>
var centerdiv=document.querySelector(".center");
var lastIndex=8;
var index=1;
var count=0;
console.log(centerdiv);
centerdiv.addEventListener('click',function(){
var win = Math.floor(Math.random()*8+1);
var circle=Math.ceil(Math.random()*7+1);
var timer=setInterval(function(){
index++;
if(index>8){
index=1;
count++;
}
var indexDiv = document.getElementById('item'+index)
var lastIndexDiv = document.getElementById('item'+lastIndex)
indexDiv.style.backgroundColor = "rgb(243, 199, 116)"
lastIndexDiv.style.backgroundColor = "rgb(245, 222, 179)"
lastIndex=index;
if(count==circle && index==win)
{
clearInterval(timer);
count=0;
index = 0;
}
},100)
});
</script>
</body>
</html>