HTML + CSS + JS 實現隨機抽獎

HTML + CSS + JS 實現隨機抽獎

HTML

<!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>
		<button id="btn2">停止</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" id="content-z">恭喜你,中獎了</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 oStop = document.getElementById("btn2")
		var oTooltips = document.getElementById("tooltips")
		var oConfirm = document.getElementById("confirm")
		var oContentz = document.getElementById("content-z")
		var nowIndex = 0
		var timer = null
		oStart.onclick = function(){
			clearInterval(timer)
			timer = setInterval(function(){
				for(var i = 0;i<aLi.length;i++){
					aLi[i].style.backgroundColor = "white"
				}
				aLi[nowIndex].style.backgroundColor = "red"
				nowIndex = (nowIndex+1) % (aLi.length)
			},10)
		}
		oStop.onclick = function(){
			clearInterval(timer)
			oTooltips.style.display = "block"
			var zhong_jiang = aLi[nowIndex].innerText
			console.log(zhong_jiang)
			if(zhong_jiang == "很遺憾"){
				oContentz.innerText = "很遺憾,沒有中獎"
			}else{
				oContentz.innerText = "恭喜你,抽中了" + zhong_jiang + "!" 
			}
		}
		oConfirm.onclick = function(){
			oTooltips.style.display = "none"
			for(var i = 0;i<aLi.length;i++){
				aLi[i].style.backgroundColor = "white"
			}
		}
	</script>
</body>
</html>

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章