下載: https://download.csdn.net/download/weixin_44893902/20366745
效果:

代碼:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>在線抽獎 隨機選取 自動挑選</title>
<script src="https://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
<style> body { background-color:aliceblue; } .wrapDiv { width:80%; max-width:1200px; margin:0 auto; text-align:center; position:absolute; top:80px; left:0; right:0; } .leftBox { float:left; width:800px; height:240px; margin:0 auto; margin-top:0px; clear:both; } #span { float:right; top:30px; right:185px; } #btn { float:left; width:100px; height:30px; margin-left:10px; margin-top:150px; } .nameBox { width:100px; height:30px; float:left; background-color:antiquewhite; margin-left:10px; margin-top:10px; text-align:center; line-height:30px; } .selectedName { float:right; width:340px; background:#666; margin-top:10px; margin-left:30px; background:#ffffff; overflow:hidden; } h1 { text-align:center; } </style>
</head>
<body>
<h1>隨機抽獎系統</h1>
<span id="span"></span>
<div class="wrapDiv">
<div id="leftBox" class="leftBox"></div>
<div id="selectedName" class="selectedName">
<h1>中獎者名單</h1>
</div>
<input type="button" id="btn" value="開始走起">
</div>
<script> var arr = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "22", "23", "24", "25", "26", "27", "28", "29", "30", ]; var orgArrCount = arr.length; var currentSelectNum = 0; initForm(); function initForm() { var selectedNameHeight = orgArrCount / 3 * 40 + 120; $("#selectedName").css("height", selectedNameHeight + "px"); dynamicCreateBox(); } function dynamicCreateBox() { for (var i = 0; i < arr.length; i++) { var div = document.createElement("div"); div.innerText = arr[i]; div.className = "nameBox"; $("#leftBox").append(div); }; } function clearBoxColor() { $("#leftBox").children("div").each(function() { $(this).css("background-color", ""); }); } function setBoxColor() { $("#leftBox").children("div").each(function() { var thisText = ($(this).text()); var selectedName = arr[currentSelectNum]; if (thisText == selectedName) { $(this).css("background-color", "red"); } }); } function appendSelectedName() { var div = document.createElement("div"); div.innerText = arr[currentSelectNum]; div.className = "nameBox"; $("#selectedName").append(div); } $('#btn').click(function() { var curentCount = arr.length; if (curentCount < 1) { alert("沒有可選人了"); clearBoxColor(); return; } if (this.value === "開始走起") { timeId = setInterval(function() { clearBoxColor(); var num = Math.floor(Math.random() * curentCount); currentSelectNum = num; setBoxColor(); }, 10); this.value = "停止"; } else { clearInterval(timeId); appendSelectedName(); arr.splice(currentSelectNum, 1); this.value = "開始走起"; } }); getTime(); setInterval(getTime, 10) function getTime() { var day = new Date(); var year = day.getFullYear(); var month = day.getMonth() + 1; var dat = day.getDate(); var hour = day.getHours(); var minitue = day.getMinutes(); var second = day.getSeconds(); month = month < 10 ? "0" + month : month; dat = dat < 10 ? "0" + dat : dat; hour = hour < 10 ? "0" + hour : hour; minitue = minitue < 10 ? "0" + minitue : minitue; second = second < 10 ? "0" + second : second; $("#span").innerText = year + "-" + month + "-" + dat + " " + hour + ":" + minitue + ":" + second } </script>
</body>
</html>