JS實現(xiàn)方形抽獎效果
更新時間:2018年08月27日 16:03:38 作者:ProsperLee
這篇文章主要為大家詳細介紹了JS實現(xiàn)方形抽獎效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了JS實現(xiàn)抽獎效果展示的具體代碼,供大家參考,具體內(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)">隨機抽/停止</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:
// 定義一個獎池
var jackpot = [
['獎品A1', '獎品A2', '獎品A3'],
['獎品B1', '獎品B2', '獎品B3'],
['獎品C1', '獎品C2', '獎品C3']
];
/**
* [table 創(chuàng)建表格]
* @param {[Array]} arr [獎品數(shù)組]
* @param {[String]} selector [選擇器]
* @return {[String]} table [返回一個HTML標簽]
*/
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;
}
// 輸出獎池
document.getElementById('table').innerHTML = table(jackpot, 'p');
var key = true; // start,startRan控制器
var num = 3; // 抽獎次數(shù)
// 抽過的還能抽 可定義抽獎次數(shù)-->次數(shù)限制 num需要定義
// 不定義抽獎次數(shù)-->次數(shù)無限 num不需定義
// 抽過的不能抽 可定義抽獎次數(shù)-->次數(shù)限制(次數(shù)不超過選擇器長度) num需要定義
// 不定義抽獎次數(shù)-->次數(shù)等于選擇器長度 num需要定義
/**
* [start 開始抽獎]
* @param {[String]} selector [選擇器]
* @param {[String]} addselector [給選中的添加樣式]
* @param {[String]} newaddselector [中獎獎品樣式]
* @param {[Number]} speed [時間越小,速度越快]
* @return {[type]} [description]
*/
function start(selector, addselector, newaddselector, speed) {
if (key) {
if (typeof(num) == 'undefined' || num != 0) {
var count = 0;
// 如果寫成var timer會每次執(zhí)行時重新定義一個timer,那么clearInterval(timer)只能清除后面定義的那個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é)束");
}
} else {
clearInterval(timer);
// 決定抽中的獎品的樣式和抽中的獎品能否繼續(xù)抽
$('.' + addselector).addClass(newaddselector).removeClass(selector);
// 獎品
console.log($('.' + addselector).html());
}
key = !key;
}
/**
* [start 開始抽獎]
* @param {[String]} selector [選擇器]
* @param {[String]} addselector [給選中的添加樣式]
* @param {[String]} newaddselector [中獎獎品樣式]
* @param {[Number]} speed [時間越小,速度越快]
* @return {[type]} [description]
*/
function startRan(selector, addselector, newaddselector, speed) {
if (key) {
if (typeof(num) == 'undefined' || num != 0) {
// 如果寫成var timer會每次執(zhí)行時重新定義一個timer,那么clearInterval(timer)只能清除后面定義的那個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é)束");
}
} else {
clearInterval(timer);
// 決定抽中的獎品的樣式和抽中的獎品能否繼續(xù)抽
$('.' + addselector).addClass(newaddselector).removeClass(selector);
// 獎品
console.log($('.' + addselector).html());
}
key = !key;
}
GitHub:地址
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript高級程序設(shè)計 閱讀筆記(十八) js跨平臺的事件
js跨平臺的事件經(jīng)驗分享,需要的朋友可以參考下2012-08-08
抓取JavaScript動態(tài)加載的內(nèi)容的方法總結(jié)
JavaScript動態(tài)加載的內(nèi)容常見于現(xiàn)代Web應(yīng)用中,用于增強用戶體驗和減少初始頁面加載時間,然而,這些動態(tài)加載的內(nèi)容對于傳統(tǒng)的網(wǎng)頁抓取工具來說往往是不可見的,本文主要介紹了有JavaScript動態(tài)加載的內(nèi)容如何抓取,需要的朋友可以參考下2024-09-09
TypeScript模塊與命名空間的關(guān)系和使用方法
在TypeScript中就像在EC5中一樣,任何包含頂級import或export的文件都被認為是一個模塊,下面這篇文章主要給大家介紹了關(guān)于如何在TypeScript使用模塊與命名空間以及注意事項的相關(guān)資料,需要的朋友可以參考下2023-03-03
javascript實現(xiàn)遮罩層動態(tài)效果實例
這篇文章主要介紹了javascript實現(xiàn)遮罩層動態(tài)效果,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-05-05

