微信小程序?qū)崿F(xiàn)多宮格抽獎(jiǎng)活動(dòng)
最近閑來(lái)無(wú)事,做了一個(gè)多宮格抽獎(jiǎng)的例子,有什么需要改進(jìn)或者錯(cuò)誤的地方,請(qǐng)留言,謝謝
首先看效果:

思路是先讓其轉(zhuǎn)動(dòng)2圈多,然后再進(jìn)行抽獎(jiǎng),格子運(yùn)動(dòng)用的是setTimeout,讓其運(yùn)行的時(shí)間間隔不一樣,然后產(chǎn)生運(yùn)動(dòng)快慢的效果
好了,上代碼
首先是WXML(這里面的判斷可能有些繞,仔細(xì)按順序看,因其第五個(gè)和第十一個(gè)格子在不同手機(jī)屏幕大小上的顯示有問(wèn)題,所以再次給它們判斷了一下)
<view class="box">
<view class="boxsub {{luckynum==index?'luck':''}}" wx:for='{{box}}' style="{{index>0&&index<4?'top:0;left:'+index*140+'rpx;':(index>3&&index<8?'right:0;top:'+((index-4)*100)+'rpx;':(index>7&&index<12?'bottom:0;right:'+(index-7)*140+'rpx;':(index>11&&index<14?'left:0;bottom:'+(index-11)*100+'rpx;':'')))}} {{index=='4'?'top:0;':''}} {{index=='11'?'left:0;':''}} " wx:key=''>
<text class='boxcontent' style='{{item.name.length>6?"line-height:40rpx;margin-top:10rpx;":"line-height:100rpx;"}}'>{{item.name}}</text>
</view>
<view class="lucky" catchtap="luckyTap">
<text class="taplucky">點(diǎn)擊抽獎(jiǎng)</text>
<text class="howMany">您還有<text class="howMany_num" >{{howManyNum}}</text>次抽獎(jiǎng)機(jī)會(huì)</text>
</view>
</view>
<view class="explain">
</view>
WXSS:
.box{
margin:20rpx 25rpx;
height: 400rpx;
width: 698rpx;
/*border:1px solid #ddd;*/
position: relative;
/*box-sizing: border-box;*/
}
.boxsub{
width: 140rpx;
height: 100rpx;
/*border: 1px solid #f00;*/
box-sizing: border-box;
position: absolute;
background: #ff6100;
border: 1rpx solid #fff;
}
.boxcontent{
text-align: center;
font-size: 26rpx;
display: block;
color: #fff;
}
.lucky{
width: 300rpx;
height:130rpx;
background:#ff6100;/* #ff6100;007FFF*/
position: absolute;
left: 0;
bottom: 0;
right: 0;
top: 0rpx;
margin: auto;
}
.lucky:active{
opacity: 0.7;
}
.taplucky{
display: block;
text-align: center;
font-size: 30rpx;
line-height: 50rpx;
height: 50rpx;
color: #fff;
margin-top: 20rpx;
}
.howMany{
display: block;
text-align: center;
font-size: 26rpx;
color: #fff;
line-height: 40rpx;
height: 40rpx;
}
.howMany_num{
color:#007FFF;
font-size:32rpx;
line-height:40rpx;
padding:0 10rpx;
}
.luck{
opacity: 0.5;
background: #ff6100;
}
JS
var time = null;//setTimeout的ID,用clearTimeout清除
Page({
data: {
box: [{
name:'100積分'
}, {
name: '10元優(yōu)惠券\n滿100可用'
}, {
name: '60積分'
}, {
name: '30積分'
}, {
name: '50積分'
}, {
name: '30元優(yōu)惠券\n滿120可用'
}, {
name: '100積分'
}, {
name: '200積分'
}, {
name: '10積分'
}, {
name: '50積分'
}, {
name: '40積分'
}, {
name: '50優(yōu)惠券滿500可用'
}, {
name: '60積分'
}, {
name: '70積分'
}],
luckynum:0,//當(dāng)前運(yùn)動(dòng)到的位置,在界面渲染
howManyNum:10,//抽獎(jiǎng)的剩余次數(shù)
content:{
index: 0, //當(dāng)前轉(zhuǎn)動(dòng)到哪個(gè)位置,起點(diǎn)位置
count: 0, //總共有多少個(gè)位置
speed: 50, //初始轉(zhuǎn)動(dòng)速度
cycle: 3*14, //轉(zhuǎn)動(dòng)基本次數(shù):即至少需要轉(zhuǎn)動(dòng)多少次再進(jìn)入抽獎(jiǎng)環(huán)節(jié),這里設(shè)置的是轉(zhuǎn)動(dòng)三次后進(jìn)入抽獎(jiǎng)環(huán)節(jié)
},
prize:0,//中獎(jiǎng)的位置
luckytapif:true//判斷現(xiàn)在是否可以點(diǎn)擊
},
//點(diǎn)擊抽獎(jiǎng)
luckyTap:function(){
var i=0,
that=this,
howManyNum = this.data.howManyNum,//剩余的抽獎(jiǎng)次數(shù)
luckytapif = this.data.luckytapif,//獲取現(xiàn)在處于的狀態(tài)
luckynum = this.data.luckynum,//當(dāng)前所在的格子
prize =Math.floor(Math.random()*14) ;//中獎(jiǎng)序號(hào),隨機(jī)生成
if (luckytapif && howManyNum>0){//當(dāng)沒(méi)有處于抽獎(jiǎng)狀態(tài)且剩余的抽獎(jiǎng)次數(shù)大于零,則可以進(jìn)行抽獎(jiǎng)
console.log('prize:'+prize);
this.data.content.count=this.data.box.length;
this.setData({
howManyNum:howManyNum-1//更新抽獎(jiǎng)次數(shù)
});
this.data.luckytapif=false;//設(shè)置為抽獎(jiǎng)狀態(tài)
this.data.prize = prize;//中獎(jiǎng)的序號(hào)
this.roll();//運(yùn)行抽獎(jiǎng)函數(shù)
} else if (howManyNum == 0 && luckytapif){
wx.showModal({
title: '',
content: '您的抽獎(jiǎng)次數(shù)已經(jīng)沒(méi)有了',
showCancel:false
})
}
},
//抽獎(jiǎng)
roll:function(){
var content=this.data.content,
prize = this.data.prize,//中獎(jiǎng)序號(hào)
that=this;
if (content.cycle - (content.count-prize)>0){//最后一輪的時(shí)間進(jìn)行抽獎(jiǎng)
content.index++;
content.cycle--;
this.setData({
luckynum: content.index%14 //當(dāng)前應(yīng)該反映在界面上的位置
});
setTimeout(this.roll, content.speed);//繼續(xù)運(yùn)行抽獎(jiǎng)函數(shù)
}else{
if (content.index < (content.count*3 + prize)){//判斷是否停止
content.index++;
content.speed += (550 /14);//最后一輪的速度,勻加速,最后停下時(shí)的速度為550+50
this.data.content=content;
this.setData({
luckynum: content.index % 14
});
console.log(content.index, content.speed);//打印當(dāng)前的步數(shù)和當(dāng)前的速度
setTimeout(this.roll,content.speed);
}else{
//完成抽獎(jiǎng),初始化數(shù)據(jù)
console.log('完成');
content.index =0;
content.cycle = 3 * 14;
content.speed = 50;
this.data.luckytapif = true;
clearTimeout(time);
wx.showModal({
title: '',
content: '恭喜你抽到了'+that.data.box[prize].name,
showCancel:false
})
}
}
},
onLoad: function (options) {
},
onReady: function () {
},
onShow: function () {
},
onHide: function () {
},
onUnload: function () {
}
})
完結(jié)。
想要學(xué)習(xí)更多關(guān)于抽獎(jiǎng)功能的實(shí)現(xiàn),請(qǐng)參考此專題:抽獎(jiǎng)功能
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序?qū)崿F(xiàn)轉(zhuǎn)盤抽獎(jiǎng)
- 微信小程序開發(fā)之大轉(zhuǎn)盤 仿天貓超市抽獎(jiǎng)實(shí)例
- 微信小程序?qū)崿F(xiàn)簡(jiǎn)單九宮格抽獎(jiǎng)
- 微信小程序?qū)崿F(xiàn)走馬燈式抽獎(jiǎng)
- 微信小程序抽獎(jiǎng)組件的使用步驟
- 微信小程序?qū)崿F(xiàn)翻牌抽獎(jiǎng)動(dòng)畫
- 微信小程序 扭蛋抽獎(jiǎng)機(jī)css3動(dòng)畫實(shí)現(xiàn)詳解
- 微信小程序?qū)崿F(xiàn)九宮格抽獎(jiǎng)
- 微信小程序 搖一搖抽獎(jiǎng)簡(jiǎn)單實(shí)例實(shí)現(xiàn)代碼
- 微信小程序轉(zhuǎn)盤抽獎(jiǎng)的實(shí)現(xiàn)方法
相關(guān)文章
JavaScript代碼性能優(yōu)化總結(jié)(推薦)
下面小編就為大家?guī)?lái)一篇JavaScript代碼性能優(yōu)化總結(jié)(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧,祝大家游戲愉快哦2016-05-05
微信小程序動(dòng)態(tài)添加和刪除組件的現(xiàn)實(shí)
這篇文章主要介紹了微信小程序動(dòng)態(tài)添加和刪除組件的現(xiàn)實(shí),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
在Layui中實(shí)現(xiàn)開關(guān)按鈕的效果實(shí)例
今天小編就為大家分享一篇在Layui中實(shí)現(xiàn)開關(guān)按鈕的效果實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
JavaScript實(shí)現(xiàn)簡(jiǎn)單的拖動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)簡(jiǎn)單的拖動(dòng)效果,具有一定的參考價(jià)值,感興趣的朋友可以參考一下2016-07-07

