vue抽奖系统
html部分
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
<title>可能我见了黄河才会死心吧</title>
<link rel="stylesheet" href="css/lottery.css" />
<script src="js/vue.min.js"></script>
</head>
<body>
<div class="lottery-box" id="app">
<h1 class="title">可能我撞了南墙才会回头吧</h1>
<div class="lottery">
<div class="lottery-item">
<div class="lottery-start">
<div class="box gray" v-if="isStart===0">
<p>活动未开始</p>
</div>
<div class="box" @click="startLottery" v-if="isStart===1">
<p><b>抽奖</b></p>
<p>{{score}}RMB一次</p>
</div>
<div class="box gray" v-if="isStart===2">
<p>活动已过期</p>
</div>
</div>
<ul>
<li v-for="(item,i) in list" :class="i==index?'on':''">
<div class="box">
<p><img :src="item.img" alt=""></p>
<p>{{item.title}}</p>
</div>
</li>
</ul>
</div>
</div>
<div style="font-size: 14px;color: #FFFFFF;text-align: center;font-weight: 600;margin-top: 60px;">本着公平公正的原则,在此公布全部概率:</br>我就是个非洲土著人:33%</br>非洲穿自制凉鞋的土著人:13%</br>非洲有单车的土著人:13%</br>即将入非的欧洲人:15%</br>欧洲人:13%</br>圣骑士:7%</br>
<span style="color: cornsilk;">欧洲教皇:4.9999%</span>
</br>
<span style="color: #D92B2F;">快去买彩票吧:0.0001%</span>
</div>
<!-- 中奖弹窗 -->
<div class="mask" v-if="showToast"></div>
<div class="lottery-alert" v-if="showToast">
<h1>恭喜您</h1>
<p><img src="img/j2.png" alt=""></p>
<h2>获得{{list[index].title}}</h2>
<div class="btnsave" @click="showToast=false">确定</div>
</div>
</div>
<script src="js/lottery.js"></script>
</body>
</html>
css
* {
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
body {
background-image: linear-gradient(to top, #a8edea 0%, #fed6e3 100%);
font-size: 14px;
}
img {
border: 0px;
}
ul, li {
list-style-type: none;
}
.lottery-box {
overflow: hidden;
}
.lottery-box .title {
text-align: center;
padding: 50px 0;
font-size: 18px;
color: #fff;
}
.lottery {
animation: changeBg .5s ease infinite;
overflow: hidden;
padding: 20px;
width: 400px;
margin: 0 auto;
background-repeat: no-repeat;
background-size: 100% 100%;
}
@keyframes changeBg {
0% {
background-image:url(../img/k1.png);
}
100% {
background-image:url(../img/k2.png);
}
}
.lottery .lottery-item {
height: 340px;
position: relative;
margin-top: 10px;
margin-left: 10px;
}
.lottery .lottery-item ul li {
width: 33.33333333%;
position: absolute;
padding-right: 10px;
}
.lottery .lottery-item ul li:nth-child(2) {
left: 33.33333333%;
}
.lottery .lottery-item ul li:nth-child(3) {
left: 66.66666666%;
}
.lottery .lottery-item ul li:nth-child(4) {
left: 66.66666666%;
top: 110px;
}
.lottery .lottery-item ul li:nth-child(5) {
left: 66.66666666%;
top: 220px;
}
.lottery .lottery-item ul li:nth-child(6) {
left: 33.33333333%;
top: 220px;
}
.lottery .lottery-item ul li:nth-child(7) {
left: 0;
top: 220px;
}
.lottery .lottery-item ul li:nth-child(8) {
left: 0;
top: 110px;
}
.lottery .lottery-item ul li .box {
height: 100px;
position: relative;
text-align: center;
overflow: hidden;
background: url(../img/bg2.png) no-repeat center;
background-size: 100% 100%;
}
.lottery .lottery-item ul li .box img {
display: block;
height: 50px;
margin: 0 auto;
margin-top: 10px;
margin-bottom: 5px;
}
.lottery .lottery-item ul li .box p {
color: #708ABF;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
font-size: 14px;
}
.lottery .lottery-item ul li.on .box {
background: url(../img/bg1.png) no-repeat center;
background-size: 100% 100%;
}
.lottery .lottery-item ul li.on .box p {
color: #fff;
}
.lottery .lottery-item .lottery-start {
position: absolute;
left: 33.33333333%;
width: 33.33333333%;
top: 110px;
padding-right: 10px;
}
.lottery .lottery-item .lottery-start .box {
height: 100px;
font-size: 14px;
color: #fff;
cursor: pointer;
text-align: center;
overflow: hidden;
background: url(../img/bg1.png) no-repeat center;
background-size: 100% 100%;
}
.lottery .lottery-item .lottery-start .box p b {
font-size: 40px;
margin-top: 16px;
margin-bottom: 15px;
line-height: 30px;
display: block;
}
.lottery .lottery-item .lottery-start .box:active {
opacity: 0.7;
}
.lottery .lottery-item .lottery-start .box.gray {
background: url(../img/bg3.png) no-repeat center;
background-size: 100% 100%;
}
.lottery .lottery-item .lottery-start .box.gray p {
color: #708ABF;
font-weight: bold;
}
.mask {
width: 100%;
height: 100%;
background: rgba(0,0,0,0.7);
position: fixed;
overflow: hidden;
z-index: 222;
top: 0;
left: 0;
}
.lottery-alert {
max-width: 400px;
text-align: center;
z-index: 10000;
border-radius: 10px;
background: #fff;
padding: 20px;
position: fixed;
left: 0;
right: 0;
margin: auto;
top: 50%;
transform: translateY(-50%);
}
.lottery-alert h1 {
font-size: 18px;
font-weight: bold;
color: #D92B2F;
}
.lottery-alert img {
display: block;
height: 120px;
margin: 0 auto;
}
.lottery-alert h2 {
font-weight: normal;
color: #D92B2F;
font-size: 15px;
padding-top: 15px;
}
.lottery-alert p {
color: #666;
font-size: 16px;
padding-top: 5px;
}
.lottery-alert .btnsave {
border-radius: 3px;
box-shadow: none;
height: 40px;
cursor: pointer;
line-height: 40px;
color: #fff;
margin-top: 12px;
background: linear-gradient(180deg, rgba(213,60,63,1) 0%, rgba(201,20,24,1) 100%);
font-size: 16px;
}
js
new Vue({
el:"#app",
data:{
isStart:1,
score:1,
list:[
{img:'img/j2.png',title:'欧洲人'},
{img:'img/j1.png',title:'非洲有单车的土著人'},
{img:'img/j1.png',title:'欧洲教皇'},
{img:'img/j2.png',title:'非洲穿自制凉鞋的土著人'},
{img:'img/j2.png',title:'圣骑士'},
{img:'img/j1.png',title:'即将入非的欧洲人'},
{img:'img/j2.png',title:'我就是个非洲土著人'},
{img:'img/j1.png',title:'快去买彩票吧'},
],
index: -1,
count: 8,
timer: 0,
speed: 200,
times: 0,
cycle: 50,
prize: -1,
click: true,
showToast: false,
},
mounted(){},
methods:{
startLottery(){
if (!this.click) { return }
this.startRoll();
},
startRoll () {
this.times += 1
this.oneRoll()
if (this.times > this.cycle + 10 && this.prize === this.index) {
clearTimeout(this.timer)
this.prize = -1
this.times = 0
this.speed = 200
this.click = true;
var that = this;
setTimeout(res=>{
that.showToast = true;
},500)
} else {
if (this.times < this.cycle) {
this.speed -= 10
} else if (this.times === this.cycle) {
const index = parseInt(Math.random() * 100, 0) || 0;
this.prize = index;
if(this.prize > 98) {this.prize = 7}
else if(this.prize > 94) {this.prize = 2}
else if(this.prize > 87) {this.prize = 4}
else if(this.prize > 74) {this.prize = 0}
else if(this.prize > 59) {this.prize = 5}
else if(this.prize > 46) {this.prize = 1}
else if(this.prize > 33) {this.prize = 3}
else if(this.prize >= 0) {this.prize = 6}
} else if (this.times > this.cycle + 10 && ((this.prize === 0 && this.index === 7) || this.prize === this.index + 1)) {
this.speed += 110
} else {
this.speed += 20
}
if (this.speed < 40) {this.speed = 40}
this.timer = setTimeout(this.startRoll, this.speed)
}
},
oneRoll () {
let index = this.index
const count = this.count
index += 1
if (index > count - 1) { index = 0 }
this.index = index
},
}
})