温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

原生js怎么实现简易抽奖系统

发布时间:2022-03-09 13:47:04 来源:亿速云 阅读:117 作者:iii 栏目:开发技术

这篇文章主要讲解了“原生js怎么实现简易抽奖系统”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“原生js怎么实现简易抽奖系统”吧!

效果图

原生js怎么实现简易抽奖系统

原理:

其实这里的原理就是通过按钮的状态是开始抽奖还是停止 如果i=ture 那就触发定时器 每50毫秒更换一次中奖的内容。如果i=false,那就清除定时器,显示最后的抽奖结果

下面我给大家画了个更直观的图

原生js怎么实现简易抽奖系统

HTML结构与样式

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        h3 {
            font-weight: normal;
        }

        .box {
            width: 450px;
            height: auto;
            background: #fff;
            border-radius: 3px;
            margin: 50px auto;
            padding-bottom: 1em;
            box-shadow: 0 10px 10px 0 rgba(0, 0, 0, 0.2), 0 5px 15px 0 rgba(0, 0, 0, 0.19);
        }

        .header {
            width: 100%;
            height: auto;
            padding: 0.5em 0.8em;
            border-bottom: 1px solid #ccc;
            box-sizing: border-box;
        }

        .body {
            width: 100%;
            height: auto;
            text-align: center;
        }

        .body:after {
            content: "";
            display: block;
            clear: both;
        }

        .body > div {
            width: 180px;
            margin: 0 auto;
        }

        .body > div > span {
            padding-top: 1em;
            float: left;
        }

        #tip {
            display: none;
        }

        .footer {
            width: 180px;
            height: 30px;
            background: #2ab8ff;
            line-height: 30px;
            text-align: center;
            margin: 1em auto;
            color: #ccc;
            border: 1px solid #2193cc;
            border-radius: 3px;
            cursor: pointer;
        }

        .footer:hover {
            background: #4ec1fb;
        }

    </style>
</head>
<body>
    <div class="box">
        <div class="header">
            <h3>简易抽奖系统</h3>
        </div>
        <div class="body">
            <div>
                <span id="tip">恭喜你!获得:</span>
                <span id="put"></span>
            </div>
        </div>
        <div class="footer">
            点击抽奖
        </div>
</div>

js代码

<script>
        /* 获取按钮 */
        var btn = document.querySelector('.footer');
        /* 获取提示的标签 */
        var tip = document.querySelector('#tip');
        /* 获取要输出的标签 */
        var put = document.querySelector('#put');
        /* 定义中奖的项目 */
        var gift = ['QQ会员','黄钻','绿钻','黑钻','紫钻','红钻','蓝钻','钻皇'];
        /* 定义i==true 用于判断 */
        var i = true;
        /* 定义定时器 */
        var Timer;
        var n = 0;

        btn.onclick=function() {
            if (i == true) {
                btn.style.background = '#f1516c';
                btn.style.borderColor = '#db2745';
                tip.style.display = 'block';
                Timer = setInterval(function() {
                    n++;
                    if (n == gift.length) {
                        n = 0;
                    }
                    put.innerHTML = gift[n];
                },50)
                btn.innerHTML = '停止';
                i = false;
            }else {
                btn.style.background = '#2ab8ff';
                btn.style.borderColor = '#2193cc';
                clearInterval(Timer);
                btn.innerHTML = '开始抽奖';
                i = true;
            }
        }
</script>

感谢各位的阅读,以上就是“原生js怎么实现简易抽奖系统”的内容了,经过本文的学习后,相信大家对原生js怎么实现简易抽奖系统这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

js
AI