js實現(xiàn)老虎機(jī)數(shù)字抽獎功能——微信小程序

效果圖

抽獎功能主要運用css動畫及js控制動畫邏輯蟆淀,整體難度不大拯啦,完成整個抽獎邏輯功能需要比較嚴(yán)謹(jǐn)?shù)倪壿嬙璺耍灰韵率菍崿F(xiàn)此功能的代碼

// lottery.wxml
// 抽獎數(shù)字
<view class="lamp-box">
    <image class="first" style="width: 100%; display: block;" src="./../../images/lottery/lukey_bg_01.png" mode="widthFix" />
</view>
<view class="prizeContent-box">
  <image class="second" style="width: 100%; display: block;" src="./../../images/lottery/lukey_bg_02.png" mode="widthFix" />
  <view class="expend-text">每抽一次消耗{{stepSingle}}步數(shù)</view>
  <view class="prizeContent">
    <view class="prizeList" wx:for="{{[1,2,3]}}" wx:key="{{index}}">
          <view class="prizeBox" style=" transform: rotateX({{index==0?animation1:index==1?animation2:animation3}}deg);  transition-duration:{{index==0?durationTime1:index==1?durationTime2:durationTime3}}s">
              <view class="red-envelope" wx:for="{{index==0?numberArr1:index==1?numberArr2:numberArr3}}" wx:for-index="i" wx:key="i" style="transform:rotateX({{36*(i)}}deg) translateZ(400rpx);padding-top:0rpx;">
                  <view class="text">
                      {{item}}
                  </view>
              </view>
          </view>
      </view>            
  </view>
</view>
// 按鈕
<view class="btn-box">
    <image class="three" style="width: 100%; display: block;" src="./../../images/lottery/lukey.png" mode="widthFix" />
    <button class="btn" type="default" bindtap='start' plain="{{true}}" hover-class="none">
        <image src="./../../images/lottery/btn.png" mode="widthFix" class="up" />
        <image src="./../../images/lottery/btn_press.png" mode="widthFix" class="press" />
        <view class="text">
            <view class="left">
                <text class="chance">剩余次數(shù)</text>
                <text class="times">{{residueDegree}}</text>
            </view>
            <view class="right">
                <button type="default" plain="true" hover-class="none">立即抽獎</button>
            </view>
        </view>
    </button>
</view>

按鈕部分主要控制點擊事件時控制兩張圖顯示隱藏達(dá)到按鈕動態(tài)按下的效果

.prizeContent-box {
 position: relative;
 overflow: hidden;
 perspective: 1000px;
}
.prizeContent-box .expend-text{
 position: absolute;
 top: -5rpx;
 left: 0;
 width: 100%;
 text-align: center;
 font-size: 26rpx;
 font-family:PingFangSC-Regular;
 font-weight: 400;
 color:rgba(205,69,0,1);
 line-height: 37rpx;
 z-index: 10;
}

.prizeContent-box image.second {
 position: relative;
 z-index: 9;
}

.luckyDraw .main .prizeContent {
 width: 490rpx;
 height: 254rpx;
 overflow: hidden;
 border-radius: 16rpx;
 display: flex;
 justify-content: space-between;
 position: absolute;
 left: 50%;
 top: 57rpx;
 z-index: 10;
 margin-left: -246rpx;
}

.prizeBox {
 transform-style: preserve-3d;
 -webkit-transform-style: preserve-3d;
 width: 100%;
 height: 256rpx;
 display: flex;
 justify-content: center;
 transform-origin: 50% 50%;
 animation-timing-function: ease;
}

.luckyDraw .main .prizeContent .prizeList {
 width: 154rpx;
 height: 260rpx;
 position: relative;
 background-color: #fff;
 transform-style: preserve-3d;
 -webkit-transform-style: preserve-3d;
}

.prizeContent .prizeList .red-envelope {
 width: 132rpx;
 height: 256rpx;
 line-height: 256rpx;
 position: absolute;
 left: 50%;
 top: 0;
 margin-left: -66rpx;
 /* transform-origin: center;
 display: flex;
 justify-content: center;
 align-items: center; */
}

.prizeContent .prizeList .red-envelope .text {
 /* width: 120rpx; */
 text-align: center;
 font-size: 120rpx;
 font-weight:900;
 color:rgba(51,51,51,1);
 font-family: Arial, Helvetica, sans-serif;
}
data: {
  // 滾動數(shù)字
  numberArr1: numbers,
  numberArr2: numbers,
  numberArr3: numbers,
  // 記錄當(dāng)前旋轉(zhuǎn)度數(shù)
  animation1: 0,
  animation2: 0,
  animation3: 0,
  // 百十分?jǐn)?shù)字滾動持續(xù)時間
  durationTime1: 2,
  durationTime2: 3,
  durationTime3: 4,
  // 記錄當(dāng)前滾動數(shù)字
  number1: 0,
  number2: 0,
  number3: 0,
  residueDegree: 0, // 剩余抽獎次數(shù)
},
// 初始化步數(shù)信息
initStepInfo() {
  let self = this;
  wx.request({
    url: '/lotter/getStep',
    method: 'GET',
    success(res) {
      let data = res.data.data;
      self.setData({
        residueDegree: parseInt(data.todayStepNum / data.stepFunThresholdNum), // 剩余抽獎次數(shù)
        stepSingle: data.stepFunThresholdNum
      })
    }
  })
},
抽獎事件
 */
