微信小程序实现走马灯式抽奖_第1页
微信小程序实现走马灯式抽奖_第2页
微信小程序实现走马灯式抽奖_第3页
微信小程序实现走马灯式抽奖_第4页
微信小程序实现走马灯式抽奖_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第微信小程序实现走马灯式抽奖本文实例为大家分享了微信小程序实现走马灯式抽奖的具体代码,供大家参考,具体内容如下

先来看下效果

设置奖项

awardList是从后台拿到的奖项数组,list不够八位时填充谢谢参与奖项,超过八位时截取数组,然后随机打乱数组,保证奖项随机布局,第四位固定填充立即抽奖按钮

//设置奖项

settingAward(awardList){

constlen=awardList.length;

constaward={

awardName:'谢谢参与',

awardMoney:0,

awardType:'00',

awardCode:''

};

let_awardList=[];

if(len8){

for(leti=0;i8-len;i++){

awardList.push(JSON.parse(JSON.stringify(award)));

}

this.randArr(awardList);

_awardList=awardList;

console.log(_awardList)

}elseif(awardList.length==8)_awardList=awardList;

else{

_awardList=awardList.splice(0,9);

}

_awardList.splice(4,0,{

awardName:'立即抽奖'

})

return_awardList;

//随机打乱奖项

randArr(arr){

for(vari=0;iarr.length;i++){

variRand=parseInt(arr.length*Math.random());

vartemp=arr[i];

arr[i]=arr[iRand];

arr[iRand]=temp;

}

returnarr;

}

布局

主要用了flex布局,遍历奖品list,index==4时渲染立即抽奖按钮,否则渲染奖项

view

viewwx:for="{{awardList}}"

viewwx:if="{{index==4}}"

viewwx:if="{{activityCount0}}"class="btn{{lucking'lucking':'lucked'}}"

textcatchtap="startLuck"{{item.awardName}}/text

/view

viewwx:else

text{{item.awardName}}/text

/view

/view

viewwx:elseclass="award{{currentIndex==index'selected':'unselected'}}"

blockwx:if="{{item.awardType=='00'}}"

view

imagesrc="../../img/noluck_icon.png"/image

/view

view{{item.awardName}}/view

/block

blockwx:elif="{{item.awardType=='07'}}"

view

imagesrc="../../img/mianxi_icon.png"/image

/view

view{{item.awardName}}/view

/block

blockwx:else

view

imagesrc="../../img/turntable_redpacket.png"/image

text{{util.formatMoney(item.awardMoney)}}/text

/view

view{{item.awardName}}/view

/block

/view

/view

/view

抽奖逻辑

开始抽奖时默认选中第一个,初始化idArr为currentIndex的索引,即下一个奖项在哪激活

记录圈数letcycles=0;

开始设置interval=setInterval(frame,100);

index==8时轮询了一圈,cycles加一

当cycles2时减速定时器interval=setInterval(frame,300);

当抽奖接口有结果且转了三圈后跳到获奖位置,清除定时器并弹出获奖结果弹窗

//开始抽奖

startLuck(){

constidArr=[0,1,2,5,8,7,6,3];

letcycles=0;

letthat=this;

let_awardList=this.data.awardList;

letindex=this.data.currentIndex;

letactivityCount=this.data.activityCount-1;

varinterval=setInterval(frame,100);

this.setData({

lucking:true,

activityCount

})

letpending=true;

post('122201.app',{

duration:2000,

activityCode:this.data.activityCode

},{

isMarket:true

}).then(res={

pending=false;

this.setData({

awardResult:{

awardCode:"",

...res

}

})

}).catch(err={

clearInterval(interval);

pending=false;

activityCount+=1;

this.setData({

activityCount,

lucking:false,

})

})

functionframe(){

if(!pending){

//转三圈后跳到获奖位置

if(cycles3){

if(_awardList[that.data.currentIndex].awardCode==that.data.awardResult.awardCode){

clearInterval(interval);

that.setData({

lucking:false,

showModal:true

})

return;

}

}

}

if(index==8){

index=0;

if(!pending){

//两圈后转盘减速

if(cycles++1){

clearInterval(interval);

interval=setInterval(frame,300);

}

}

}

//设置奖项跳到对应位置

that.setData({

currentIndex:idArr[index++]

})

}

},

wxss

.turntable.content{

width:568rpx;

height:568rpx;

background:#F48002;

border-radius:20px;

position:absolute;

top:90rpx;

left:30rpx;

display:flex;

flex-wrap:wrap;

justify-content:space-around;

align-items:center;

padding:10rpx;

box-sizing:border-box;

.turntable.cont

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论