怎么使用小程序?qū)崿F(xiàn)一個(gè)變速大轉(zhuǎn)盤?下面本篇文章給大家介紹一下使用小程序?qū)崿F(xiàn)一個(gè)變速大轉(zhuǎn)盤的方法,希望對(duì)大家有所幫助!
使用小程序來實(shí)現(xiàn)一個(gè)大轉(zhuǎn)盤吧!大轉(zhuǎn)盤都不陌生,開始抽獎(jiǎng),然后停止的位置就是獲得的獎(jiǎng)品。
實(shí)現(xiàn)方法:setInterval
先來實(shí)現(xiàn)一下勻速大轉(zhuǎn)盤吧
先將轉(zhuǎn)盤設(shè)計(jì)好,比如3?x?3?的大轉(zhuǎn)盤,中間是個(gè)開始按鈕; 我這里設(shè)置的是背景顏色的變化,當(dāng)抽獎(jiǎng)到達(dá)某個(gè)位置時(shí),這個(gè)位置的顏色發(fā)生變化; 先貼一下我的ttml頁面吧(不要在意我奇怪的配色~) //?index.ttml <view class="container"> ????<view class="box"> ????????<view class="item" style="background-color: {{ index == 4 ? 'red': (index == active ? 'rgb(229, 250, 250)' : 'rgb(236, 216, 135)')}};" tt:for="{{games}}" bindtap="{{index == 4 ? 'beginLottery' : ''}}">{{item}}</view> ????</view> </view> 順便css也貼一下吧,看效果直接復(fù)制就好了嘛 //?index.ttss .box{ ????margin:?0?auto; ????width:?600rpx; ????display:?flex; ????flex-wrap:?wrap; ????border:?1px?solid?black; } .item{ ????width:?200rpx; ????height:?200rpx; ????line-height:?200rpx; ????text-align:?center; }
另起一行,只是換個(gè)位置貼js
- 先看data:games是轉(zhuǎn)盤上要顯示的內(nèi)容,轉(zhuǎn)盤的格式可以根據(jù)自己的需求自己來寫,我這個(gè)就是最基本的。active用來記錄旋轉(zhuǎn)到了什么位置,start用來記錄開始的位置
- 再來看全局定義的round和timer。round用來設(shè)置一個(gè)軌跡,相當(dāng)于鋪路啦,里面是要走的下標(biāo),剛好是外圍一圈。timer是定時(shí)器
- 最后看begin方法吧
// index.js const round = [0,1,2,5,8,7,6,3,0]; let timer ; Page({ data: { games:['$1','$2','$3','$4','開始','$5','$6','$7','$8'], active: 0, start: 0, }, onLoad: function (options) { }, beginLottery(){ this.begin(); }, // begin begin(){ let start = this.data.start; let random = Math.floor(Math.random()*9); let num = 0; timer = setInterval(() => { start++; start = start > 8 ? 0 : start; this.setData({ start, active: round[start] }) num++; if(num > 24 && this.data.active == random){// clearInterval(timer) } }, 70); } })
比較簡單,然后實(shí)現(xiàn)變速,其實(shí)速度的改變就是旋轉(zhuǎn)一圈時(shí)間的改變
我這里的設(shè)計(jì)是:每旋轉(zhuǎn)兩圈實(shí)現(xiàn)一次變速,每次變速的時(shí)間在上一次時(shí)間上+100s,在第五圈停止
//index.js const round = [0, 1, 2, 5, 8, 7, 6, 3, 0]; let timer; // 定時(shí)器 let num = 0; // 用來記錄一共轉(zhuǎn)了幾次,方便判斷轉(zhuǎn)的圈數(shù) let start = 0; // 記錄開始的位置下標(biāo) let random = ''; // 記錄停下來的隨機(jī)數(shù)(下標(biāo)) let time = 70; // 記錄定時(shí)器的時(shí)間 let count = 0; // 記錄圈數(shù),用來判斷每2圈一次變速 Page({ data: { games: ['$1', '$2', '$3', '$4', '開始', '$5', '$6', '$7', '$8'], active: 0, }, onLoad: function (options) {}, beginLottery() { this.begin1(); }, begin1() { if(num != 0){ // 防止用戶重復(fù)點(diǎn)擊 return } timer = setInterval(this.process, time); }, // 旋轉(zhuǎn)的過程 process() { start = start + 1; if (start >= 8) { start = 0; // 當(dāng)start = 8的時(shí)候,表示已經(jīng)轉(zhuǎn)過1圈了count+1 count = count + 1; } this.setData({ active: round[start] }) num = num + 1; // 實(shí)現(xiàn)兩圈一次變速 if (num % 8 === 0 && count === 2) { count = 0; clearInterval(timer); time = time + 100; timer = setInterval(this.process, time); // 轉(zhuǎn)了4圈,即將在第五圈停止 if (Math.floor(num / 8) === 4) { this.getRandom(); } } if (this.data.active === random) { clearInterval(timer); num = 0; random = ''; time = 70; count = 0; } }, getRandom(){ let n = Math.floor(Math.random() * 9); if(n == 4){ this.getRandom(); }else{ random = n return; } } })
示例代碼是根據(jù)旋轉(zhuǎn)的圈數(shù)來進(jìn)行變速,也可以根據(jù)旋轉(zhuǎn)一定的時(shí)間來實(shí)現(xiàn)變速,這樣就需要使用setTimeout來實(shí)現(xiàn)。
好啦,這次記錄就到這里啦,有更好的解決方案,性能優(yōu)化 歡迎評(píng)論!
【相關(guān)學(xué)習(xí)推薦:小程序開發(fā)教程】
? ??? ?? ?? ?? ????? ???? ?? ?? ????? ???? ??? ?? ??? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

