用jQuery实现9宫格抽奖

1.布局

<div class="box">
    <div class="small s1"></div>
    <div class="small s2"></div>
    <div class="small s3"></div>
    <div class="small s4"></div>
    <div class="small s5"></div>
    <div class="small s6"></div>
    <div class="small s7"></div>
    <div class="small s8"></div>
    <div class="s9 start">
        开始
    </div>
</div>
 <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            position: relative;
            width: 260px;
            height: 260px;
            margin: 0 auto;
        }

        .small {
            position: absolute;
            width: 80px;
            height: 80px;
            box-sizing: border-box;
            background-color: #10a0ff;
            text-align: center;
            line-height: 80px;
            color: yellow;
        }

        .s1 {
            left: 0;
            top: 0;
        }

        .s2 {
            left: 90px;
            top: 0;
        }

        .s3 {
            left: 180px;
            top: 0;
        }

        .s4 {
            left: 180px;
            top: 90px;
        }

        .s5 {
            left: 180px;
            top: 180px;
        }

        .s6 {
            left: 90px;
            top: 180px;
        }

        .s7 {
            left: 0px;
            top: 180px;
        }

        .s8 {
            left: 0px;
            top: 90px;
        }

        .s9 {
            left: 90px;
            top: 90px;
        }

        .start {
            color: #3667d6;
            text-align: center;
            line-height: 80px;
            position: absolute;
            width: 80px;
            height: 80px;
            box-sizing: border-box;
            background-color: orange;
        }

        .add {
            background-color: deeppink;
        }
    </style>

效果图如下
在这里插入图片描述
添加动画效果,并保证每次点击开始就会随机一个数字

<script src="js/jquery-1.9.1.js"></script>
<script>
    var arr = ["洗衣机", "电饭煲", "谢谢参与", "电视机", "冰箱", "空调", "谢谢参与", "笔记本"];
    var speed = 60;
    var num = -1;
    var count = 0
    var time = null;
    $(".small").each(function (index) {
        $(this).text(arr[index]);
    });
    $(".start").on("click", function () {
        var times = parseInt(Math.random() * 16 + 24) + parseInt(Math.random() * 8);
        $(this).off();
        $(".small").removeClass("add");
        time = setInterval(function () {
            num++;
            count++;
            if (num > 7) {
                num = 0;
                $(".small").eq(0).addClass("add");
                $(".small").eq(7).removeClass("add");
            }
            else {
                $(".small").eq(num).addClass("add");
                $(".small").eq(num - 1).removeClass("add");
            }
            if (count > times) {
                clearInterval(time);
                setTimeout(function () {
                    alert(arr[num]);
                }, 0);
            }
        }, speed);
    });
</script>

实现效果
在这里插入图片描述

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值