单文件抽奖小工具(不放回抽)

news/2024/9/20 6:47:44/文章来源:https://www.cnblogs.com/suifeng2000/p/18374146

单文件抽奖小工具(不放回抽)

创建时间:2024-08-12

一、HTML 部分

这段 HTML 代码构建了抽奖小工具的页面结构。引入了 jQuery 库用于后续的 JavaScript 操作,定义了两个音频元素用于播放抽奖相关音效。h1 标签显示“抽奖”标题,span 标签用于显示时间,wrapDiv 包含了抽奖的主要区域,如参与抽奖的人员列表、抽奖按钮和已选中人员列表。

<!doctype html>
<html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>抽奖小工具</title><script src="https://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script><style><!-- 此处是一系列的样式定义,包括页面整体背景、字体、各个元素的布局、颜色、边框、阴影等 --></style>
</head><body><audio id="drawSound" src="draw.mp3" preload="auto"></audio><audio id="drawSound1" src="drawSound1.mp3" preload="auto"></audio><h1>抽奖</h1><span id="span"></span><div class="wrapDiv"><div id="leftBox" class="leftBox"></div><input type="button" id="btn" value="开始抽奖"><div id="selectedName" class="selectedName"><h1>抽中名单</h1></div></div><img src="./first-logo.png" alt="First Logo" style="position: absolute; top: 80px; left: 100px; width: 150px; height: auto;"><!--    <img src="./second-logo.png" alt="Second Logo" style="position: absolute; top: 94px; left: 200px; width: 150px; height: auto;">--><br>
</body></html>

二、CSS 部分

这里通过 CSS 对页面元素进行了详细的样式设置。比如,body 的背景、字体和颜色,wrapDiv 的宽度、位置、背景、阴影和边框,leftBox 的边框和溢出处理,spanbtn 的样式,nameBox 的样式及其鼠标悬停效果,selectedName 的样式等等,共同营造出美观且具有交互性的页面效果。

body {background-color: #f0f8ff;font-family: 'Arial', sans-serif;color: #333;
}.wrapDiv {width: 80%;max-width: 1200px;margin: 0 auto;text-align: center;position: absolute;top: 80px;left: 0;right: 0;background: #ffffff;box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);border-radius: 10px;padding: 20px;
}.leftBox {width: 100%;height: auto;margin: 20px auto;padding: 10px;border-bottom: 2px solid #e0e0e0;overflow: hidden;
}#span {float: right;margin-top: 10px;font-size: 16px;color: #666;
}#btn {width: 150px;height: 40px;background-color: #007bff;color: white;border: none;border-radius: 5px;cursor: pointer;margin: 20px auto;font-size: 16px;
}#btn:hover {background-color: #0056b3;
}.nameBox {width: 100px;height: 40px;float: left;background-color: #f8f9fa;margin: 10px;text-align: center;line-height: 40px;border-radius: 5px;box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);transition: background-color 0.3s, color 0.3s;
}.nameBox:hover {background-color: #007bff;color: white;
}.selectedName {width: 100%;background: #f8f9fa;margin-top: 20px;padding: 20px;border-radius: 10px;box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);height: auto;
}.selectedName h1 {color: #007bff;
}h1 {text-align: center;font-size: 24px;margin-bottom: 20px;color: #007bff;
}

三、JavaScript 部分

在 JavaScript 部分,首先定义了一个包含众多人员名字的数组 arr ,并通过 shuffle 函数对其进行随机打乱。initForm 函数用于初始化页面布局,包括计算并设置已选中人员列表的高度,以及动态创建参与抽奖人员的盒子。

clearBoxColor 函数用于清除所有盒子的背景颜色,setBoxColor 函数用于根据当前选中的人员设置相应盒子的背景颜色为红色。appendSelectedName 函数用于将选中的人员添加到已选中人员列表中。

点击抽奖按钮时,根据按钮的当前值决定是开始还是停止抽奖。开始抽奖时,通过 setInterval 定时随机选择人员并设置颜色,同时播放音效;停止抽奖时,暂停音效,将选中人员添加到已选中列表,从原数组中移除,并更新按钮的值。

getTime 函数用于获取当前时间,并通过 setInterval 每秒更新页面上显示的时间

