原生JS實(shí)現(xiàn)九宮格抽獎(jiǎng)
本文實(shí)例為大家分享了JS實(shí)現(xiàn)九宮格抽獎(jiǎng)的具體代碼,供大家參考,具體內(nèi)容如下
上代碼:
<div class='wrapper'> <div>謝謝惠顧</div> <div>十萬元現(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語句可限制中獎(jiǎng),從第一個(gè)開始外圈順時(shí)針分別對(duì)應(yīng)7,6,5,4,3,2,1,0 //如設(shè)置target == 6 即限制中十萬元現(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í)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python 寫一個(gè)文件分發(fā)小程序2. Python本地及虛擬解釋器配置過程解析3. Python importlib模塊重載使用方法詳解4. Vue3中使用this的詳細(xì)教程5. Python 利用flask搭建一個(gè)共享服務(wù)器的步驟6. Python中Anaconda3 安裝gdal庫(kù)的方法7. 用python對(duì)oracle進(jìn)行簡(jiǎn)單性能測(cè)試8. Python自動(dòng)化之定位方法大殺器xpath9. Python類綁定方法及非綁定方法實(shí)例解析10. Python Selenium破解滑塊驗(yàn)證碼最新版(GEETEST95%以上通過率)
