JS實(shí)現(xiàn)方形抽獎(jiǎng)效果
更新時(shí)間:2018年08月27日 16:03:38 作者:ProsperLee
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)方形抽獎(jiǎng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了JS實(shí)現(xiàn)抽獎(jiǎng)效果展示的具體代碼,供大家參考,具體內(nèi)容如下
展示:


HTML:
<div id="table"></div>
<div id="btn">
<button onclick="start('p', 'active','newactive', 100)">順序抽/停止</button>
<button onclick="startRan('p', 'active','newactive', 100)">隨機(jī)抽/停止</button>
</div>
CSS:
table {
text-align: center;
border-collapse: collapse;
}
table * {
width: 60px;
height: 60px;
}
#btn {
box-sizing: border-box;
width: 190px;
display: flex;
justify-content: space-between;
align-items: center;
}
#btn * {
flex-grow: 1;
background-color: red;
border: 1px solid #000;
color: #fff;
height: 30px;
font-size: 10px;
}
.active {
background-color: #ccc;
}
.newactive {
background-color: #00ffff;
}
JavaScript:
// 定義一個(gè)獎(jiǎng)池
var jackpot = [
['獎(jiǎng)品A1', '獎(jiǎng)品A2', '獎(jiǎng)品A3'],
['獎(jiǎng)品B1', '獎(jiǎng)品B2', '獎(jiǎng)品B3'],
['獎(jiǎng)品C1', '獎(jiǎng)品C2', '獎(jiǎng)品C3']
];
/**
* [table 創(chuàng)建表格]
* @param {[Array]} arr [獎(jiǎng)品數(shù)組]
* @param {[String]} selector [選擇器]
* @return {[String]} table [返回一個(gè)HTML標(biāo)簽]
*/
function table(arr, selector) {
var table = '<table border="1">';
for (var i = 0; i < arr.length; i++) {
table += '<tr>';
for (var j = 0; j < arr[i].length; j++) {
table += '<td class="' + selector + '">' + arr[i][j] + '</td>';
}
table += '</tr>';
}
table += '</table>';
return table;
}
// 輸出獎(jiǎng)池
document.getElementById('table').innerHTML = table(jackpot, 'p');
var key = true; // start,startRan控制器
var num = 3; // 抽獎(jiǎng)次數(shù)
// 抽過的還能抽 可定義抽獎(jiǎng)次數(shù)-->次數(shù)限制 num需要定義
// 不定義抽獎(jiǎng)次數(shù)-->次數(shù)無限 num不需定義
// 抽過的不能抽 可定義抽獎(jiǎng)次數(shù)-->次數(shù)限制(次數(shù)不超過選擇器長度) num需要定義
// 不定義抽獎(jiǎng)次數(shù)-->次數(shù)等于選擇器長度 num需要定義
/**
* [start 開始抽獎(jiǎng)]
* @param {[String]} selector [選擇器]
* @param {[String]} addselector [給選中的添加樣式]
* @param {[String]} newaddselector [中獎(jiǎng)獎(jiǎng)品樣式]
* @param {[Number]} speed [時(shí)間越小,速度越快]
* @return {[type]} [description]
*/
function start(selector, addselector, newaddselector, speed) {
if (key) {
if (typeof(num) == 'undefined' || num != 0) {
var count = 0;
// 如果寫成var timer會(huì)每次執(zhí)行時(shí)重新定義一個(gè)timer,那么clearInterval(timer)只能清除后面定義的那個(gè)timer,前面定義的已經(jīng)沒有變量指向了 無法清除
timer = setInterval(function() {
if (count < $('.' + selector).length) {
$('.' + selector).eq(count).addClass(addselector);
$('.' + selector).eq(count).siblings().removeClass(addselector);
$('.' + selector).eq(count).parent().siblings().children().removeClass(addselector);
count++;
} else {
count = 0;
}
}, speed);
if(typeof(num) != 'undefined'){
num--;
}
} else{
key = false;
console.log("抽獎(jiǎng)結(jié)束");
}
} else {
clearInterval(timer);
// 決定抽中的獎(jiǎng)品的樣式和抽中的獎(jiǎng)品能否繼續(xù)抽
$('.' + addselector).addClass(newaddselector).removeClass(selector);
// 獎(jiǎng)品
console.log($('.' + addselector).html());
}
key = !key;
}
/**
* [start 開始抽獎(jiǎng)]
* @param {[String]} selector [選擇器]
* @param {[String]} addselector [給選中的添加樣式]
* @param {[String]} newaddselector [中獎(jiǎng)獎(jiǎng)品樣式]
* @param {[Number]} speed [時(shí)間越小,速度越快]
* @return {[type]} [description]
*/
function startRan(selector, addselector, newaddselector, speed) {
if (key) {
if (typeof(num) == 'undefined' || num != 0) {
// 如果寫成var timer會(huì)每次執(zhí)行時(shí)重新定義一個(gè)timer,那么clearInterval(timer)只能清除后面定義的那個(gè)timer,前面定義的已經(jīng)沒有變量指向了 無法清除
timer = setInterval(function() {
var count = Math.floor(Math.random() * $('.' + selector).length);
$('.' + selector).eq(count).addClass(addselector);
$('.' + selector).eq(count).siblings().removeClass(addselector);
$('.' + selector).eq(count).parent().siblings().children().removeClass(addselector);
}, speed);
if(typeof(num) != 'undefined'){
num--;
}
} else {
key = false;
console.log("抽獎(jiǎng)結(jié)束");
}
} else {
clearInterval(timer);
// 決定抽中的獎(jiǎng)品的樣式和抽中的獎(jiǎng)品能否繼續(xù)抽
$('.' + addselector).addClass(newaddselector).removeClass(selector);
// 獎(jiǎng)品
console.log($('.' + addselector).html());
}
key = !key;
}
GitHub:地址
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- js和html5實(shí)現(xiàn)手機(jī)端刮刮卡抽獎(jiǎng)效果完美兼容android/IOS
- js輪盤抽獎(jiǎng)實(shí)例分析
- js抽獎(jiǎng)實(shí)現(xiàn)隨機(jī)抽獎(jiǎng)代碼效果
- js實(shí)現(xiàn)大轉(zhuǎn)盤抽獎(jiǎng)游戲?qū)嵗?/a>
- js簡單抽獎(jiǎng)代碼
- js實(shí)現(xiàn)簡單隨機(jī)抽獎(jiǎng)的方法
- jquery.rotate.js實(shí)現(xiàn)可選抽獎(jiǎng)次數(shù)和中獎(jiǎng)內(nèi)容的轉(zhuǎn)盤抽獎(jiǎng)代碼
- javascript 隨機(jī)抽獎(jiǎng)程序代碼
- JS模擬抽獎(jiǎng)序效果實(shí)現(xiàn)代碼
- 原生JS實(shí)現(xiàn)九宮格抽獎(jiǎng)效果
相關(guān)文章
javascript Array.prototype.slice使用說明
slice 可以用來獲取數(shù)組片段,它返回新數(shù)組,不會(huì)修改原數(shù)組。2010-10-10
NestJS裝飾器實(shí)現(xiàn)GET請(qǐng)求
本文介紹了如何通過裝飾器實(shí)現(xiàn)GET請(qǐng)求,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-10-10