start(event, a = 1, b = 2, c = 3) {
  let self = this
  self.initInfo()
  // 判斷當(dāng)前步數(shù)是否有抽獎機(jī)會
  if (this.data.residueDegree <= 0) {
    self.setData({
      lotteryCommonType: 'earnStep',
      lotteryCommonPopupShow: true,
      maskShow: true
    })
    return
  }
  // 判斷距離上次執(zhí)行5s內(nèi)禁止再次抽獎
  if (!self.data.repeatFlag) {
    return
  }
  self.data.repeatFlag = false
  // 獲取抽獎信息
  wx.request({
    url: '/executelottery',
    method: 'POST',
    data: {},
    success(res) {
      if (res.data.code == 1 && res.data.data) {
        let data = res.data.data
        let lotteryNum = data.lotteryNum;
        let [async1, async2, async3] = [lotteryNum.substring(0, 1), lotteryNum.substring(1, 2), lotteryNum.substring(2, 3)]
        self.lottery(async1, async2, async3, data)
      } else {
        self.data.repeatFlag = true
      }
    }
  })
},
// 獲取后端返回抽獎信息
lottery(async1, async2, async3, response) {
  let timer = null
  let self = this
  // 記錄當(dāng)前的滾動數(shù)值 
  let [number1, number2, number3] = [self.data.number1, self.data.number2, self.data.number3]
  // 要轉(zhuǎn)動的度數(shù)
  let [rotateDeg1, rotateDeg2, rotateDeg3] = [0, 0, 0]
  // 百位數(shù)數(shù)字轉(zhuǎn)動判斷
  if (number1 - async1 >= 0) {
    rotateDeg1 = (number1 - async1) * 36
  } else {
    rotateDeg1 = (number1 - async1) * 36 + 720
  }
  // 十位數(shù)數(shù)字轉(zhuǎn)動判斷
  if (number2 - async2 >= 0) {
    rotateDeg2 = (number2 - async2) * 36
  } else {
    rotateDeg2 = (number2 - async2) * 36 + 720
  }
  // 個位數(shù)數(shù)字轉(zhuǎn)動判斷
  if (number2 - async3 >= 0) {
    rotateDeg3 = (number3 - async3) * 36
  } else {
    rotateDeg3 = (number3 - async3) * 36 + 720
  }
  //  重置數(shù)組順序后轉(zhuǎn)動兩圈
  self.setData({
    number1: async1,
    number2: async2,
    number3: async3,
    animation1: self.data.animation1 + 720 + rotateDeg1,
    animation2: self.data.animation2 + 720 + rotateDeg2,
    animation3: self.data.animation3 + 720 + rotateDeg3
  })
  // 5s內(nèi)只執(zhí)行一次抽獎
  timer = setTimeout(() => {
    // 顯示中獎彈框內(nèi)容
    self.prizePopupJudge(response)
    self.data.repeatFlag = true
    clearTimeout(timer)
  }, 5000)
},
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市褒链,隨后出現(xiàn)的幾起案子唁情,更是在濱河造成了極大的恐慌,老刑警劉巖甫匹,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件甸鸟,死亡現(xiàn)場離奇詭異,居然都是意外死亡兵迅,警方通過查閱死者的電腦和手機(jī)抢韭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來恍箭,“玉大人刻恭,你說我怎么就攤上這事〕敦玻” “怎么了鳍贾?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長交洗。 經(jīng)常有香客問我骑科,道長,這世上最難降的妖魔是什么藕筋? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任纵散,我火速辦了婚禮,結(jié)果婚禮上隐圾,老公的妹妹穿的比我還像新娘。我一直安慰自己掰茶,他們只是感情好暇藏,可當(dāng)我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著濒蒋,像睡著了一般盐碱。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上沪伙,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天瓮顽,我揣著相機(jī)與錄音,去河邊找鬼围橡。 笑死暖混,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的翁授。 我是一名探鬼主播拣播,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼晾咪,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了贮配?” 一聲冷哼從身側(cè)響起谍倦,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎泪勒,沒想到半個月后昼蛀,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡圆存,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年叼旋,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片辽剧。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡送淆,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出怕轿,到底是詐尸還是另有隱情偷崩,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布撞羽,位于F島的核電站阐斜,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏诀紊。R本人自食惡果不足惜谒出,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望邻奠。 院中可真熱鬧笤喳,春花似錦、人聲如沸碌宴。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽贰镣。三九已至呜象,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間碑隆,已是汗流浹背恭陡。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留上煤,地道東北人休玩。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親哥捕。 傳聞我的和親對象是個殘疾皇子牧抽,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容