Vue实现简单九宫格抽奖

抽奖
这个是一个简单的抽奖,具体的图文根据需要添加即可
思路:

  1. 开启一个定时器
  2. 获取一个一定范围的随机数字,赋值给对应奖品的索引
  3. 在一定时间关闭定时器,

在这里插入图片描述
HTML

<div>
      <ul class="draw">
        <li class="options"> 
          <img src="@/assets/fj7.jpg" alt="" />
        </li>
        <li class="options">吕布</li>
        <li class="options">王昭君</li>
        <li class="options">大乔</li>
        <li @click="onStart">开始</li>
        <li class="options">小乔</li>
        <li class="options">安琪拉</li>
        <li class="options">西施</li>
        <li class="options">谢谢惠顾</li>
      </ul>
    </div>

JS

data() {
    return {
      num: 0,
      timer: null
    }
  },
 methods: {
    onStart() {
      this.$nextTick(() => {
        if (this.timer === null) {
          this.timer = setInterval(() => {
            this.num++
            const lis = document.querySelectorAll('.draw .options')
            // 获得随机数0-7
            const ran = Math.round(Math.random() * (7 - 0) + 0) 
            lis.forEach(item => {
              item.style.backgroundColor = '#fff'
            })
            lis[ran].style.backgroundColor = 'red'
            if (this.num >= 50) {
              // 根据业务关定时器
              clearInterval(this.timer)
              this.timer = null
              this.num = 0
            }
          }, 100)
        }
      })
    }
  }

CSS

* {
  margin: 0;
  padding: 0;
}
ul {
  width: 602px;
  height: 601px;
  border: 1px solid red;
  display: flex;
  flex-wrap: wrap;
  font-size: 30px;
  font-weight: bold;

  li {
    width: 200px;
    height: 200px;
    border-bottom: 1px solid red;
    border-right: 1px solid red;
    background-color: #fff;
    list-style: none;
    text-align: center;
    line-height: 200px;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
    }
  }
  & li:nth-child(5) {
    background-color: pink;
  }
  & li:nth-child(3n) {
    border-right: 0;
  }
}


我欲乘风归去,又恐琼楼玉宇

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值