var arr = ['刘备','关羽','张飞','赵云','马超','黄忠','诸葛亮','庞统','姜维','魏延','马岱','关平','周仓','廖化','法正','李严','黄权','孟达','刘封','马良','马谡','蒋琬','费祎','董允','向宠','张苞','关兴','曹仁','曹洪','夏侯惇','夏侯渊','张辽','张郃','徐晃','许褚','典韦','郭嘉','荀彧','荀攸','贾诩','程昱','司马懿','司马师','司马昭','邓艾','钟会','孙策','孙权','周瑜','鲁肃','貂蝉','大乔','小乔','孙尚香'];
var orgArrCount = arr.length;
var currentSelectNum = 0;function shuffle(array) {for (let i = array.length - 1; i > 0; i--) {const j = Math.floor(Math.random() * (i + 1));[array[i], array[j]] = [array[j], array[i]];}return array;
}arr = shuffle(arr);initForm();function initForm() {var selectedNameHeight = orgArrCount / 3 * 40 + 120;$("#selectedName").css("height", selectedNameHeight + "px");dynamicCreateBox();
}function dynamicCreateBox() {for (var i = 0; i < arr.length; i++) {var div = document.createElement("div");div.innerText = arr[i];div.className = "nameBox";$("#leftBox").append(div);}
}function clearBoxColor() {$("#leftBox").children("div").each(function () {$(this).css("background-color", "");});
}function setBoxColor() {$("#leftBox").children("div").each(function () {var thisText = ($(this).text());var selectedName = arr[currentSelectNum];if (thisText == selectedName) {$(this).css("background-color", "red");}});
}function appendSelectedName() {var div = document.createElement("div");div.innerText = arr[currentSelectNum];div.className = "nameBox";$("#selectedName").append(div);
}$('#btn').click(function () {var curentCount = arr.length;if (curentCount < 1) {alert("全部抽完了~~~~");clearBoxColor();return;}if (this.value === "开始抽奖") {// 播放音效document.getElementById('drawSound').play();document.getElementById('drawSound1').play();timeId = setInterval(function () {clearBoxColor();var num = Math.floor(Math.random() * curentCount);currentSelectNum = num;setBoxColor();}, 90);this.value = "停止";} else {// 停止音效document.getElementById('drawSound').pause();// document.getElementById('drawSound1').pause();document.getElementById('drawSound').currentTime = 0;// document.getElementById('drawSound1').currentTime = 0;clearInterval(timeId);appendSelectedName();arr.splice(currentSelectNum, 1);this.value = "开始抽奖";}
});getTime();
setInterval(getTime, 1000);function getTime() {var day = new Date();var year = day.getFullYear();var month = day.getMonth() + 1;var dat = day.getDate();var hour = day.getHours();var minute = day.getMinutes();var second = day.getSeconds();month = month < 10? "0" + month : month;dat = dat < 10? "0" + dat : dat;hour = hour < 10? "0" + hour : hour;minute = minute < 10? "0" + minute : minute;second = second < 10? "0" + second : second;$("#span").text(year + "-" + month + "-" + dat + " " + hour + ":" + minute + ":" + second);
}

四、完整代码

<!doctype html>
<html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>抽奖小工具</title><script src="https://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script><style>body {background-color: #f0f8ff;font-family: 'Arial', sans-serif;color: #333;}.wrapDiv {width: 80%;max-width: 1200px;margin: 0 auto;text-align: center;position: absolute;top: 80px;left: 0;right: 0;background: #ffffff;box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);border-radius: 10px;padding: 20px;}.leftBox {width: 100%;height: auto;margin: 20px auto;padding: 10px;border-bottom: 2px solid #e0e0e0;overflow: hidden;}#span {float: right;margin-top: 10px;font-size: 16px;color: #666;}#btn {width: 150px;height: 40px;background-color: #007bff;color: white;border: none;border-radius: 5px;cursor: pointer;margin: 20px auto;font-size: 16px;}#btn:hover {background-color: #0056b3;}.nameBox {width: 100px;height: 40px;float: left;background-color: #f8f9fa;margin: 10px;text-align: center;line-height: 40px;border-radius: 5px;box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);transition: background-color 0.3s, color 0.3s;}.nameBox:hover {background-color: #007bff;color: white;}.selectedName {width: 100%;background: #f8f9fa;margin-top: 20px;padding: 20px;border-radius: 10px;box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);height: auto;}.selectedName h1 {color: #007bff;}h1 {text-align: center;font-size: 24px;margin-bottom: 20px;color: #007bff;}</style>
</head><body>
<audio id="drawSound" src="draw.mp3" preload="auto"></audio>
<audio id="drawSound1" src="drawSound1.mp3" preload="auto"></audio><h1>抽奖</h1><span id="span"></span><div class="wrapDiv"><div id="leftBox" class="leftBox"></div><input type="button" id="btn" value="开始抽奖"><div id="selectedName" class="selectedName"><h1>抽中名单</h1></div></div><img src="./first-logo.png" alt="First Logo" style="position: absolute; top: 80px; left: 100px; width: 150px; height: auto;">
<!--    <img src="./second-logo.png" alt="Second Logo" style="position: absolute; top: 94px; left: 200px; width: 150px; height: auto;">--><br><script>var arr = ['刘备','关羽','张飞','赵云','马超','黄忠','诸葛亮','庞统','姜维','魏延','马岱','关平','周仓','廖化','法正','李严','黄权','孟达','刘封','马良','马谡','蒋琬','费祎','董允','向宠','张苞','关兴','曹仁','曹洪','夏侯惇','夏侯渊','张辽','张郃','徐晃','许褚','典韦','郭嘉','荀彧','荀攸','贾诩','程昱','司马懿','司马师','司马昭','邓艾','钟会','孙策','孙权','周瑜','鲁肃','貂蝉','大乔','小乔','孙尚香'];var orgArrCount = arr.length;var currentSelectNum = 0;function shuffle(array) {for (let i = array.length - 1; i > 0; i--) {const j = Math.floor(Math.random() * (i + 1));[array[i], array[j]] = [array[j], array[i]];}return array;}arr = shuffle(arr);initForm();function initForm() {var selectedNameHeight = orgArrCount / 3 * 40 + 120;$("#selectedName").css("height", selectedNameHeight + "px");dynamicCreateBox();}function dynamicCreateBox() {for (var i = 0; i < arr.length; i++) {var div = document.createElement("div");div.innerText = arr[i];div.className = "nameBox";$("#leftBox").append(div);}}function clearBoxColor() {$("#leftBox").children("div").each(function () {$(this).css("background-color", "");});}function setBoxColor() {$("#leftBox").children("div").each(function () {var thisText = ($(this).text());var selectedName = arr[currentSelectNum];if (thisText == selectedName) {$(this).css("background-color", "red");}});}function appendSelectedName() {var div = document.createElement("div");div.innerText = arr[currentSelectNum];div.className = "nameBox";$("#selectedName").append(div);}$('#btn').click(function () {var curentCount = arr.length;if (curentCount < 1) {alert("全部抽完了~~~~");clearBoxColor();return;}if (this.value === "开始抽奖") {// 播放音效document.getElementById('drawSound').play();document.getElementById('drawSound1').play();timeId = setInterval(function () {clearBoxColor();var num = Math.floor(Math.random() * curentCount);currentSelectNum = num;setBoxColor();}, 90);this.value = "停止";} else {// 停止音效document.getElementById('drawSound').pause();
<!--                document.getElementById('drawSound1').pause();-->document.getElementById('drawSound').currentTime = 0;
<!--                document.getElementById('drawSound1').currentTime = 0;-->clearInterval(timeId);appendSelectedName();arr.splice(currentSelectNum, 1);this.value = "开始抽奖";}});getTime();setInterval(getTime, 1000);function getTime() {var day = new Date();var year = day.getFullYear();var month = day.getMonth() + 1;var dat = day.getDate();var hour = day.getHours();var minute = day.getMinutes();var second = day.getSeconds();month = month < 10 ? "0" + month : month;dat = dat < 10 ? "0" + dat : dat;hour = hour < 10 ? "0" + hour : hour;minute = minute < 10 ? "0" + minute : minute;second = second < 10 ? "0" + second : second;$("#span").text(year + "-" + month + "-" + dat + " " + hour + ":" + minute + ":" + second);}</script></body></html>

五、目录树

    draw.mp3  # 声音2 drawSound1.mp3 # 声音1 first-logo.png # logoindex.html  # 主文件声音和logo文件可之间替换,抽奖的人也可以之间在index.html 文件里面之间进行修改。 不放回抽

六、效果

6.1 界面

6.2 抽奖过程

6.3 最后一个

七、文件位置

解压密码:  lottery蓝奏云: https://wwsl.lanzoul.com/iAAMb278n8yd百度网盘: 通过百度网盘分享的文件:抽奖脱敏版
链接:https://pan.baidu.com/s/1aTxx94Xjv17m4lWBVvDr5g?pwd=gcxn 
提取码:gcxn 
--来自百度网盘超级会员V6的分享gittee: 这是合集下面的一个小项目。
https://gitee.com/suifeng55549/small_tolls.git 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/785460.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【C#】.NET报错:所生成项目的处理器框架“MSIL”与引用“xxx”的处理器架构“AMD64”不匹配

一、现象所生成项目的处理器架构“MSIL”与引用“System.Data.SQLite, Version=1.0.60.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=x86”的处理器架构“AMD64”不匹配。这种不匹配可能会导致运行时失败。请考虑通过配置管理器更改您的项目的…

算法与数据结构——数据结构的分类

数据结构的分类 常见的数据结构包括数组、链表、栈、队列、哈希表、树、堆、图,它们可以从“逻辑结构”和“物理结构”两个维度进行分类 逻辑结构:线性与非线性 逻辑结构揭示了数据元素之间的逻辑关系。在数组和链表中,数据按照一定顺序排列,体现了数据之间的线性关系;而在…

Python 实现批量数字二维码生成器

Python 实现批量数字二维码生成器 创建时间:2024-08-09 一、背景 手动逐个生成特定格式和内容的二维码是一项繁琐且耗时的任务。虽然有写二维码工具也可以制作,但是往往有一些限制,为了能够高效、批量生成自定义二维码的需求,开发了这个基于 Python 的数字二维码生成器应用…

【工程应用十一】基于PatchMatch算法的图像修复研究(inpaint)。

这个东西是个非常古老的算法了,大概是2008年的东西,参考资料也有很多,不过基本上都是重复的。最近受一个朋友的需求,前后大概用了二十多天时间去研究,也有所成果,在这里简单的予以记录。图像修复这个东西目前流行的基本都是用深度学习来弄了,而且深度学习的效果还是非常…

jmeter基本操作

发送一个post请求 1、创建一个线程2、新建一个http请求编辑http请求的内容接口断言: 响应参数:{"code":"200","msg":"登录成功!","model":{}}查看结果:保存,运行 a、保存:b、运行红色表示错误 绿色表示成功 查看请求后…

Blazor开发框架Known-V2.0.9

V2.0.9 Known是基于Blazor的企业级快速开发框架,低代码,跨平台,开箱即用,一处代码,多处运行。本次版本主要是修复一些BUG和表格页面功能增强。官网:http://known.pumantech.com Gitee: https://gitee.com/known/Known Github:https://github.com/known/Known概述基于C#…

antd5版本修改Table组件滚动条样式

因为项目需求,要将Table组件的样式修改为UI图所给的效果,但是怎么写都不生效 最后发现在 .ant-table的样式中设了scrollbar-color,只要把这里的样式设为scrollbar-color: auto; 然后再改.ant-table-body里面滚动条的样式,就可以了.ant-table-body{&::-webkit-scrollbar…

ThinkPHP6同步千万级流水数据

ThinkPHP6定时任务同步千万级流水数据 多数据源配置 自定义指令 定时同步单次1000条 <?php declare (strict_types = 1);namespace app\command\SyncDtaTask;use think\console\Command; use think\console\Input; use think\console\Output; use think\Exception; use thi…

AtCoder Beginner Contest 047

A - Fighting over Candies 简单排序。 #include <bits/stdc++.h>using namespace std; using i64 = long long;int main() {ios::sync_with_stdio(false), cin.tie(nullptr);vector<int> a(3);cin >> a[0] >> a[1] >> a[2];sort(a.begin(), a.e…

CRTP 和静态多态

c++古典面试问题之一:面向对象编程三大特性--封装,继承,多态 c++古典面试问题之二:如何实现多态-- 当基类指针指向派生类对象,并通过这个指针调用在派生类中被重写的虚函数基于上述知识点,今天我们讲下另一种多态实现方式:CRTP (curiously recurring template pattern)虚…

小程序直传oss

直传使用 const host = <host>; const signature = <signatureString>; const ossAccessKeyId = <accessKey>; const policy = <policyBase64Str>; const key = <object name>; const securityToken = <x-oss-security-token>; const fi…

呼死你 手机轰炸机 (29021243

基于 短信轰炸机 原理研究并实现之后 又研究起了电话轰炸机 实现原理其实大同小异,最终的区别的 用户在进行短信发送 并未收到短信的情况下 【产生的原因有 网络信号原因、用户手机自动屏蔽原因】 可以利用第三方平台提供的语音验证码进行发送 这种情况也是通过fiddler进行抓包…