JS實(shí)現(xiàn)方形抽獎(jiǎng)效果
本文實(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ù) // 抽過(guò)的還能抽 可定義抽獎(jiǎng)次數(shù)-->次數(shù)限制 num需要定義 // 不定義抽獎(jiǎng)次數(shù)-->次數(shù)無(wú)限 num不需定義 // 抽過(guò)的不能抽 可定義抽獎(jiǎng)次數(shù)-->次數(shù)限制(次數(shù)不超過(guò)選擇器長(zhǎng)度) num需要定義 // 不定義抽獎(jiǎng)次數(shù)-->次數(shù)等于選擇器長(zhǎng)度 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)沒(méi)有變量指向了 無(wú)法清除 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)沒(méi)有變量指向了 無(wú)法清除 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:地址
以上就是本文的全部?jī)?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ǎn)單抽獎(jiǎng)代碼
- js實(shí)現(xiàn)簡(jiǎ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高級(jí)程序設(shè)計(jì) 閱讀筆記(十八) js跨平臺(tái)的事件
js跨平臺(tái)的事件經(jīng)驗(yàn)分享,需要的朋友可以參考下2012-08-08抓取JavaScript動(dòng)態(tài)加載的內(nèi)容的方法總結(jié)
JavaScript動(dòng)態(tài)加載的內(nèi)容常見(jiàn)于現(xiàn)代Web應(yīng)用中,用于增強(qiáng)用戶體驗(yàn)和減少初始頁(yè)面加載時(shí)間,然而,這些動(dòng)態(tài)加載的內(nèi)容對(duì)于傳統(tǒng)的網(wǎng)頁(yè)抓取工具來(lái)說(shuō)往往是不可見(jiàn)的,本文主要介紹了有JavaScript動(dòng)態(tài)加載的內(nèi)容如何抓取,需要的朋友可以參考下2024-09-09TypeScript模塊與命名空間的關(guān)系和使用方法
在TypeScript中就像在EC5中一樣,任何包含頂級(jí)import或export的文件都被認(rèn)為是一個(gè)模塊,下面這篇文章主要給大家介紹了關(guān)于如何在TypeScript使用模塊與命名空間以及注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下2023-03-03js字符串轉(zhuǎn)換成xml對(duì)象并使用技巧解讀
在js中有方法可以將字符串轉(zhuǎn)化為xml對(duì)象,感興趣的朋友可以參考下面的代碼片段,希望對(duì)你有所幫助2013-04-04javascript實(shí)現(xiàn)遮罩層動(dòng)態(tài)效果實(shí)例
這篇文章主要介紹了javascript實(shí)現(xiàn)遮罩層動(dòng)態(tài)效果,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05使用js實(shí)現(xiàn)單鏈解決前端隊(duì)列問(wèn)題的方法
這篇文章主要介紹了使用js實(shí)現(xiàn)單鏈解決前端隊(duì)列問(wèn)題的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02