Native JS Realizes Jiugongge Lucky Draw
- 2021-08-21 19:28:11
- OfStack
In this paper, we share the specific code of JS to realize the 9-grid lottery for your reference. The specific contents are as follows
On the code:
<div class="wrapper">
<div> Thank you for your patronage </div>
<div>10 Ten thousand yuan in cash </div>
<div> Thank you for your patronage </div>
<div>iphone11</div>
<div> Lucky draw </div>
<div> Beautiful refrigerator </div>
<div> Thank you for your patronage </div>
<div>50 Yuan red envelope </div>
<div> Thank you for your patronage </div>
</div>
<div class="result"></div>
CSS style code:
<style>
.wrapper {
width: 300px;
height: 300px;
display: flex;
flex-flow: row wrap;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
border: 1px solid red;
}
.wrapper div {
flex: none;
width: 100px;
height: 100px;
box-sizing: border-box;
border: 1px solid red;
display: flex;
align-items: center;
justify-content: center;
}
.active {
background-color: red;
}
.wrapper div:nth-child(5) {
cursor: pointer;
}
.result {
height: 100px;
display: inline-block;
position: absolute;
top: 50px;
left: 0;
right: 0;
margin: auto;
text-align: center;
line-height: 100px;
font-size: 40px;
font-weight: 700;
color: #ff4400;
}
</style>
JS code:
<script>
var t, m, num, time, index, target, current;
// With index value as 0 , 1 , 2 , 5 , 8 , 7 , 6 , 3 Adj. div Element is the loop target,
// Because with num The total number is decreasing, so the array is defined in reverse order
var arr = [3, 6, 7, 8, 5, 2, 1, 0];
var div = document.querySelectorAll('.wrapper div');
var result = document.querySelector('.result');
div[4].onclick = function() {
clearInterval(time);
div[4].innerHTML = ' In the lottery ...';
result.innerHTML = '';
// The winning goal is set to 0 To 7 Random integer of
target = Math.floor(Math.random() * 8);
// The starting position is set to random, and the num Is the total number of cycles
num = Math.floor(Math.random() * 8) + 40;
// Of the total number of cycles 2/3 Save, easy to adjust the time when the rate peak appears
// If m For the total cycle 1/2 The peak velocity appears in the middle of the total duration
m = Math.floor(num * 2 / 3);
// Here if Statement can limit the winning, starting from the 1 The starting outer rings correspond clockwise respectively 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0
// Such as setting target == 6 That is, under restriction 10 Ten thousand yuan in cash, the following code is 100% Don't win a prize
if (target == 6|| target == 4 || target == 2 || target == 0) {
target++;
}
speed();
function speed() {
// Will loop the target div To an expression that converts the index value of to the total number of loops
index = arr[num % 8];
// Adds a style to the current loop element and removes the previous style
if (current) {
current.remove('active');
}
div[index].classList.add('active');
current = div[index].classList;
// Speed function, adjustable speed
t = Math.floor(Math.pow((num - m), 2) + 250);
//1 Secondary timer, nested recursive loop control speed
time = setTimeout(function() {
speed()
}, t)
// Judge the winning result
if (num == target) {
clearTimeout(time);
div[4].innerHTML = ' Lucky draw ';
switch (target) {
case 6:
result.innerHTML = ' Congratulations on your winning ' + div[arr[target % 8]].innerHTML + ' Grand prize ';
break;
case 4:
result.innerHTML = ' Congratulations on your winning ' + div[arr[target % 8]].innerHTML;
break;
case 2:
result.innerHTML = ' Congratulations on your winning ' + div[arr[target % 8]].innerHTML;
break;
case 0:
result.innerHTML = ' Congratulations on your winning ' + div[arr[target % 8]].innerHTML;
break;
default:
result.innerHTML = div[arr[target % 8]].innerHTML;
}
}
num--;
}
}
</script>