域名預(yù)訂/競(jìng)價(jià),好“米”不錯(cuò)過(guò)
這篇文章主要為大家詳細(xì)介紹了原生JS九宮格抽獎(jiǎng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了JS實(shí)現(xiàn)九宮格抽獎(jiǎng)的具體代碼,供大家參考,具體內(nèi)容如下
上代碼:
<div class="wrapper">
<div>謝謝惠顧</div>
<div>十萬(wàn)元現(xiàn)金</div>
<div>謝謝惠顧</div>
<div>iphone11</div>
<div>抽獎(jiǎng)</div>
<div>美的冰箱</div>
<div>謝謝惠顧</div>
<div>50元紅包</div>
<div>謝謝惠顧</div>
</div>
<div class="result"></div>
CSS樣式代碼:
<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代碼:
<script>
var t, m, num, time, index, target, current;
//以索引值為0,1,2,5,8,7,6,3的div元素為循環(huán)目標(biāo),
//因?yàn)橐詎um總數(shù)遞減的方式進(jìn)行循環(huán),故將數(shù)組倒序定義
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 = '抽獎(jiǎng)中...';
result.innerHTML = '';
//中獎(jiǎng)目標(biāo)設(shè)為0到7的隨機(jī)整數(shù)
target = Math.floor(Math.random() * 8);
//起始位置設(shè)為隨機(jī),且以num為總的循環(huán)數(shù)
num = Math.floor(Math.random() * 8) + 40;
//將總循環(huán)數(shù)的2/3保存,方便調(diào)整速率峰值出現(xiàn)的時(shí)間
//若m為總循環(huán)的1/2,則速度峰值會(huì)在總時(shí)長(zhǎng)的中間出現(xiàn)
m = Math.floor(num * 2 / 3);
//此處if語(yǔ)句可限制中獎(jiǎng),從第一個(gè)開(kāi)始外圈順時(shí)針?lè)謩e對(duì)應(yīng)7,6,5,4,3,2,1,0
//如設(shè)置target == 6 即限制中十萬(wàn)元現(xiàn)金,以下代碼為100%不中獎(jiǎng)
if (target == 6|| target == 4 || target == 2 || target == 0) {
target++;
}
speed();
function speed() {
//將循環(huán)目標(biāo)div的索引值轉(zhuǎn)換為循環(huán)總數(shù)的表達(dá)式
index = arr[num % 8];
//給當(dāng)前循環(huán)元素添加樣式,并移除之前的樣式
if (current) {
current.remove('active');
}
div[index].classList.add('active');
current = div[index].classList;
//速度函數(shù),可調(diào)試速率
t = Math.floor(Math.pow((num - m), 2) + 250);
//一次性定時(shí)器,嵌套遞歸循環(huán)控制速度
time = setTimeout(function() {
speed()
}, t)
//判斷中獎(jiǎng)結(jié)果
if (num == target) {
clearTimeout(time);
div[4].innerHTML = '抽獎(jiǎng)';
switch (target) {
case 6:
result.innerHTML = '恭喜您抽中' + div[arr[target % 8]].innerHTML + '大獎(jiǎng)';
break;
case 4:
result.innerHTML = '恭喜您抽中' + div[arr[target % 8]].innerHTML;
break;
case 2:
result.innerHTML = '恭喜您抽中' + div[arr[target % 8]].innerHTML;
break;
case 0:
result.innerHTML = '恭喜您抽中' + div[arr[target % 8]].innerHTML;
break;
default:
result.innerHTML = div[arr[target % 8]].innerHTML;
}
}
num--;
}
}
</script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
來(lái)源:腳本之家
鏈接:https://www.jb51.net/article/195524.htm
申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!