随机数抽奖代码


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #wrap{
            text-align: center;
            width:500px;
            margin: 100px auto;
            position: relative;
        }
        #ul1{
            width: 303px;
            height: 303px;
            margin: 50px auto;
            padding:0;
            border-top:1px solid black;
            border-left: 1px solid black; 
        }
        #ul1 li{
            float: left;
            border-right: 1px solid black;
            border-bottom: 1px solid black;
            list-style: none;
            width: 100px;
            height: 100px;
            line-height: 100px;
            text-align: center;
        }
        #tooltips{
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
            position: absolute;
            top: 0;
            z-index: 999;
            display: none;
        }
        #info{
            width: 400px;
            height: 200px;
            background-color: white;
            margin: 150px auto;
        }
        #info .title{
            width: 100%;
            height: 40px;
            background-color: #009f95;
            line-height: 40px;
            color: white;
            padding-left: 20px;
            box-sizing: border-box;
        }
        #info .btn button{
            background-color: #009f95;
            color: white;
            outline: none;
            font-size: 10px;
            width:60px;
            height: 30px;
            margin-left: 300px;
        }
        #info .content {
            height: 120px;
            padding: 20px;
            box-sizing: border-box;
        }
    </style>
</head>
<body>
    <div id="wrap">
        <button id="btn">开始抽奖</button>
        <ul id="ul1">
            <li>鼠标</li>
            <li>1000万</li>
            <li>100优惠卷</li>
            <li>很遗憾</li>
            <li>键盘</li>
            <li>iphoneX</li>
            <li>很遗憾</li>
            <li>迪拜10日游</li>
            <li>很遗憾</li>
        </ul>
    </div>
    <!-- 提示信息 -->
    <div id="tooltips">
        <div id="info">
            <div class="title">信息</div>
            <div class="content">恭喜你,中奖了</div>
            <div class="btn">
                <button id="confirm">确定</button>
            </div>
        </div>
    </div>
    <script type="text/javascript">
            //思路: 1 实现红色背景切换  2 当运动停止,弹出对话框--用js去修改tooltips的display 变为block
            var oStart = document.getElementById("btn")
            var aLi = document.getElementsByTagName("li")
            //提示框
            var oTooltips = document.getElementById("tooltips")
            //确定按钮
            var oConfirm = document.getElementById("confirm")
            //当前li的下标
            var nowIndex = 0
            // setInterval的id
            var timer = null
            oStart.onclick = function(){
                //生成一个中奖数字
                var randomInt = getRandomInt(0, 8)
                console.log(randomInt)
                //下面的代码只是为了给用户一个感觉:正在抽奖
                timer = setInterval(function(){
                    for(var i = 0; i< aLi.length; i++){
                        aLi[i].style.backgroundColor = "white"
                    }
                    aLi[nowIndex].style.backgroundColor = "red"
                    nowIndex++
                    //randomInt表示中奖的数字,如果nowIndex和randomInt一样,我们就认为当前的li是抽中的奖品
                    if(nowIndex === randomInt){
                        clearInterval(timer)
                        oTooltips.style.display = "block"
                    }
                    
//                  nowIndex = nowIndex % aLi.length
                nowIndex %= aLi.length
                }, 500)
                //思考一个问题:什么时候停止?当中奖的时候停止, 抽中的是谁?
                //可以用随机数生成一个具体中奖的数字 randomInt
                
            }
            //当点击确定的时候,提示框消失
            oConfirm.onclick = function(){
                //让tooltips消失
                oTooltips.style.display = "none"
            }
            
            function getRandomInt(min, max){
            return Math.floor(Math.random()*(max - min + 1) + min)
        }
    </script>
</body>
</html>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM