写在开头
上期代码主要实现PC端电商网站商品放大效果,本期就来实现积分随机抽奖效果,开发久了很多功能都是通过框架组件库来完成,但是如果组件满足不了开发需求,还需要开发人员手动封装组件,专门出这样一期文章,通过原生js实现一些特定功能,功能也比较简单,也是想借助这样一个简单的功能,然后来帮助大家了解我们JavaScript,在前端中的作用,另外也培养下我们的代码思维,那我们本次就通过由简单到复杂循序渐进,这份专栏中我们会带领大家用前端实现拼图、无缝轮播图、图片瀑布流、读心术小游戏等等有趣的小功能,纯前端语言实现,都会陆续带给大家。
功能介绍
转盘抽奖功能,正常开发中中奖概率,抽奖次数都是后台数据返回的,就像你拼西西抽红包,因为是纯前端完成,概率完全随机数,页面逻辑代码中注释很完善,请看下图代码
页面创建
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>随机抽奖</title>
<link rel="stylesheet" href="demo.css">
</head>
<body>
<!-- 整体 -->
<div class="main">
<!-- 抽奖区域 -->
<div class="content-container">
<!-- 抽奖项 -->
<div class="price-list">
<img src="../img/prize_1.png" alt="">
<span>IphoneX19</span>
</div>
<div class="price-list">
<img src="../img/prize_2.png" alt="">
<span>金币20个</span>
</div>
<div class="price-list">
<img src="../img/prize_3.png" alt="">
<span>华为Mate 90 一部</span>
</div>
<div class="price-list">
<img src="../img/prize_4.png" alt="">
<span>金币10个</span>
</div>
<div class="price-list">
<img src="../img/prize_5.png" alt="">
<span>谢谢参与</span>
</div>
<div class="price-list">
<img src="../img/prize_6.png" alt="">
<span>手机优惠券</span>
</div>
<div class="price-list">
<img src="../img/prize_7.png" alt="">
<span>电脑优惠券</span>
</div>
<div class="price-list">
<img src="../img/prize_8.png" alt="">
<span>U盘</span>
</div>
<!-- 中间区域 -->
<div class="hander-container">
<div class="inner-container">
<img src="../img/center_1.png" alt="" class="hander-left">
<div class="hander-container-middle">
还可以抽奖 <span class="price-number">0</span>次
</div>
<div class="hander-container-btn"></div>
</div>
</div>
</div>
<!-- 弹窗区域 -->
<div class="dialog-container">
<div class="dialog-main">
<div class="head">
<span class="title">温馨提示</span>
<span class="close">×</span>
</div>
<div class="content">
每次抽奖将消耗 8000 积分
</div>
<div class="dialog-main-footer">
<div class="button">再来一次</div>
</div>
</div>
</div>
</div>
<script src="./demo.js"></script>
</body>
</html>
样式搭建
* {
margin: 0;
padding: 0;
}
/* 大背景 */
.main {
height: 546px;
background-image: url('../img/banner-bg.jpg');
position: relative;
}
/* 抽奖背景 */
.content-container {
position: absolute;
width: 874px;
height: 458px;
background-image: url('../img/bg.png');
/* 水平垂直居中 */
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
/* 奖项区域 */
.price-list {
width: 270px;
height: 100px;
position: absolute;
padding: 5px;
text-align: center;
background-color: #622230;
border: 1px solid #722536;
border-radius: 5px;
box-sizing: border-box;
}
.price-list>img {
max-height: 75px;
position: absolute;
left: 30px;
top: 15px;
}
.price-list>span {
font-size: 19px;
color: #ffb100;
font-weight: bold;
position: absolute;
left: 102px;
top: 36px;
width: 164px;
}
.price-list:nth-child(1) {
top: 22px;
left: 26px;
}
.price-list:nth-child(2) {
top: 22px;
left: 302px;
}
.price-list:nth-child(3) {
top: 22px;
right: 26px;
}
.price-list:nth-child(4) {
top: 129px;
right: 26px;
width: 100px;
height: 200px;
}
.price-list:nth-child(4)>img,
.price-list:nth-child(8)>img {
position: absolute;
left: 50%;
transform: translateX(-50%);
top: 15px;
}
.price-list:nth-child(4)>span,
.price-list:nth-child(8)>span {
position: absolute;
left: 50%;
transform: translateX(-50%);
bottom: 36px;
top: auto;
}
.price-list:nth-child(5) {
bottom: 22px;
right: 26px;
}
.price-list:nth-child(6) {
bottom: 22px;
left: 302px;
}
.price-list:nth-child(7) {
bottom: 22px;
left: 26px;
}
.price-list:nth-child(8) {
top: 129px;
left: 26px;
width: 100px;
height: 200px;
}
/* 中间抽奖区域 */
.hander-container {
width: 614px;
height: 198px;
background-image: url('../img/middle-bg.png');
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
.inner-container {
display: table-cell;
vertical-align: middle;
padding: 0 20px;
padding: 50px;
}
.hander-left {
float: left;
vertical-align: middle;
}
.hander-container-middle {
float: left;
color: #ffd200;
font-size: 30px;
font-weight: bold;
margin: 17px 0 0 18px;
}
.hander-container-btn {
width: 210px;
height: 102px;
background-image: url('../img/btn.png');
float: right;
cursor: pointer;
}
.active {
background-color: #ffd100;
}
.active>span {
color: #470c1b;
}
/* 对话框样式 */
.dialog-container {
position: fixed;
width: 100%;
height: 100%;
left: 0;
top: 0;
background-color: rgba(0, 0, 0, .5);
display: none;
}
.dialog-main {
width: 400px;
background-color: #fff;
position: absolute;
left: 50%;
top: 30%;
transform: translate(-50%, -50%);
box-sizing: border-box;
}
.dialog-main>.head {
height: 50px;
line-height: 50px;
background-color: #e99c3d;
border-bottom: 1px solid #ddd;
box-sizing: border-box;
padding: 0 20px;
}
.dialog-main>.head>.title {
float: left;
color: #fff;
font-size: 18px;
}
.dialog-main>.head>.close {
float: right;
color: #fff;
font-size: 42px;
cursor: pointer;
}
.dialog-main-footer {
text-align: right;
margin: 20px;
}
.dialog-main>.content {
text-align: center;
margin: 20px;
color: #622230;
font-weight: bold;
font-size: 20px;
}
.dialog-main-footer>.button {
background-color: #fca825;
height: 28px;
cursor: pointer;
display: inline-block;
line-height: 28px;
color: #fff;
padding: 0 10px;
border-radius: 3px;
}
主要逻辑
/*
1: 定义抽奖次数渲染
1-1 获取DOM元素
1-2 定义剩余的抽奖次数
2: 点击抽奖按钮,实现滚动抽奖效果(复杂度高)
2-1 获取点击按钮 ,绑定点击事件
2-2 为每一个list选项添加类名,实现高亮状态
2-3 定义当前高亮的列表项索引值
2-4 使用定时器实现滚动效果
2-5 使用随机数定义停止条件
3: 弹窗处理
3-1 打开弹窗.显示中奖信息(处理未中奖时的弹窗提示内容)
3-2 打开弹窗的同时,减少剩余的抽奖次数
3-3 关闭按钮的事件绑定
3-4 再来一次按钮事件绑定
4: 定义runGame函数
5: timer定时器bug修复
*/
(function(){
// 开始抽奖按钮
var startBtn = document.querySelector('.hander-container-btn')
// 奖项
var priceList =document.querySelectorAll('.price-list')
// 弹窗区域
var dialogContainer = document.querySelector('.dialog-container')
// 弹窗内容区域
var priceContent = document.querySelector('.dialog-container .content')
var index = -1
var timer = null
var number = 5
var currentIndex = null
// 弹窗关闭按钮
var closeBtn = document.querySelector('.close')
// 弹窗按钮再来一次
var confirmBtn = document.querySelector('.dialog-main-footer .button')
// 剩余抽奖次数
var priceNumber = document.querySelector('.price-number')
// 初始化
var init = function(){
priceNumber.innerHTML = number
initEvent()
}
// 初始化函数
var initEvent = function(){
// 添加监听事件
startBtn.addEventListener('click',onStartBtnClick)
closeBtn.addEventListener('click',onCloseBtnClick)
confirmBtn.addEventListener('click',onConfirmBtnClick)
}
// 开始抽奖
var onStartBtnClick = function(){
if(timer) return
index = -1
if(number === 0) return
runGame()
}
// 转盘转动
var runGame = function(){
// 获取随机数
var ranDom = Math.floor(Math.random() * 6000 + 3000)
timer = setInterval(function(){
ranDom -= 200
if(ranDom < 200){
// 小于200停止转盘
clearInterval(timer)
timer = null
// 打开弹窗
openDialog()
return
}
currentIndex = ++index % priceList.length
priceList.forEach(function(el){
// 先移除背景滚动效果
el.classList.remove('active')
})
// 给滚动项加上背景高亮背景
priceList[currentIndex].classList.add('active')
},60)
}
// 打开弹窗
var openDialog = function(){
priceNumber.innerHTML = --number
if(number === 0){
document.querySelector('.dialog-main-footer .button').innerHTML = '确定'
}
dialogContainer.style.display = 'block'
var spanText = document.querySelector('.active span').innerHTML
if(spanText.includes('谢谢参与')){
priceContent.innerHTML = '很遗憾您没有中奖'
}else {
priceContent.innerHTML = '恭喜您获得' + spanText
}
}
// 关闭弹窗
var onCloseBtnClick = function(){
dialogContainer.style.display = 'none'
}
// 弹窗确认按钮 && 再来一次
var onConfirmBtnClick = function(){
index = -1
dialogContainer.style.display = 'none'
if(number === 0 || timer) return
runGame()
}
init()
})()
结尾总结
点击抽奖按钮之后,里面涉及的功能有些许复杂,需要一步步捋下来,遇到问题就一步步解决,给一个参考图