vue九宫格随机抽奖

html代码

<div class="cj_box">
      <div class="line_item" :class="index == 1 ? 'active' : 'white_item'">
        <div>洗碗</div>
      </div>
      <div class="line_item" :class="index == 2 ? 'active' : 'white_item'">
        <div>买菜</div>
      </div>
      <div class="line_item" :class="index == 3 ? 'active' : 'white_item'">
        <div>赠送十次</div>
      </div>
      <div class="line_item" :class="index == 0 ? 'active' : 'white_item'">
        <div>做饭</div>
      </div>
      <div class="line_item" @click="goLottery()">
        立即挑选
      </div>
      <div class="line_item" :class="index == 4 ? 'active' : 'white_item'">
        <div>拖地</div>
      </div>
      <div class="line_item" :class="index == 7 ? 'active' : 'white_item'">
        <div>赠送十次</div>
      </div>
      <div class="line_item" :class="index == 6 ? 'active' : 'white_item'">
        <div>待着</div>
      </div>
      <div class="line_item" :class="index == 5 ? 'active' : 'white_item'">
        <div>洗衣服</div>
      </div>
    </div>

css代码

.cj_box {
    width: 300px;
    display: block;
    margin: 0 auto;
  }

  .line_item {
    width: 100px;
    height: 100px;
    line-height: 100px;
    background-color: red;
    float: left;
  }

  .active {
    background-color: #fffea5 !important;
  }

  .white_item {
    background-color: #fff;
  }

js代码

data() {
    return {
      index: 3,    // 当前转动到哪个位置,起点位置
      count: 8,    // 总共有多少个位置
      timer: 0,    // 每次转动定时器
      speed: 200,   // 初始转动速度
      times: 0,    // 转动次数
      cycle: 30,   // 转动基本次数:即至少需要转动多少次再进入抽奖环节
      prizes: 3,   // 中奖位置
      prize: [0,1,2,3,4,5,6,7],   // 奖品位置
    }
  },
   methods: {
    goLottery() {
    //判断如果抽奖停止后才可以再次点击
      if(this.speed == 200) {
        this.prizes = this.prize[Math.floor((Math.random() * this.prize.length))]
        console.log(this.prizes)
        this.startRoll();
      }
    },
    // 开始转动
    startRoll() {
      this.times += 1  // 转动次数
      this.oneRoll()  // 转动过程调用的每一次转动方法,这里是第一次调用初始化
      this.usePrize()
    },
    // 每一次转动
    oneRoll() {
      let index = this.index  // 当前转动到哪个位置
      const count = 8  // 总共有多少个位置
      index += 1
      if (index > count - 1) {
        index = 0
      }
      this.index = index
    },
    usePrize() {
      // 如果当前转动次数达到要求 && 目前转到的位置是中奖位置
      if (this.times > this.cycle + 10 && this.prizes === this.index) {
        clearTimeout(this.timer)   // 清除转动定时器,停止转动
        this.times = 0
        this.speed = 200    //速度初始化
      } else {
        if (this.times < this.cycle) {
          this.speed -= 5   // 加快转动速度
        }
        this.timer = setTimeout(this.startRoll, this.speed)
      }
    }
  }

 

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值