【黃啊碼】小程式:九宮格抽獎如何實現?可控制抽獎率
黃啊碼向來簡單粗暴,來,程式碼伺候
js程式碼如下:
//index.js //獲取應用實例 const app = getApp() //計數器 var interval = null; //值越大旋轉時間越長 即旋轉速度 var intime = 50; Page({ data: { color: [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5], //9張獎品圖片 images: [], btnconfirm: '/images/dianjichoujiang.png', clickLuck:'clickLuck', luckPosition:2, prizeWeight:[], titles:[], }, onLoad:function(){ var that=this; wx.request({ url: 'XXXXXXX', method:'POST', header: { 'content-type': 'application/json' // 默認值 }, data:{ },success(res){ that.setData({prizeWeight:res.data.data.percent_list});//獎項權重數組,表徵各獎項的中獎機會佔總數的百分比。比如一等獎的中獎率是1%,二等獎的中獎率是5% that.setData({images:res.data.data.images_list}); that.setData({titles:res.data.data.title_list}); //例子:prizeWeight:({5,5,10,20,30,10,10,10})記住是按順序的,所有的加起來等於100,images就是八張圖片的地址,titles就是八個標題的內容 } }) }, //點擊抽獎按鈕 clickLuck:function(){ var e = this; var weightSum = e.data.prizeWeight.reduce(function(prev, currVal){ //計算權重之和:1+5+20+74=100 return prev + currVal; //prev 是前一次累加後的數值,currVal 是本次待加的數值 }, 0); console.log(weightSum); var random = Math.random()*weightSum; console.log(random); var concatWeightArr = e.data.prizeWeight.concat(random); //將隨機數加入權重數組 console.log(concatWeightArr); var sortedWeightArr = concatWeightArr.sort(function(a, b){return a-b;}); //將包含隨機數的新權重數組按從小到大(升序)排序 console.log(sortedWeightArr); var randomIndex = sortedWeightArr.indexOf(random); //索引隨機數在新權重數組中的位置 console.log(randomIndex); randomIndex = Math.min(randomIndex, e.data.images.length -1); //權重隨機數的下標不得超過獎項數組的長度-1,重新計算隨機數在獎項數組中的索引位置 console.log(randomIndex); e.setData({luckPosition:randomIndex}); //設置按鈕不可點擊 e.setData({ btnconfirm:'/images/dianjichoujiangd.png', clickLuck:'', }) //清空計時器 clearInterval(interval); var index = 0; console.log(e.data.color[0]); //循環設置每一項的透明度 interval = setInterval(function () { if (index > 7) { index = 0; e.data.color[7] = 0.5 } else if (index != 0) { e.data.color[index - 1] = 0.5 } e.data.color[index] = 1 e.setData({ color: e.data.color, }) index++; }, intime); //模擬網路請求時間 設為兩秒 var stoptime = 2000; setTimeout(function () { e.stop(e.data.luckPosition); }, stoptime) }, stop: function (which){ var e = this; //清空計數器 clearInterval(interval); //初始化當前位置 var current = -1; var color = e.data.color; for (var i = 0; i < color.length; i++) { if (color[i] == 1) { current = i; } } //下標從1開始 var index = current + 1; e.stopLuck(which, index, intime, 10); }, /** * which:中獎位置 * index:當前位置 * time:時間標記 * splittime:每次增加的時間 值越大減速越快 */ stopLuck: function (which, index,time,splittime){ var e = this; //值越大出現中獎結果後減速時間越長 var color = e.data.color; setTimeout(function () { //重置前一個位置 if (index > 7) { index = 0; color[7] = 0.5 } else if (index != 0) { color[index - 1] = 0.5 } //當前位置為選中狀態 color[index] = 1 e.setData({ color: color, }) //如果旋轉時間過短或者當前位置不等於中獎位置則遞歸執行 //直到旋轉至中獎位置 if (time < 400 || index != which){ //越來越慢 splittime++; time += splittime; //當前位置+1 index++; e.stopLuck(which, index, time, splittime); }else{ //1秒後顯示彈窗 setTimeout(function () { wx.showModal({ title: '提示', content: e.data.titles[which], showCancel: false, success: function (res) { if (res.confirm) { //設置按鈕可以點擊 e.setData({ btnconfirm: '/images/dianjichoujiang.png', clickLuck: 'clickLuck', }) } } }) }, 1000); } }, time); console.log(time); } })
前端:
<!--index.wxml--> <view class="container"> <view class='frame_view'> <view class='frame_row'> <image class='frame_item' style='opacity:{{color[0]}}' src='{{images[0]}}'></image> <image class='frame_item' style='opacity:{{color[1]}}' src='{{images[1]}}'></image> <image class='frame_item' style='opacity:{{color[2]}}' src='{{images[2]}}'></image> </view> <view class='frame_row'> <image class='frame_item' style='opacity:{{color[7]}}' src='{{images[7]}}'></image> <image class='frame_item' src='{{btnconfirm}}' bindtap='{{clickLuck}}'></image> <image class='frame_item' style='opacity:{{color[3]}}' src='{{images[3]}}'></image> </view> <view class='frame_row'> <image class='frame_item' style='opacity:{{color[6]}}' src='{{images[6]}}'></image> <image class='frame_item' style='opacity:{{color[5]}}' src='{{images[5]}}'></image> <image class='frame_item' style='opacity:{{color[4]}}' src='{{images[4]}}'></image> </view> </view> </view>
wxss:
/**index.wxss**/ .frame_view{ bottom: 160rpx; left: 60rpx; right: 60rpx; width:590rpx; height:590rpx; padding: 20rpx; background: #792db3; z-index: 3; display: flex; flex-direction: column; justify-content: space-between; align-items: center; border-radius: 30rpx; } .frame_row{ width:580rpx; height:180rpx; display: flex; flex-direction: row; justify-content: space-between; align-items: center; } .frame_item{ width:180rpx; height:180rpx; }
嗯,就這樣,如果還不會,掃我碼,戳我頭像,使勁戳,OK?