首页 技术 正文
技术 2022年11月18日
0 收藏 898 点赞 2,411 浏览 3050 个字

展示:

JS实现抽奖(方形)JS实现抽奖(方形)

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:;
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 创建表格]
* @param {[Array]} arr [奖品数组]
* @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; // 抽奖次数
43 // 抽过的还能抽 可定义抽奖次数-->次数限制 num需要定义
44 // 不定义抽奖次数-->次数无限 num不需定义
45 // 抽过的不能抽 可定义抽奖次数-->次数限制(次数不超过选择器长度) num需要定义
46 // 不定义抽奖次数-->次数等于选择器长度 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会每次执行时重新定义一个timer,那么clearInterval(timer)只能清除后面定义的那个timer,前面定义的已经没有变量指向了 无法清除
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("抽奖结束"); } } else { clearInterval(timer); // 决定抽中的奖品的样式和抽中的奖品能否继续抽
$('.' + 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会每次执行时重新定义一个timer,那么clearInterval(timer)只能清除后面定义的那个timer,前面定义的已经没有变量指向了 无法清除
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("抽奖结束"); } } else { clearInterval(timer); // 决定抽中的奖品的样式和抽中的奖品能否继续抽
$('.' + addselector).addClass(newaddselector).removeClass(selector); // 奖品
console.log($('.' + addselector).html()); } key = !key; }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902