用html+js实现9宫格翻牌抽奖【建议收藏】

在前端浏览器页面,我们想实现抽奖的功能,铁子们可以朝这儿看齐,9个格子可以填写9份礼物,而且礼物可以控制位置,公司举办活动啥的直接就能用,方便实用;

1、效果展示

 

 2、代码分享

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>9宫格翻牌抽奖</title>
  <style>
    .title {
      text-align: center;
    }

    .box {
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
      width: 330px;
      margin: 0 auto;
    }

    .item {
      position: relative;
      margin: 5px;
      width: 100px;
      height: 100px;
    }

    .style1,
    .style2 {
      position: absolute;
      left: 0;
      top: 0;
      bottom: 0;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 5px;
      text-align: center;
      transition: transform .4s
    }

    .style1 {
      background-color: #ff8800;
      color: #fff;
    }

    .style2 {
      color: #ff8800;
      border: 1px solid #ff8800;
      padding: 0 10px;
      transform: scaleX(0);
      background-color: #ffe6c9;
    }

    .hide {
      transform: scaleX(0);
    }

    .show {
      transform: scaleX(1);
    }

    .show-result {
      transform: scaleX(1);
      opacity: 0.5;
    }
  </style>
</head>
<style>
</style>

<body>
  <div class="title">可抽奖<label id="count">0</label>次</div>
  <div class="box"></div>
</body>
<script>
  // 奖品列表
  const list = [
    { id: 1, name: '1元优惠券', is: true },
    { id: 2, name: '10元优惠券', is: true },
    { id: 3, name: '谢谢惠顾', is: true },
    { id: 4, name: '豪华电动车' },
    { id: 5, name: '1w购物券' },
    { id: 6, name: '5w购物券' },
    { id: 7, name: '豪华轿车' },
    { id: 8, name: '房子一套' },
    { id: 9, name: '顶配笔记本' }
  ].sort(v => Math.random() - 0.5)
  // 中奖项
  const isArr = list.filter(v => v.is)
  // 非中奖项
  const noArr = list.filter(v => !v.is)
  // 抽奖次数
  let count = 3
  const box = document.querySelector('.box')
  const countEL = document.querySelector('#count')
  countEL.innerHTML = count
  box.innerHTML = list.map(v => {
    return `<div class="item" onclick="fp(this)">
            <div class="style1">抽奖</div>
            <div class="style2"></div>
        </div>`
  }).join('')
  // 翻牌抽奖
  const fp = (me) => {
    if (count === 0) {
      return
    }
    me.querySelector('.style1').classList.add('hide')
    // 抽奖动画
    setTimeout(() => {
      // 从非中奖项中取最后一个,并从数组中移除该项
      const item = isArr.pop()
      const style2 = me.querySelector('.style2')
      style2.innerHTML = item.name
      style2.classList.add('show')
    }, 400)
    count--
    countEL.innerHTML = count
    // 当抽奖次数为0的时候,就把剩余的奖项全部展示出来
    if (count === 0) {
      setTimeout(() => {
        box.querySelectorAll('.style1').forEach(v => {
          v.classList.add('hide')
          if (v.nextElementSibling.className.indexOf('show') === -1) {
            const item = noArr.pop()
            v.nextElementSibling.innerHTML = item.name
          }
        })
        setTimeout(() => {
          box.querySelectorAll('.style2').forEach(v => {
            if (v.className.indexOf('show') === -1) {
              v.classList.add('show-result')
            }
          })
        }, 400)
      }, 1000)
    }
  }
</script>

