上海千语创想科技有限公司
 175-2108-6175
网站建设资讯详细

100行前端代码实现九宫格抽奖功能

日期:2020-09-23  作者:千语创想  浏览:6672

话不多说,直接上效果:



主要流程为:


1. 根据效果图,构建静态页面


2. 获取元素(自带的属性)


3. 绑定事件


4. 事件触发之后


4.1 所有的li元素 在指定的时间间隔下 颜色随机变化


4.2 延时器 2秒后 清除定时器


4.3 在清除定时器之后,所有的li背景色复位,随机选一个


代码实现过程如下:




<!DOCTYPE html>

<html>

<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>抽奖</title>

    <style>

        * {

            margin: 0;

            padding: 0;

            list-style: none;

        }


        #box {

            width: 240px;

            margin: 30px auto;

            border: 1px solid #ccc;

        }


        ul li {

            width: 60px;

            height: 60px;

            line-height: 30px;

            text-align: center;

            margin: 10px;

            float: left;

            background-color: orange;

            color: white;

        }

        /* 清浮动 */

        ul:after {

            content: "";

            display: block;

            clear: both;

        }


        p {

            margin: 20px auto;

            text-align: center;

        }


        .btn {

            width: 100px;

            height: 40px;

            line-height: 40px;

            text-align: center;

            border: none;

            background-color: skyblue;

        }

    </style>

</head>


<body>

    <div id="box">

        <ul>

            <li>礼品1</li>

            <li>礼品2</li>

            <li>礼品3</li>

            <li>礼品4</li>

            <li>礼品5</li>

            <li>礼品6</li>

            <li>礼品7</li>

            <li>礼品8</li>

            <li>礼品9</li>

        </ul>

        <p><button onclick="alert(123)" title="按钮">开始抽奖</button></p>


    </div>


</body>

<script>

    var btn = document.getElementsByTagName("button")[0];

    btn.className = "btn";

    // 通过标签名 获取元素

    var lis = document.getElementsByTagName("li"); 

    // 为元素绑定单击事件

    btn.onclick = function () {

        // 禁用按钮

        btn.disabled = true;

        var timer = setInterval(function () {

            for (var i = 0; i < lis.length; i++) {

                lis[i].style.backgroundColor = getColor();

            }

        }, 100);

        // 使用延时器 清除定时器

        setTimeout(function (){

            clearInterval(timer)

            for (var i = 0; i < lis.length; i++) {

                lis[i].style.backgroundColor = "orange"

            }

            var index = Math.floor( Math.random() * lis.length )

            lis[index].style.backgroundColor = "red";

            // 启用按钮

            btn.disabled = false;

            

        }, 2000)

    }

    // 随机颜色的函数

    function getColor() {

        return "#" + Math.random().toString(16).substr(2, 6);

    }


</script>

</html>


来千语创想移动应用开发平台学习更多APP开发知识:app开发app制作app开发源码下载app开发框架app制作模板等免费获取。千语创想支持APP定制开发,一键网站自动生成app网站转app网站封装app,有网站即可生成自己的app。

 

立即免费在线制作一个APP,新手注册即送开发大礼包

提交app定制需求,免费获取报价和周期:

电脑请点击https://www.qianyuthink.com/?url=/index?uzchannel=500

手机请点击https://www.qianyuthink.com/?url=/m/quickaddcustom



转载请注明来自:https://www.qianyuthink.com/news/6313.html

填写您的项目需求给我们

或者直接拨打 7×12小时一对一咨询电话

175 2108 6175

请填写需求信息,我们会在10分钟内与您取得联系

请认真填写需求信息,我们会在10分钟内与您取得联系

×
客服二维码
咨询技术总监
175-2108-6175
客服二维码
技术总监微信
客服二维码