??? ??? ??? ????? ???? ?? WeChat? ???? ?? ???? ?? ??????? ?????. WeChat ?? ????? ???? ???? ??????? ?????? ???? ??? ?? ????? ?? ???? ? ?? ??? ?? ??? ??? ? ????. ? ????? Python? ???? WeChat ???? ???? ??? ?????. 1. ?? Python? ???? WeChat ???? ???? ?? ?? Python ?????? ???? ???. ???? wxpy? itchat ? ?????? ???? ?? ????. wxpy? ?? ?????

WeChat ?? ?????? ?? ??? ?? ?? WeChat ?? ?????? ?? ??? ??? ???? ?? ??? ??? ????? ?? ??? ??? ???? ? ?? ???? ????? ?????. ??? WeChat ????? ?? ??? ??? ???? ??? ??? ???? ?? ?? ??? ?????. ??, ?? ????? ??? ???? ??? ? ?? ?? ??? ???? ???. ??? ?? ??? ???? ?? ??? ?? ??? ?? ??? ???? ?? ????. <--index.wxml- ->&l

10? 31? ? ???? ??? ??? ?? 5? 27? Ant Group? '?? ?? ????'? ????? ????? ?? ??? ??? ?????. Alipay? '?? ?? - ??? ?? ??' ?? ????? ??????. ?? ???? ?? ??? ?????? ???? ?? ???? ?? ??? ?? ??? ???? Alipay? ?? ??? ?? ??? ???? ? ??? ???. ?? ???? "????", "????" ?? ???? ???? "????" ???? ??? ? ????. ?? ?????? ???? ????? ?? ? ???? ?? ?? ??? ??? ??? ? ??? ?? ? Alipay ????? ?? ?????? ?? ??? ?????. ? ??????? ?? ??????? ?? ?? ?? ?? ??? ??? ? ??? ?????. ? ?? ??? ??? ???? ?? ??? ?? ???????. ??? ??

?? ????? ??? ??? ? ????. ?? ??: 1. "react-reconciler"? ???? ???? ???? DSL? ?????. 2. DSL? ?? ???? ????? ?? ?? ???? ?? ??? ????. 3. npm? ???? ???? ?????. ???? npm? ?????. 4. ??? ???? ???? ??? ?? API? ???? ??? ?????.

???? ?? ????? H5 ??? ??? ????? ???? ?? ??? ?????. ?? ??? ???? ??? ????? ???? ?? ?? ????? H5? ?? ?????? ??? ?????. ??? ??? ?? ?????? uniapp? ?? ??? ???? ?? ????? H5 ?? ??? ???? ???? ?? ???? ?? ???? ? ????. ? ????? uniapp? ?? ????? H5 ?? ??? ??? ???? ??? ???? ???? ?? ??? ?????. 1. ??? ??? ??

?? ?? ?? ?? ????? ??? ? ?? ??? ?? ????, ?? ?????? ?? ?? ??? ???? ???? ??? ?? ???????.

?? ???? x01 ?? ?? ??, ?? ???? ??? ???? ???? ?????. ?? ??? ??? ??? ? ???? ?? ??? ?? ? ??? ?????. ?? ???? ???? ???? ??? ?? ??? ?????. x02 ?????? ??? ???? ?? ?????. ?????? ??? ???? ??? ?? ???? ?? ??? ???? ?????. ??? ??? ??? ????? ????? ??? ? ?? ???? ???? ???. ??? ??? ?? ???? ?? ??? ??? ?? ?????. ????, ??

PHP ? ?? ????? ??? ?? ?? ? ?? ?? ??? ?? ?? ? ?? ??? ?? ???? ??? ?? ? ??? ?????. ??? ??? ??? ?? ?? ?? ? ?? ??? ?? ???? ??? ???? ????. ?? ???? PHP? ???? ? ?? ???? ?? ?????. ? ????? PHP ? ?? ?????? ??? ?? ?? ? ?? ?? ?? ??? ???? ?? ?? ??? ?????. 1. PHP? ???? PHP??? ?? ????? ??? ? ????.