</html>

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现9宫格抽奖,可以使用Vue.js框架的component组件进行实现。具体步骤如下: 1. 创建一个九宫格的组件,可以通过table标签和tr、td标签组合实现: ``` <template> <table> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <td>4</td> <td>5</td> <td>6</td> </tr> <tr> <td>7</td> <td>8</td> <td>9</td> </tr> </table> </template> ``` 2. 在组件中定义数据,用于控制九宫格指针的指向,以及奖品的位置: ``` <script> export default { data() { return { pointer: 0, // 指针位置 prizeIndex: 4, // 奖品位置 rotating: false // 是否正在旋转 }; } }; </script> ``` 3. 在组件中定义方法,用于处理抽奖逻辑,例如开始抽奖、停止抽奖等: ``` <script> export default { data() { return { pointer: 0, // 指针位置 prizeIndex: 4, // 奖品位置 rotating: false // 是否正在旋转 }; }, methods: { start() { if (this.rotating) return; this.rotating = true; let interval = setInterval(() => { this.pointer = (this.pointer + 1) % 9; }, 100); setTimeout(() => { clearInterval(interval); this.rotating = false; if (this.pointer === this.prizeIndex) { alert("恭喜您中奖了!"); } else { alert("很遗憾,您没有中奖!"); } }, 3000); } } }; </script> ``` 4. 在组件中添加样式,用于实现宫格的样式,以及指针的指向: ``` <style scoped> table { width: 200px; height: 200px; border-collapse: collapse; } td { width: 30px; height: 30px; text-align: center; vertical-align: middle; border: 1px solid #ccc; } .active { background-color: orange; } .pointer { position: relative; &::before, &::after { content: ""; position: absolute; left: 50%; transform: translateX(-50%); width: 0; height: 0; border-style: solid; border-width: 0 10px 20px 10px; } &::before { top: -20px; border-color: transparent transparent orange transparent; } &::after { bottom: -20px; border-color: orange transparent transparent transparent; } } </style> ``` 5. 在组件中添加指针的样式类,并根据指针位置动态添加该样式类: ``` <template> <table> <tr> <td :class="{ active: pointer === 0 }">1</td> <td :class="{ active: pointer === 1 }">2</td> <td :class="{ active: pointer === 2 }">3</td> </tr> <tr> <td :class="{ active: pointer === 7 }">4</td> <td :class="{ active: pointer === 8 }">5</td> <td :class="{ active: pointer === 3 }">6</td> </tr> <tr> <td :class="{ active: pointer === 6 }">7</td> <td :class="{ active: pointer === 5 }">8</td> <td :class="{ active: pointer === 4 }">9</td> </tr> </table> </template> ``` 6. 在组件中添加开始抽奖按钮,以及绑定开始抽奖方法: ``` <template> <div> <table> ... </table> <button @click="start">开始抽奖</button> </div> </template> ``` 完整代码如下: ``` <template> <div> <table> <tr> <td :class="{ active: pointer === 0 }">1</td> <td :class="{ active: pointer === 1 }">2</td> <td :class="{ active: pointer === 2 }">3</td> </tr> <tr> <td :class="{ active: pointer === 7 }">4</td> <td :class="{ active: pointer === 8 }">5</td> <td :class="{ active: pointer === 3 }">6</td> </tr> <tr> <td :class="{ active: pointer === 6 }">7</td> <td :class="{ active: pointer === 5 }">8</td> <td :class="{ active: pointer === 4 }">9</td> </tr> </table> <button @click="start">开始抽奖</button> </div> </template> <script> export default { data() { return { pointer: 0, // 指针位置 prizeIndex: 4, // 奖品位置 rotating: false // 是否正在旋转 }; }, methods: { start() { if (this.rotating) return; this.rotating = true; let interval = setInterval(() => { this.pointer = (this.pointer + 1) % 9; }, 100); setTimeout(() => { clearInterval(interval); this.rotating = false; if (this.pointer === this.prizeIndex) { alert("恭喜您中奖了!"); } else { alert("很遗憾,您没有中奖!"); } }, 3000); } } }; </script> <style scoped> table { width: 200px; height: 200px; border-collapse: collapse; } td { width: 30px; height: 30px; text-align: center; vertical-align: middle; border: 1px solid #ccc; } .active { background-color: orange; } .pointer { position: relative; &::before, &::after { content: ""; position: absolute; left: 50%; transform: translateX(-50%); width: 0; height: 0; border-style: solid; border-width: 0 10px 20px 10px; } &::before { top: -20px; border-color: transparent transparent orange transparent; } &::after { bottom: -20px; border-color: orange transparent transparent transparent; } } </style> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值