Vue<幸運(yùn)抽獎(jiǎng)-九宮格>

2021-01-19 14-32-53.gif

在線演示地址

實(shí)現(xiàn)思路:
1.要想實(shí)現(xiàn)從 特 - 1 - 2 - 3 - 4 - 5 - 6 - 7的方向執(zhí)行榨汤,必須要在css內(nèi)對(duì)標(biāo)簽進(jìn)行position定位處理屑埋,以便于實(shí)現(xiàn)走馬燈的效果
2.借助setTimeout計(jì)時(shí)器的方法帜慢,通過遞歸改變time來實(shí)現(xiàn)由快變慢的效果

配置參數(shù):

      winner: 0, //指定獲獎(jiǎng)下標(biāo) specified為true時(shí)生效
      specified: true, //是否指定獲獎(jiǎng)結(jié)果,false時(shí)為隨機(jī)
      list: [], //抽獎(jiǎng)列表

      //----------------------上面的為主要參數(shù)--------------------------

      rollIndex: -1, //轉(zhuǎn)動(dòng)時(shí)的下標(biāo)位置
      prize: -1, // 中獎(jiǎng)位置
      count: 8, // 總共有多少個(gè)位置
      speed: 100, // 初始轉(zhuǎn)動(dòng)速度
      cycle: 50, // 轉(zhuǎn)動(dòng)基本次數(shù):即至少需要轉(zhuǎn)動(dòng)多少次再進(jìn)入抽獎(jiǎng)環(huán)節(jié)
      rolling: false, //是否處于抽獎(jiǎng)狀態(tài),防止多次點(diǎn)擊
      timer: null, // 每次轉(zhuǎn)動(dòng)定時(shí)器
      times: 0, // 轉(zhuǎn)動(dòng)次數(shù)
     $lottery_w: 5rem; //每個(gè)獎(jiǎng)品的寬高
     $lottery_h: 5rem; //每個(gè)獎(jiǎng)品的寬高
     $lottery_margin: 0.2rem; //每個(gè)獎(jiǎng)品的間隔

代碼如下:

