Vue_cli实现抽奖

Vue_cli实现抽奖

<template>
  <div class="test02">
    <p>我是test02</p>
    <div class="prize">
      <ul>
        <li v-for="(item, i) of mapList" :key="i" :class="{active: activeIndex === item.index}" @click="startLuck(item)">{{item.text}}</li>
      </ul>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      activeIndex: null, // 当前抽中的奖品
      lucky: null, // 抽中的商品
      // 奖品数据
      list: [
        { id: 1, text: '奖品1' }, // 0
        { id: 2, text: '奖品2' }, // 1
        { id: 3, text: '奖品3' }, // 2
        { id: 4, text: '奖品4' }, // 3
        { id: 5, text: '奖品5' }, // 4
        { id: 6, text: '奖品6' }, // 5
        { id: 7, text: '奖品7' }, // 6
        { id: 8, text: '奖品8' } // 7
      ]
    }
  },
  computed: {
    mapList () {
      // map 修改原素组返回新数组,但是原素组不受影响
      const newList = this.list.map((item, index) => {
        // 修改list数据的索引
        switch (index) {
          case 3:
            return {
              ...this.list[7],
              index: 7
            }
          case 4:
            return {
              ...this.list[3],
              index: 3
            }
          case 5:
            return {
              ...this.list[6],
              index: 6
            }
          case 6:
            return {
              ...this.list[5],
              index: 5
            }
          case 7:
            return {
              ...this.list[4],
              index: 4
            }
          default:
            return {
              ...item,
              index: index
            }
        }
      })
      // 将新数组newList截取前三个 + 自定义一个按钮 + newlist的后4个
      return newList.slice(0, 4).concat({ id: 'btn', text: '开始抽奖' }).concat(newList.slice(4))
    /*
      return的数据
      0: {id: 1, text: "奖品1", index: 0}
      1: {id: 2, text: "奖品2", index: 1}
      2: {id: 3, text: "奖品3", index: 2}
      3: {id: 8, text: "奖品8", index: 7}
      4: {id: "btn", text: "开始抽奖"}
      5: {id: 4, text: "奖品4", index: 3}
      6: {id: 7, text: "奖品7", index: 6}
      7: {id: 6, text: "奖品6", index: 5}
      8: {id: 5, text: "奖品5", index: 4}
      length: 9
    */
    }
  },
  methods: {
    // 传的item是点击当前元素
    startLuck (item) {
      // 如果当前元素的id不是btn,就不执行
      if (item.id !== 'btn') return
      this.activeIndex = null
      this.lucky = null
      setTimeout(() => { // 随机抽中奖品
        this.lucky = Math.floor(Math.random() * 8)
        // console.log('奖品是' + this.lucky) // 返回的是索引
        console.log('奖品是' + this.list[this.lucky].text)
      }, 1000)
      // 执行动画
      this.move(10)
    },
    move (time) {
      if (time > 600) { // time大于600就减速执行
        if (this.activeIndex !== this.lucky) { // 如果this.activeIndex 不等于 抽中的奖品this.lucky
          setTimeout(() => {
            const t = this.activeIndex + 1
            this.activeIndex = t % 8
            this.move(time + time * 0.05)
          }, time)
        } else {
          // console.log('抽中的奖品是' + this.activeIndex) // 返回的是索引
          console.log('抽中的奖品是' + this.list[this.activeIndex].text)
        }
      } else { // time小于600就原速执行
        setTimeout(() => {
          const t = this.activeIndex + 1
          this.activeIndex = t % 8
          this.move(time + time * 0.1)
        }, time)
      }
    }
  },
  mounted () {
    // console.log(this.mapList)
  }
}
</script>
<style lang="scss">
  .test02{
    .prize{
      width: 600px;
      margin: 100px auto;
      ul{
        display: flex;
        flex-wrap: wrap;
        li{
          width: 200px;
          height: 200px;
          text-align: center;
          line-height: 200px;
          border: 1px solid #000;
          box-sizing: border-box;
          &.active{
            width: 200px;
            height: 200px;
            background-color: red;
            opacity: 0.5;
          }
          &:nth-child(5){
            background-color: red;
          }
        }
      }
    }
  }
</style>
<!--
  0 1 2
  3   4
  5 6 7

  0 1 2
  7   3
  6 5 4
-->

效果图
效果图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
预览图: https://s4.ax1x.com/2022/01/15/7YyPjx.jpg https://s4.ax1x.com/2022/01/15/7YyFu6.jpg https://s4.ax1x.com/2022/01/15/7YykDK.jpg https://s4.ax1x.com/2022/01/15/7YyVED.jpg https://s4.ax1x.com/2022/01/15/7Yye4H.jpg https://s4.ax1x.com/2022/01/15/7YyKgI.jpg - 本程序理论支持配置百万级别人数,超过 10 万以上时初始化会比较耗时,初始化后可以正常抽奖。 - 如果人数较多,不建议导入名单或照片,按号码抽奖即可。 (1)抽奖配置:- 设置抽奖总人数、奖项及每个奖项的人数,默认包含两个奖项,如果不想抽取默认的奖项,可以设置该奖项数量为 0 - 可以新增自定义的奖项,新增后必须将奖项人数设置大于 0 才会在抽奖奖项列表中显示 (2)抽奖结果:- 显示抽取的结果,点击号码允许从结果中删除该号码。删除后该号码可以参与剩余的抽奖,否则已经中的号码无法参与剩余奖项的抽奖(除非开启全员参与功能) (3)开始: - 开始抽奖,需要选取抽取的奖项、本次抽取的人数和是否开启全员抽奖功能。 - 本次抽取的人数可以选择 1 人、5 人、一次性抽取完或者自定义抽取数量,不能大于奖项剩余的数量 - 点击停止抽取完成 (4)重置 - 重置数据恢复到初始状态 - 可选的重置选项: 1.重置全部数据 2.重置抽奖配置 3.重置名单 4.重置照片 5.重置抽奖结果 (5)导入名单 - 按照格式导入名单,可以多次输入。若号码有对应的姓名,则在抽取过程及结果中会显示号码及姓名,若没有对应的姓名,则只显示号码。 (6)导入相册 - 按照 抽奖号-照片的一对一导入,(可在现场将号码发出签到后,每发一个号码,导入一个照片)。抽奖结果将以照片形式展示。 - 照片格式支持.jpg和.png,照片大小不能超过 150kb,建议 20-50kb,建议尺寸为 160*160px (7)温馨提示 - 本抽奖程序无暗箱操作,无后台,无后门。 - 名单和照片显示只需导入一种即可,无导入数据则使用抽奖号码。 - 建议使用最新的 Chrome 浏览器打开体验最佳。 - 由于背景音乐加载较慢,可以在抽奖前提前打开缓存好

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

码上暴富

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

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

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

打赏作者

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

抵扣说明:

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

余额充值