vue實(shí)現(xiàn)抽獎(jiǎng)效果Demo
vue抽獎(jiǎng)效果Demo
只做了簡單的抽獎(jiǎng)效果,沒有給出彈框和中獎(jiǎng)提示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="./vue.js"></script>
<link rel="stylesheet" href="./index.css" rel="external nofollow" >
<title>九宮格抽獎(jiǎng)案例(PC端)</title>
</head>
<body>
<div id="app">
<div class="lotteryBox" >
<div v-for='(item,key) in lotteryData' :key="item.id" :class="key === index?'active':''">
<img :src=item.imgUrl alt="" class="itemImg">
<p>{{item.text}}</p>
</div>
</div>
<div class="lotteryBtn" @click='lotteryBtn'>
立即抽獎(jiǎng)
</div>
</div>
</body>
</html>
<script>
var vm = new Vue({
el:"#app",
data:{
lotteryData:[
{id:0,imgUrl:"./count.png",text:"10元"},
{id:1,imgUrl:"./count.png",text:"11元"},
{id:2,imgUrl:"./count.png",text:"10元"},
{id:3,imgUrl:"./count.png",text:"13元"},
{id:4,imgUrl:"./count.png",text:"111元"},
{id:5,imgUrl:"./count.png",text:"19元"},
{id:6,imgUrl:"./count.png",text:"310元"},
{id:7,imgUrl:"./count.png",text:"14元"},
{id:8,imgUrl:"./count.png",text:"1元"},
],
index:-1, //滾動下標(biāo)
times:1, //滾動次數(shù)(每一個(gè)方格為一次)
cycle:50, //循環(huán)滾動的次數(shù)
speed:200, //轉(zhuǎn)動速度
},
methods:{
lotteryBtn:function(){
console.log('點(diǎn)擊')
this.startRoll();
this.index = -1;
this.speed = 200;
this.times = 1;
},
roll:function(){
var index = this.index;
var count = this.lotteryData.length; //總長度
index += 1;
if(index > count -1){
index = 0
}
this.index = index
},
startRoll:function(){
this.times += 1;
this.roll();
if(this.times > this.cycle + 20){
clearTimeout(timer); //清除延遲函數(shù)
this.times = 0;
}else{
if(this.times < this.cycle+20){
this.speed -=10; //加快轉(zhuǎn)動速度
}else if(this.times >= this.cycle){
this.speed += 20;
var ind = parseInt(Math.random()*8,0) || 0; // 隨機(jī)產(chǎn)生一個(gè)中獎(jiǎng)號碼
console.log(ind)
this.index = ind
}
var timer = setTimeout(this.startRoll,this.speed)
}
}
},
updated(){
}
})
</script>效果圖:

下面的代碼是我平時(shí)做項(xiàng)目的時(shí)候?qū)懙拇a,數(shù)據(jù)是從后臺獲取的,由于id的特殊性,所以中獎(jiǎng)位置我做了一些調(diào)整
data:{
times: 1 , //轉(zhuǎn)動次數(shù)
indexs: -1 , //滾動下標(biāo)
cycle:50, //基本轉(zhuǎn)動次數(shù)
speed:200, //轉(zhuǎn)動速度
}
下面是methods中的方法
startRoll:function(){ //開始滾動
var that = this,
_this = this;
if (_this.on_load == 1) return;
that.Roll();
that.times += 1; // 轉(zhuǎn)動次數(shù)
if(that.times > that.cycle + 10){
clearTimeout(this.timer); //清除定時(shí)器
that.times = 0 ;
}else{
if(that.times < this.cycle){
this.speed -=10; //加快轉(zhuǎn)動速度
}else if(that.times >= that.cycle ){
this.speed += 20;
_this.on_load = 1;
$.post("{{:url('lotteryProcessHandle')}}", function (res) {
switch (parseInt(res.code)) {
case 0:
that.prizes(res.data.id);
_this.user_info.luckydraw_times;
if (parseInt(res.data.reward_category) == 0) layerMsg(res.msg, 5, 3000, 5, '', function(){
_this.on_load = 0;
});
else layerMsg(res.msg, 1, 3000, 5, '', function(){
_this.on_load = 0;
});
break;
default:
layerMsg(res.msg, 5, 1500, 5, '', function(){
_this.on_load = 0;
});
break;
}
});
/**
* 重置參數(shù)
*/
_this.times = 1;
_this.indexs = -1;
_this.speed = 200;
}
if(that.speed <40) that.speed = 40;
this.timer = setTimeout(this.startRoll,this.speed);
}
},
Roll:function(){ //滾動函數(shù)
var _this = this;
var index = _this.indexs;
var count = _this.lotteryItem.length;
index += 1;
if(index > count -1) index = 0;
_this.indexs = index;
},
prizes:function(id){
var that = this,
datas = that.lotteryItem;
if (!isEmpty(datas)) {
for(var i=0; i< datas.length ;i++){
var ind = i;
if(datas[ind].id == id){
that.indexs = i;
break;
}
}
}
},
},
mounted(){
// this.changeType();
},
updated:function(){
this.prizes()
var _that = this
// 抽獎(jiǎng)按鈕點(diǎn)擊事件
var letts = document.getElementsByClassName('lotterydefaultBg');
for(var i =0 ; i<letts.length; i++){
var index = i
if(index === 4){
letts[i].onclick = function(){
if (_that.on_load == 1) return;
_that.startRoll();
}
}
}
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
手把手帶你安裝vue-cli并創(chuàng)建第一個(gè)vue-cli應(yīng)用程序
vue-cli這個(gè)構(gòu)建工具大大降低了webpack的使用難度,支持熱更新,有webpack-dev-server的支持,相當(dāng)于啟動了一個(gè)請求服務(wù)器,給你搭建了一個(gè)測試環(huán)境,下面這篇文章主要給大家介紹了關(guān)于安裝vue-cli并創(chuàng)建第一個(gè)vue-cli應(yīng)用程序的相關(guān)資料,需要的朋友可以參考下2022-08-08
Vue項(xiàng)目中input框focus時(shí)不調(diào)出鍵盤問題的解決
這篇文章主要介紹了Vue項(xiàng)目中input框focus時(shí)不調(diào)出鍵盤問題的解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue中實(shí)現(xiàn)代碼高亮與語法著色的方法介紹
在Vue的開發(fā)過程中,我們經(jīng)常需要展示代碼片段或者進(jìn)行代碼高亮與語法著色,Vue提供了多種方式來實(shí)現(xiàn)代碼高亮與語法著色,本文將為你詳細(xì)介紹這些方法,需要的朋友可以參考下2023-06-06
el-table實(shí)現(xiàn)搜索高亮展示并滾動到元素位置的操作代碼
這篇文章主要介紹了el-table實(shí)現(xiàn)搜索高亮展示并滾動到元素位置,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01
vue3利用customRef實(shí)現(xiàn)防抖
防抖就是對于頻繁觸發(fā)的事件添加一個(gè)延時(shí)同時(shí)設(shè)定一個(gè)最小觸發(fā)間隔,防抖大家都學(xué)過,但是如何在?Vue3?里中將防抖做到極致呢,下面小編就來和大家詳細(xì)講講2023-10-10