<template>
  <div class="overall">
    <div class="lottery-box">
      <div class="lottery" v-for="(i,index) in list" :key="index">
        <div class="item" :class="{'on':index == rollIndex}">{{i.title}}</div>
      </div>
      <div class="start" @click="start()">抽獎(jiǎng)</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      winner: 0, //指定獲獎(jiǎng)下標(biāo) specified為true時(shí)生效
      specified: true, //是否指定獲獎(jiǎng)結(jié)果巩剖,false時(shí)為隨機(jī)
      rollIndex: -1, //轉(zhuǎn)動(dòng)時(shí)的下標(biāo)位置
      prize: -1, // 中獎(jiǎng)位置
      count: 8, // 總共有多少個(gè)位置
      speed: 100, // 初始轉(zhuǎn)動(dòng)速度
      cycle: 50, // 轉(zhuǎn)動(dòng)基本次數(shù):即至少需要轉(zhuǎn)動(dòng)多少次再進(jìn)入抽獎(jiǎng)環(huán)節(jié)
      rolling: false, //是否處于抽獎(jiǎng)狀態(tài),防止多次點(diǎn)擊
      timer: null, // 每次轉(zhuǎn)動(dòng)定時(shí)器
      times: 0, // 轉(zhuǎn)動(dòng)次數(shù)
      list: [
        {
          title: '特等獎(jiǎng)'
        },
        {
          title: '一等獎(jiǎng)'
        },
        {
          title: '二等獎(jiǎng)'
        },
        {
          title: '三等獎(jiǎng)'
        },
        {
          title: '四等獎(jiǎng)'
        },
        {
          title: '五等獎(jiǎng)'
        },
        {
          title: '六等獎(jiǎng)'
        },
        {
          title: '七等獎(jiǎng)'
        }
      ]
    }
  },
  methods: {
    start() {
      if (!this.rolling) {
        this.startRoll()
      }
    },
    //開始轉(zhuǎn)動(dòng)方法
    startRoll() {
      this.rolling = true //開啟轉(zhuǎn)動(dòng)狀態(tài)
      this.times += 1 // 轉(zhuǎn)動(dòng)次數(shù)
      this.oneRoll() // 轉(zhuǎn)動(dòng)過程調(diào)用的每一次轉(zhuǎn)動(dòng)方法,這里是第一次調(diào)用初始化

      // 如果當(dāng)前轉(zhuǎn)動(dòng)次數(shù)達(dá)到要求 && 目前轉(zhuǎn)到的位置是中獎(jiǎng)位置
      if (this.times > this.cycle + 10 && this.prize === this.rollIndex) {
        clearTimeout(this.timer) // 清除轉(zhuǎn)動(dòng)定時(shí)器暑始,停止轉(zhuǎn)動(dòng)
        //初始化相關(guān)數(shù)據(jù)
        this.times = this.$options.data().times
        this.speed = this.$options.data().speed
        setTimeout(res => {
          this.rolling = false
          alert(`恭喜你抽中了${this.list[this.rollIndex].title}`)
        }, 500)
      } else {
        if (this.times < this.cycle) {
          this.speed -= 10 // 加快轉(zhuǎn)動(dòng)速度
        } else if (this.times === this.cycle) {
          //當(dāng)轉(zhuǎn)動(dòng)次數(shù)達(dá)到規(guī)定的 必要轉(zhuǎn)動(dòng)次數(shù)后 開始確定中獎(jiǎng)位置
          //判斷是否指定獲獎(jiǎng)結(jié)果,false時(shí)為隨機(jī)
          if (this.specified) {
            this.prize = this.winner
          } else {
            const index = this.random(0, this.count - 1) // 隨機(jī)獲得一個(gè)中獎(jiǎng)位置
            this.prize = index //中獎(jiǎng)位置,可由后臺(tái)返回
          }
        } else {
            //當(dāng)轉(zhuǎn)動(dòng)次數(shù)times > cycle規(guī)定停止的轉(zhuǎn)次時(shí)婴削,開始慢慢減速廊镜,直到達(dá)到指定的中獎(jiǎng)位置
          this.speed += 20
        }
        //當(dāng)轉(zhuǎn)速達(dá)到一定高度時(shí),就固定為40ms/轉(zhuǎn)
        if (this.speed < 40) {
          this.speed = 40
        }
        this.timer = setTimeout(this.startRoll, this.speed)
      }
    },
    // 每一次轉(zhuǎn)動(dòng)
    oneRoll() {
      let index = this.rollIndex // 當(dāng)前轉(zhuǎn)動(dòng)到哪個(gè)位置
      const count = this.count // 總共有多少個(gè)位置
      index += 1
      if (index > count - 1) {
        index = 0
      }
      this.rollIndex = index
    },
    random(min, max) {
      return parseInt(Math.random() * (max - min + 1) + min)
    }
  }
}
</script>
<style lang="scss" scoped>
$lottery_w: 5rem; //每個(gè)獎(jiǎng)品的寬高
$lottery_h: 5rem; //每個(gè)獎(jiǎng)品的寬高
$lottery_margin: 0.2rem; //每個(gè)獎(jiǎng)品的間隔

