微信小程序轉(zhuǎn)盤(pán)抽獎(jiǎng)的實(shí)現(xiàn)方法
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)轉(zhuǎn)盤(pán)抽獎(jiǎng)的具體代碼,供大家參考,具體內(nèi)容如下
lucky-draw.wxss:
.lucky_draw_zp{ width: 502rpx; height: 502rpx; margin: 0 auto; position: relative;} .lucky_draw_zp_img , .lucky_draw_zp_btn{ width: 100%; height: 100%; position: absolute; left: 0; top: 0;}
lucky-draw.wxml:
<view class="lucky_draw_zp" bindtap="getLucky"> ?? ?<image style="{{zpRotateDeg}}" class="lucky_draw_zp_img" src="{{zpData.zpImg}}" mode="aspectFit"></image> ?? ?<image class="lucky_draw_zp_btn" src="../../../images/zp_btn.png" mode="aspectFit"></image> </view>
lucky-draw.js:
?Page({ ? ?? ?/** ?? ? * 頁(yè)面的初始數(shù)據(jù) ?? ? */ ?? ?data: { ?? ??? ?zpData:{ ?? ??? ??? ?//轉(zhuǎn)盤(pán)主圖圖片url ?? ??? ??? ?zpImg:'../../../images/zp.png', ?? ??? ??? ? ?? ??? ??? ?equalParts:null, //一共多少等份 ?? ??? ??? ?oneAngle:null, //每一等份多少度 ?? ??? ??? ? ?? ??? ??? ?// ******** 轉(zhuǎn)盤(pán)獎(jiǎng)品數(shù)據(jù):后臺(tái)獲取數(shù)據(jù) ******** ?? ??? ??? ?//注:根據(jù)轉(zhuǎn)盤(pán)圖片對(duì)應(yīng)的值(轉(zhuǎn)盤(pán)圖片指針處順時(shí)針向右數(shù),起始1,獎(jiǎng)品對(duì)應(yīng)的格子數(shù)【第幾等份上】) ?? ??? ??? ?awardSetting:[ ?? ??? ??? ??? ?'第1個(gè)格子對(duì)應(yīng)內(nèi)容', ?? ??? ??? ??? ?'第2個(gè)格子對(duì)應(yīng)內(nèi)容', ?? ??? ??? ??? ?'第3個(gè)格子對(duì)應(yīng)內(nèi)容', ?? ??? ??? ??? ?'第4個(gè)格子對(duì)應(yīng)內(nèi)容', ?? ??? ??? ??? ?'第5個(gè)格子對(duì)應(yīng)內(nèi)容', ?? ??? ??? ??? ?'第6個(gè)格子對(duì)應(yīng)內(nèi)容', ?? ??? ??? ?], ?? ??? ?}, ?? ??? ? ?? ??? ?ifRoate:false, //轉(zhuǎn)盤(pán)是否在轉(zhuǎn)動(dòng)(判斷阻止多次點(diǎn)擊) ?? ??? ?zpRotateDeg:'', //旋轉(zhuǎn)角度 ?? ??? ? ?? ??? ?// ******** 抽獎(jiǎng)結(jié)果數(shù)據(jù):后臺(tái)獲取數(shù)據(jù) ******** ?? ??? ?curKey:null, //抽獎(jiǎng)結(jié)果 : 取值范圍 1 至 總格子數(shù) ?? ??? ?ifWinning:null, //是否中獎(jiǎng) ?? ?}, ? ? ? ? ?? ?// 獲取轉(zhuǎn)盤(pán)初始數(shù)據(jù) ?? ?getZpData(){ ?? ??? ?let zpImg = 'zpData.zpImg'; ?? ??? ?let awardSetting = 'zpData.awardSetting'; ?? ??? ?this.setData({ ?? ??? ??? ?[zpImg]:'../../../images/zp.png', ?? ??? ??? ?[awardSetting]:[ ?? ??? ??? ??? ?'1', ?? ??? ??? ??? ?'2', ?? ??? ??? ??? ?'3', ?? ??? ??? ??? ?'4', ?? ??? ??? ??? ?'5', ?? ??? ??? ??? ?'6', ?? ??? ??? ?], ?? ??? ?}); ?? ??? ? ?? ??? ?this.setZpDefault(); ?? ?}, ?? ?// 根據(jù)轉(zhuǎn)盤(pán)初始數(shù)據(jù)設(shè)置轉(zhuǎn)盤(pán)初始關(guān)鍵參數(shù) ?? ?setZpDefault(){ ?? ??? ?let equalPartsNum = this.data.zpData.awardSetting.length; ?? ??? ?let oneAngleNum = 360 / equalPartsNum; ?? ??? ?let equalParts = 'zpData.equalParts'; ?? ??? ?let oneAngle = 'zpData.oneAngle'; ?? ??? ?this.setData({ ?? ??? ??? ?//一共多少等份 ?? ??? ??? ?[equalParts] : equalPartsNum, ?? ??? ??? ?// 根據(jù)轉(zhuǎn)盤(pán)得等份數(shù)設(shè)置 每一等份多少度 ?? ??? ??? ?[oneAngle] : oneAngleNum, ?? ??? ?}); ?? ?}, ? ? ?? ?// 設(shè)置旋轉(zhuǎn)動(dòng)效 ?? ?setToRotate(degNum){ ?? ??? ?this.setData({ ?? ??? ??? ?zpRotateDeg : '-webkit-transform: rotate(' + degNum + 'deg);transform: rotate(' + degNum + 'deg);-webkit-transition: all 5s ease;transition: all 5s ease;', ?? ??? ?}); ?? ?}, ?? ?//根據(jù) 設(shè)置的 指針停止時(shí)指向的格子(中獎(jiǎng)結(jié)果),設(shè)置其旋轉(zhuǎn)角度區(qū)間 ?? ?setRotate(awardSettingNumber){ //awardSettingNumber ?取值范圍 1 至 總格子數(shù) ?? ??? ?setTimeout(() => { ?? ??? ??? ?//轉(zhuǎn)盤(pán)停止時(shí) 指針 指向的格子 最小角度 ?? ??? ??? ?let minAngle = 360 - awardSettingNumber * this.data.zpData.oneAngle + 5; ?? ??? ??? ?//轉(zhuǎn)盤(pán)停止時(shí) 指針 指向的格子 最大角度 ?? ??? ??? ?let maxAngle = 360 - (awardSettingNumber - 1) * this.data.zpData.oneAngle - 5; ?? ??? ??? ?//旋轉(zhuǎn)區(qū)間 ?? ??? ??? ?let newAngle = Math.floor(minAngle + Math.random() * (maxAngle - minAngle)) + 360 * 15; ?? ??? ??? ? ?? ??? ??? ?this.setToRotate(newAngle); ?? ??? ??? ? ?? ??? ??? ?setTimeout(() => { ?? ??? ??? ??? ?this.roateEnd(awardSettingNumber); ?? ??? ??? ?}, 5150); ?? ??? ?},50); ?? ?}, ?? ?//旋轉(zhuǎn)結(jié)束執(zhí)行 ?? ?roateEnd(awardSettingNumber){ ?? ??? ?console.log('當(dāng)前指向格子數(shù) -- ' + awardSettingNumber , this.data.curKey); ?? ??? ?console.log('當(dāng)前指向格子數(shù)對(duì)應(yīng)內(nèi)容 -- ' + this.data.zpData.awardSetting[awardSettingNumber - 1] , this.data.zpData.awardSetting[this.data.curKey - 1]); ?? ??? ? ?? ??? ?// 是否中獎(jiǎng) ?? ??? ?if(this.data.ifWinning){ ?? ??? ??? ?console.log('中獎(jiǎng)'); ?? ??? ?}else{ ?? ??? ??? ?console.log('未中獎(jiǎng)'); ?? ??? ?} ?? ??? ? ?? ??? ?setTimeout(() => { ?? ??? ??? ?this.setData({ ?? ??? ??? ??? ?ifRoate : false, //轉(zhuǎn)盤(pán)是否在轉(zhuǎn)動(dòng) ?? ??? ??? ?}); ?? ??? ?}, 100); ?? ?}, ? ? ?? ?//點(diǎn)擊抽獎(jiǎng) ?? ?getLucky(){ ?? ??? ?if(this.data.ifRoate){ ?? ??? ??? ?return false; ?? ??? ?} ?? ??? ? ?? ??? ?this.setData({ ?? ??? ??? ?ifRoate : true, //轉(zhuǎn)盤(pán)是否在轉(zhuǎn)動(dòng) ?? ??? ??? ?zpRotateDeg : '' ?? ??? ?}); ?? ??? ??? ??? ? ?? ??? ?// 請(qǐng)求后臺(tái)獲取抽獎(jiǎng)結(jié)果中... ?? ??? ? ?? ??? ?/*test*/ ?? ??? ?var res = { ?? ??? ??? ?status:1, ?? ??? ??? ?curKey:Math.floor(1 + Math.random() * this.data.zpData.equalParts), //抽獎(jiǎng)結(jié)果 : 取值范圍 1 至 總格子數(shù) ?? ??? ??? ?ifWinning:true, //是否中獎(jiǎng) ?? ??? ??? ?info:'沒(méi)機(jī)會(huì)或什么什么' ?? ??? ?} ?? ??? ?/*test*/ ?? ??? ? ?? ??? ?/ ?? ??? ??? ?if(res.status == 1){ ?? ??? ??? ??? ?this.setData({ ?? ??? ??? ??? ??? ?curKey : res.curKey, ?? ??? ??? ??? ??? ?ifWinning : res.ifWinning ?? ??? ??? ??? ?}); ?? ??? ??? ??? ? ?? ??? ??? ??? ?this.setRotate(this.data.curKey); ?? ??? ??? ?}else{ ?? ??? ??? ??? ?this.setData({ ?? ??? ??? ??? ??? ?ifRoate : false, //轉(zhuǎn)盤(pán)是否在轉(zhuǎn)動(dòng) ?? ??? ??? ??? ?}); ?? ??? ??? ??? ?wx.showModal({ ?? ??? ??? ??? ??? ?title: '溫馨提示', ?? ??? ??? ??? ??? ?showCancel:false, ?? ??? ??? ??? ??? ?content: res.info, ?? ??? ??? ??? ?}) ?? ??? ??? ?} ?? ??? ?/ ?? ?}, ? ? ?? ?/** ?? ? * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載 ?? ? */ ?? ?onLoad: function (options) { ? ?? ?}, ? ?? ?/** ?? ? * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面初次渲染完成 ?? ? */ ?? ?onReady: function () { ? ?? ?}, ? ?? ?/** ?? ? * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面顯示 ?? ? */ ?? ?onShow: function () { ?? ??? ?this.getZpData(); ?? ?}, ? ?? ?/** ?? ? * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面隱藏 ?? ? */ ?? ?onHide: function () { ? ?? ?}, ? ?? ?/** ?? ? * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面卸載 ?? ? */ ?? ?onUnload: function () { ? ?? ?}, ? ?? ?/** ?? ? * 頁(yè)面相關(guān)事件處理函數(shù)--監(jiān)聽(tīng)用戶下拉動(dòng)作 ?? ? */ ?? ?onPullDownRefresh: function () { ? ?? ?}, ? ?? ?/** ?? ? * 頁(yè)面上拉觸底事件的處理函數(shù) ?? ? */ ?? ?onReachBottom: function () { ? ?? ?}, ? ?? ?/** ?? ? * 用戶點(diǎn)擊右上角分享 ?? ? */ ?? ?onShareAppMessage: function () { ? ?? ?} })
圖片資源:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序?qū)崿F(xiàn)轉(zhuǎn)盤(pán)抽獎(jiǎng)
- 微信小程序開(kāi)發(fā)之大轉(zhuǎn)盤(pá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)畫(huà)
- 微信小程序 扭蛋抽獎(jiǎng)機(jī)css3動(dòng)畫(huà)實(shí)現(xiàn)詳解
- 微信小程序?qū)崿F(xiàn)九宮格抽獎(jiǎng)
- 微信小程序?qū)崿F(xiàn)多宮格抽獎(jiǎng)活動(dòng)
- 微信小程序 搖一搖抽獎(jiǎng)簡(jiǎn)單實(shí)例實(shí)現(xiàn)代碼
相關(guān)文章
js數(shù)組反轉(zhuǎn)的幾種常見(jiàn)方法舉例
最近學(xué)到了數(shù)組的使用方法,給大家分享一下,這篇文章主要給大家介紹了關(guān)于js數(shù)組反轉(zhuǎn)的幾種常見(jiàn)方法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10基于JS實(shí)現(xiàn)二維碼圖片固定在右下角某處并跟隨滾動(dòng)條滾動(dòng)
這篇文章主要介紹了基于JS實(shí)現(xiàn)二維碼圖片固定在右下角某處并跟隨滾動(dòng)條滾動(dòng),代碼簡(jiǎn)單易懂非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02JavaScript實(shí)現(xiàn)長(zhǎng)圖滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)長(zhǎng)圖滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04js的math中缺少的數(shù)學(xué)方法小結(jié)
JavaScript?Math對(duì)象包含一些真正有用且強(qiáng)大的數(shù)學(xué)運(yùn)算,但它缺乏大多數(shù)其他語(yǔ)言提供的許多重要運(yùn)算,例如求和,乘積,奇數(shù)和偶數(shù)等等,本文就來(lái)介紹一下2023-08-08JS sort方法基于數(shù)組對(duì)象屬性值排序
這篇文章主要介紹了JS sort方法基于數(shù)組對(duì)象屬性值排序,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07