成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

如何使用setInterval方法實現(xiàn)一個變速大轉(zhuǎn)盤

這篇文章主要講解了“如何使用setInterval方法實現(xiàn)一個變速大轉(zhuǎn)盤”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“如何使用setInterval方法實現(xiàn)一個變速大轉(zhuǎn)盤”吧!

成都創(chuàng)新互聯(lián)是一家專業(yè)提供??h企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、H5技術(shù)、小程序制作等業(yè)務(wù)。10年已為??h眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進行中。

使用小程序來實現(xiàn)一個大轉(zhuǎn)盤吧!大轉(zhuǎn)盤都不陌生,開始抽獎,然后停止的位置就是獲得的獎品。

實現(xiàn)方法:setInterval

先來實現(xiàn)一下勻速大轉(zhuǎn)盤吧

先將轉(zhuǎn)盤設(shè)計好,比如3 x 3 的大轉(zhuǎn)盤,中間是個開始按鈕;
我這里設(shè)置的是背景顏色的變化,當(dāng)抽獎到達某個位置時,這個位置的顏色發(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;
}

另起一行,只是換個位置貼js

  1. 先看data:games是轉(zhuǎn)盤上要顯示的內(nèi)容,轉(zhuǎn)盤的格式可以根據(jù)自己的需求自己來寫,我這個就是最基本的。active用來記錄旋轉(zhuǎn)到了什么位置,start用來記錄開始的位置

  2. 再來看全局定義的round和timer。round用來設(shè)置一個軌跡,相當(dāng)于鋪路啦,里面是要走的下標(biāo),剛好是外圍一圈。timer是定時器

  3. 最后看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);
    
  }
})

比較簡單,然后實現(xiàn)變速,其實速度的改變就是旋轉(zhuǎn)一圈時間的改變

我這里的設(shè)計是:每旋轉(zhuǎn)兩圈實現(xiàn)一次變速,每次變速的時間在上一次時間上+100s,在第五圈停止

//index.js

const round = [0, 1, 2, 5, 8, 7, 6, 3, 0];
let timer; // 定時器
let num = 0; // 用來記錄一共轉(zhuǎn)了幾次,方便判斷轉(zhuǎn)的圈數(shù)
let start = 0; // 記錄開始的位置下標(biāo)
let random = ''; // 記錄停下來的隨機數(shù)(下標(biāo))
let time = 70; // 記錄定時器的時間
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ù)點擊
      return
    }
    timer = setInterval(this.process, time);
  },
  // 旋轉(zhuǎn)的過程
  process() {
    start = start + 1;
    if (start >= 8) {
      start = 0;
      // 當(dāng)start = 8的時候,表示已經(jīng)轉(zhuǎn)過1圈了count+1
      count = count + 1;
    }
    this.setData({
      active: round[start]
    })
    num = num + 1;
    // 實現(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ù)旋轉(zhuǎn)一定的時間來實現(xiàn)變速,這樣就需要使用setTimeout來實現(xiàn)。

感謝各位的閱讀,以上就是“如何使用setInterval方法實現(xiàn)一個變速大轉(zhuǎn)盤”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對如何使用setInterval方法實現(xiàn)一個變速大轉(zhuǎn)盤這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

文章標(biāo)題:如何使用setInterval方法實現(xiàn)一個變速大轉(zhuǎn)盤
本文網(wǎng)址:http://jinyejixie.com/article14/ghhdde.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司用戶體驗、服務(wù)器托管、動態(tài)網(wǎng)站、電子商務(wù)、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
伊吾县| 峨山| 安康市| 含山县| 冀州市| 康保县| 哈尔滨市| 绍兴市| 象山县| 五原县| 泽州县| 轮台县| 象州县| 定远县| 阿巴嘎旗| 新平| 高密市| 张家川| 衡阳市| 林芝县| 阜南县| 酒泉市| 锦州市| 民县| 儋州市| 庆城县| 阿瓦提县| 玉山县| 嵩明县| 凤台县| 安乡县| 雷山县| 贵州省| 广东省| 重庆市| 江达县| 噶尔县| 四会市| 广昌县| 耿马| 内黄县|