前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >vue简单实现九宫格抽奖

vue简单实现九宫格抽奖

作者头像
明知山
发布2020-09-17 10:20:13
1.3K0
发布2020-09-17 10:20:13
举报
文章被收录于专栏:前端开发随笔前端开发随笔
在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>九宫格抽奖</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

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

        .draw {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 100px;
            height: 100px;
            background: chocolate;
            text-align: center;
            line-height: 100px;
            color: #fff;
            cursor: pointer;
        }

        .row {
            position: absolute;
            width: 100px;
            height: 100px;
            line-height: 100px;
            background: cadetblue;
            color: #fff;
            text-align: center;
        }

        .box .row:nth-child(1) {
            top: 0;
            left: 0;
        }

        .box .row:nth-child(2) {
            top: 0;
            left: 50%;
            transform: translate(-50%, 0);
        }

        .box .row:nth-child(3) {
            top: 0;
            right: 0;
        }

        .box .row:nth-child(4) {
            top: 50%;
            right: 0;
            transform: translate(0, -50%);
        }

        .box .row:nth-child(5) {
            bottom: 0;
            right: 0;
        }

        .box .row:nth-child(6) {
            bottom: 0;
            right: 50%;
            transform: translate(50%, 0);
        }

        .box .row:nth-child(7) {
            bottom: 0;
            left: 0;
        }

        .box .row:nth-child(8) {
            top: 50%;
            left: 0;
            transform: translate(0, -50%);
        }

        .active {
            background: lightseagreen !important;
        }
    </style>
</head>

<body>
    <div id="Vue">
        <div class="box">
            <div class="row" :class="select == index ? 'active' : ''" v-for="(item,index) in list" :key="index">
                {{item.name}}</div>
            <div class="draw" @click="startDraw()">抽奖</div>
        </div>
    </div>
</body>
<script src="https://cdn.suoluomei.com/common/js2.0/vue/v2.5.16/vue.js"></script>
<script>
    new Vue({
        el: "#Vue",
        data: {
            list: [
                {
                    name: "手机"
                },
                {
                    name: "平板"
                },
                {
                    name: "电磁炉"
                },
                {
                    name: "洗衣机"
                },
                {
                    name: "衣柜"
                },
                {
                    name: "电脑"
                },
                {
                    name: "电视"
                },
                {
                    name: "冰箱"
                },
            ],
            flag: true,//是否开始抽奖
            select: 0,//页面对应抽奖下标
            timer: "",
            count: 0
        },
        methods: {
            startDraw() {
                if (this.flag == true) {
                    this.flag = false
                    this.timer = setInterval(() => {
                        this.spinPrize()
                    }, 100)
                }
            },

            spinPrize() {
                var spin = 3 //旋转圈数
                var win = 5 //中奖产品 0-7
                this.select += 1
                if (this.select > 7 && this.count != spin) {
                    this.select = 0
                    this.count += 1
                }
                if (this.select == win && this.count == spin) {
                    clearInterval(this.timer)
                    this.flag = true
                    this.count = 0
                    console.log("中奖产品为:" + this.list[this.select].name)
                }
            },
        }
    })
</script>

</html>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档