在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;
  }
}

幸运抽奖视频

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在HTML使用Vue.js实现九宫格抽奖功能,首先你需要了解HTML(HyperText Markup Language)用于创建网页结构,Vue.js(一个流行的前端JavaScript框架)则提供了数据绑定和组件化的开发能力。以下是一个简单的步骤指南: 1. **设置项目结构**: 创建一个新的Vue项目,你可以使用Vue CLI工具快速初始化。 ```bash vue create my-kongguo-draw cd my-kongguo-draw ``` 2. **安装所需依赖**: 在`src`目录下,安装`vue-grid-layout`库,它可以帮助你轻松地创建网格布局。 ```bash npm install vue-grid-layout --save ``` 3. **创建组件**: - `KongGuoDraw.vue`:这是主要的组件,负责渲染九宫格并处理抽奖逻辑。 ```html <template> <div class="kong-guo-draw"> <vue-grid-layout :layout="gridLayout" @draw="onDraw"> <!-- 使用v-for遍历每个网格单元 --> <div v-for="(item, index) in gridItems" :key="index" :style="{ top: item.y, left: item.x }"> <button @click="onCellClick(index)">点击抽奖</button> </div> </vue-grid-layout> </div> </template> <script> import Vue from 'vue'; import vueGridLayout from 'vue-grid-layout'; export default { components: { vueGridLayout, }, data() { return { gridLayout: { // 初始化的网格布局配置 }, gridItems: [], // 九宫格的元素数组 }; }, methods: { onDraw() { // 在这里编写抽奖逻辑 }, onCellClick(index) { // 当点击某个单元格时触发 this.gridItems[index].isDrawn = true; // 标记已抽奖 }, }, }; </script> ``` 4. **定义九宫格布局**: 在`data`,根据九宫格的需求配置`gridLayout`和`gridItems`。九宫格通常是3x3的布局,你可以设置列数、行数和每个单元格的大小。 5. **实现抽奖逻辑**: 在`onDraw`方法,可以根据需求决定如何选择或改变九宫格的奖项。可能的方式包括随机选择一个单元格或者当所有单元格都被点击后选择一个。 6. **添加样式**: 在`<style>`标签内,为九宫格和按钮添加合适的CSS样式。 记得在`main.js`导入并使用这个组件,这样九宫格抽奖功能就完成了。以上代码只是一个基础示例,实际应用可能需要根据具体需求调整和优化。如果你有任何问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wen_文文

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

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

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

打赏作者

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

抵扣说明:

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

余额充值