在Vue项目中实现九宫格幸运抽奖转盘

<h2>幸运抽奖</h2>
    <div class="w-turntable-content">
      <div v-for="(item,index) in prizeList" :key="index" :class="index === 4 ? 'turntable-btn' : 'turntable-item'" >
        <template v-if="index === 4">
          <!-- 抽奖按钮 -->
          <button class="item-btn-img" :class="curIndex===index && 'active-img'" @click="handleLottery">{{item.prizeName}}</button>
        </template>
        <template v-else>
          <!-- 奖品展示 -->
          <span class="name">{{item.prizeName}}</span>
          <!-- 抽奖活动开始时的遮罩层: 开始抽奖 || 抽奖结束 -->
          <div v-if="(prizeFlag === 1 && curIndex !== index) || (prizeFlag === 2 && prizeIndex !== index)" class="start-activity-bg"></div>
        </template>
      </div>
    </div>


class TestLottery extends Vue {

  private curLuckyDrawTimes: number = 10 // 用户剩余的抽奖次数,由接口返回,这里用于测试初始化为10
  private prizeFlag: number = 0 // 记录抽奖转盘当前的状态; 0:待开始;1-进行中;2-已完成
  private prizeIndex: number = 8 // 记录本次抽奖的奖品下标
  private curIndex: number = -1 // 当前抽奖转盘转动过程中轮询到的九宫格下标
  private counts: number = 8 // 定义转盘抽奖过程中可转的圈数
  private timer

  get prizeList() {
     return [
      {
        location: 1,
        prizeType: 2,
        prizeName: '奖品1'
      },
      {
        location: 2,
        prizeType: 2,
        prizeName: '奖品2'
      },
      {
        location: 3,
        prizeType: 2,
        prizeName: '奖品3'
      },
      {
        location: 4,
        prizeType: 3,
        prizeName: '谢谢参与'
      },
      {
        id: 'btn',
        prizeName: '点击抽奖',
        prizeType: 3,
        location: 5
      },
      {
        location: 5,
        prizeType: 2,
        prizeName: '奖品4'
      },
      {
        location: 6,
        prizeType: 2,
        prizeName: '奖品5'
      },
      {
        location: 7,
        prizeType: 2,
        prizeName: '奖品6'
      },
      {
        location: 8,
        prizeType: 3,
        prizeName: '谢谢参与'
      }
    ]
  }


  // 点击抽奖按钮触发
  async handleLottery() {
    if (this.curLuckyDrawTimes < 1) {
      console.log('暂无抽奖次数')
      return
    }
    if (this.prizeFlag === 1) {
      console.log('正在进行抽奖中,请稍等!')
      return
    }
    // 获取该次抽奖的奖品下标,设置最后停留的中奖下标(实际由接口返回)
    // 判断该用户是否有抽奖活动资格,(实际由接口返回)
    const flag = true
    console.log('是否有抽奖资格:', flag, this.prizeIndex)
    if (!flag) {
      console.log('暂无抽奖资格')
      return
    }
    // 记录转盘转动结束时要停留的奖品下标
    const pIndex = this.prizeIndex
    this.prizeFlag = 1
    // 记录顺时针转动时奖品的下标集合,剔除掉按钮的下标,
    const array = [0, 1, 2, 5, 8, 7, 6, 3]
    // 记录当前停留的奖品在转盘中的下标,方便设置转动时的样式
    let i = 0
    // 记录转盘转动的圈数
    let counts = 1
    const playRun = () => {
      this.curIndex = array[i++]
      // 最后一圈在选中中奖下标时就停止
      if (counts === this.counts && this.curIndex === pIndex) {
        // 打开奖品预览弹窗
        clearInterval(this.timer)
        // 设置本次抽奖转盘活动为已完成状态
        this.prizeFlag = 2
        setTimeout(() => {
          alert(`当前抽中的奖品: ${this.prizeList[this.curIndex].prizeName}`)
        }, 100)
        return
      }
      if (i === 8) {
        // 转盘转完了一圈,继续开始新的一圈
        i = 0
        counts++
      }
      console.log('counts:', counts, this.counts)
      // 设置转盘的速度为: 慢-快-慢
      if (counts > 1 && counts < this.counts - 1) {
        clearInterval(this.timer)
        this.timer = setInterval(playRun, 50)
      } else if (counts === this.counts - 1) {
        clearInterval(this.timer)
        this.timer = setInterval(playRun, 100)
      } else if (counts === this.counts) {
        clearInterval(this.timer)
        this.timer = setInterval(playRun, 150)
      }
    }
    this.timer = setInterval(playRun, 100)
  }
}
.w-turntable-content {
  display: flex;
  flex-wrap: wrap;
  width: 288px;
  height: 288px;
  background-color: #fff;
  margin: 20px auto;
  .turntable-item {
    width: 92px;
    height: 92px;
    border-radius: 16px;
    background-size: 100%;
    margin-right: 6px;
    margin-bottom: 6px;
    padding: 12px 16px 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
    background-color: rgba(250, 110, 90);

    &:nth-last-child(1), &:nth-last-child(2), &:nth-last-child(3) {
      margin-bottom: 0;
    }
    &:nth-child(3n) {
      margin-right: 0;
    }
    .item-img {
      width: 60px;
      height: 60px;
      border-radius: 8px;
    }

    .name {
      margin-bottom: 8px;
      font-size: 12px;
      font-weight: 400;
      color: #835023;
      line-height: 18px;
      width: 92px;
      text-align: center;
    }
  }
  .turntable-btn {
    width: 92px;
    height: 92px;
    margin-right: 6px;
    .item-btn-img {
      width: 100%;
      height: 100%;
      border-radius: 16px
    }
  }
  // 抽奖活动开始时的遮罩层
  .start-activity-bg {
    width: 92px;
    height: 92px;
    position: absolute;
    top: 0;
    left: 0;
    background: rgba(0,0,0,0.3);
    border-radius: 16px;
  }
}

幸运抽奖视频

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wen_文文

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值