.lottery-box {
  position: relative;
  //抽獎(jiǎng)盒子寬高 因?yàn)槭?3*3布局唉俗,所以這里*3
  width: $lottery_w * 3;
  height: $lottery_h * 3;
  //抽獎(jiǎng)按鈕樣式
  .start {
    width: $lottery_w - $lottery_margin * 2;
    height: $lottery_h - $lottery_margin * 2;
    background: rgb(8, 230, 218);
    position: absolute;
    top: $lottery_h + $lottery_margin;
    left: $lottery_w + $lottery_margin;
    border-radius: 0.5rem;
    display: flex;
    justify-content: center;
    align-items: center;
    color: white;
  }
  //每個(gè)獎(jiǎng)品的外層樣式
  .lottery {
    background: #fcfcfc;
    width: $lottery_w;
    height: $lottery_h;
    box-sizing: border-box;
    position: absolute;
    left: 0;
    top: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: $lottery_margin;
    // 每個(gè)獎(jiǎng)品的內(nèi)容樣式
    .item {
      width: 100%;
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      box-sizing: border-box;
      border-radius: 0.5rem;
      box-shadow: 0 0 5px #ccc;
      //轉(zhuǎn)動(dòng)時(shí)的高亮效果
      &.on {
        background: rgb(92, 245, 199);
        color: white;
        font-weight: bold;
      }
    }
  }
  //針對(duì)每個(gè)獎(jiǎng)品塊單獨(dú)調(diào)整位置嗤朴,以便適應(yīng)于跑馬燈的效果
  .lottery:nth-child(1) {
    left: 0;
    top: 0;
  }
  .lottery:nth-child(2) {
    top: 0 * $lottery_h;
    left: $lottery_w;
  }
  .lottery:nth-child(3) {
    top: 0 * $lottery_h;
    left: 2 * $lottery_w;
  }
  .lottery:nth-child(4) {
    top: 1 * $lottery_h;
    left: 2 * $lottery_w;
  }
  .lottery:nth-child(5) {
    top: 2 * $lottery_h;
    left: 2 * $lottery_w;
  }
  .lottery:nth-child(6) {
    top: 2 * $lottery_h;
    left: 1 * $lottery_w;
  }
  .lottery:nth-child(7) {
    top: 2 * $lottery_h;
    left: 0 * $lottery_w;
  }
  .lottery:nth-child(8) {
    top: 1 * $lottery_h;
    left: 0 * $lottery_w;
  }
}
</style>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市虫溜,隨后出現(xiàn)的幾起案子雹姊,更是在濱河造成了極大的恐慌,老刑警劉巖衡楞,帶你破解...
    沈念sama閱讀 219,589評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件吱雏,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡瘾境,警方通過查閱死者的電腦和手機(jī)歧杏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,615評(píng)論 3 396
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來迷守,“玉大人犬绒,你說我怎么就攤上這事《以洌” “怎么了凯力?”我有些...
    開封第一講書人閱讀 165,933評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵茵瘾,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我咐鹤,道長(zhǎng)拗秘,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,976評(píng)論 1 295
  • 正文 為了忘掉前任慷暂,我火速辦了婚禮聘殖,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘行瑞。我一直安慰自己奸腺,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,999評(píng)論 6 393
  • 文/花漫 我一把揭開白布血久。 她就那樣靜靜地躺著突照,像睡著了一般。 火紅的嫁衣襯著肌膚如雪氧吐。 梳的紋絲不亂的頭發(fā)上讹蘑,一...
    開封第一講書人閱讀 51,775評(píng)論 1 307
  • 那天,我揣著相機(jī)與錄音筑舅,去河邊找鬼座慰。 笑死,一個(gè)胖子當(dāng)著我的面吹牛翠拣,可吹牛的內(nèi)容都是我干的版仔。 我是一名探鬼主播,決...
    沈念sama閱讀 40,474評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼误墓,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼蛮粮!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起谜慌,我...
    開封第一講書人閱讀 39,359評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤然想,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后欣范,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體变泄,經(jīng)...
    沈念sama閱讀 45,854評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,007評(píng)論 3 338
  • 正文 我和宋清朗相戀三年恼琼,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了杖刷。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,146評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡驳癌,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出役听,到底是詐尸還是另有隱情颓鲜,我是刑警寧澤表窘,帶...
    沈念sama閱讀 35,826評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站甜滨,受9級(jí)特大地震影響乐严,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜衣摩,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,484評(píng)論 3 331
  • 文/蒙蒙 一昂验、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧艾扮,春花似錦既琴、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,029評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至酌予,卻和暖如春磺箕,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背抛虫。 一陣腳步聲響...
    開封第一講書人閱讀 33,153評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工松靡, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人建椰。 一個(gè)月前我還...
    沈念sama閱讀 48,420評(píng)論 3 373
  • 正文 我出身青樓雕欺,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親广凸。 傳聞我的和親對(duì)象是個(gè)殘疾皇子阅茶,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,107評(píng)論 2 356

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