这篇文章主要介绍js怎么实现随机抽奖,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
前言
在前端的开发当中,我们肯定会遇到随机抽奖的需求。我们要怎么去实现呢?下面就来分享随机抽奖的JS代码,有需要的小伙伴可以复制到编译器当中运行查看效果。
随机抽奖的JS代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#wrap {
text-align: center;
width: 500px;
margin: 100px auto;
position: relative;
}
#ul1 {
width: 303px;
height: 303px;
margin: 50px auto;
padding: 0;
border-top: 1px solid black;
border-left: 1px solid black;
}
#ul1 li {
float: left;
border-right: 1px solid black;
border-bottom: 1px solid black;
list-style: none;
width: 100px;
height: 100px;
line-height: 100px;
text-align: center;
}
#tooltips {
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
position: absolute;
top: 0;
z-index: 999;
display: none;
}
#info .btn button {
background-color: #009f95;
color: white;
outline: none;
font-size: 10px;
width: 60px;
height: 30px;
margin-left: 300px;
}
#info .content {
height: 120px;
padding: 20px;
box-sizing: border-box;
}
</style>
</head>
<body>
<div id="wrap">
<button id="btn">开始抽奖</button>
<ul id="ul1">
<li>鼠标</li>
<li>1000万</li>
<li>100优惠券</li>
<li>很遗憾</li>
<li>键盘</li>
<li>iPhoneX</li>
<li>很遗憾</li>
<li>迪拜10日游</li>
<li>很遗憾</li>
</ul>
</div>
<!--提示信息-->
<div id="tooltips">
<div id="info">
<div class="title">信息</div>
<div class="content" id="content">恭喜你,中奖啦!!!</div>
<div class="btn">
<button id="confirm">确定</button>
</div>
</div>
</div>
<script type="text/javascript">
// 思路:1.实现红色背景切换 2当运动停止,弹出对话框-- 用js去修改tooltips的display属性 变为block
var oStart = document.getElementById("btn")
// li标签
var aLi = document.getElementsByTagName("li")
// 提示框
var oTooltips = document.getElementById("tooltips")
// 提示框的确定按钮
var oConfirm = document.getElementById("confirm")
// 提示框的提示内容
var oContent = document.getElementById("content")
// 定时器id
var timmer = null
// 设置oTooltips的高度和html文档高度一样,这样把所有的内容都遮住
oTooltips.style.height = document.documentElement.offsetHeight + "px"
oStart.onclick = function() {
// 清空计时器
clearInterval(timmer)
// 定义一个下标
var nowIndex = 0
// 生成一个随机数,跑到第四圈的时候产生一个随机中奖数字
var randomInt = getRandomInt(26, 35)
// 下面代码只是为了给用户感觉:正在抽奖
timmer = setInterval(function() {
changeColor(aLi, nowIndex % aLi.length)
// 下标自动+1
nowIndex++
console.log("切换的下标", nowIndex, "随机数", randomInt)
// randomInt表示中奖的数字 ,如果nowIndex和randomInt一样,我们就认为当前的li是抽中的奖品
if(nowIndex === randomInt) {
clearInterval(timmer)
// 停止以后,还应该往后切换一次
changeColor(aLi, nowIndex % aLi.length)
// 在停止的时候,获取到当前抽中的li的内容
if(aLi[randomInt % aLi.length].innerHTML === "很遗憾") {
oContent.innerHTML = "很遗憾没有中奖"
} else {
oContent.innerHTML = "恭喜你,你抽中了" + aLi[randomInt % aLi.length].innerHTML
}
oTooltips.style.display = "block"
}
}, 100)
// 什么时候停止?当中奖的时候停止,抽中了谁?
// 可以用随机数生成一个具体的数字 randomInt
// 完善功能:提示用户抽中了什么 2让背景切换多跑几圈
}
// 当点击提示框确定按钮的时候,提示框消失
oConfirm.onclick = function() {
oTooltips.style.display = "none"
}
// 封装切换一个切换背景的方法
function changeColor(aLi, nowIndex) {
for(var i = 0; i < aLi.length; i++) {
// 清除上一个红色背景,全部设置成白色
aLi[i].style.backgroundColor = "white"
}
// 当前下标背景设置成红色
aLi[nowIndex].style.backgroundColor = "red"
}
// 获取随机数的方法
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
</script>
</body>
</html>
以上是“js怎么实现随机抽奖”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。