js實現簡單抽獎功能
阿新 • • 發佈:2020-09-09
本文例項為大家分享了js實現簡單抽獎功能的具體程式碼,供大家參考,具體內容如下
程式碼
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style> #box{ border: 1px solid aqua; height: 100px; width: 200px; text-align: center; line-height: 100px; margin: auto; font-size: 22px; } .active{ background: slateblue; } li{ text-align: center; list-style:none ; width: 40px; height: 20px; line-height: 20px; border: 1px solid blue; margin-left: 40px; float: left; } ul,li { list-style: none; } #shu { margin: auto; } </style> </head> <body> <button id="stp">開始</button><button id="off">停止</button> <div id="box">恭喜<span>1</span>號 </div> <div id="shu">0</div><span>等獎</span> <ul> <li class="active">1號</li> <li>2號</li> <li>3號</li> <li>4號</li> <li>5號</li> <li>6號</li> <li>7號</li> </ul> <script> var t=null; var ostp=document.getElementById("stp"); var ooff=document.getElementById("off"); var ospan=document.getElementsByTagName("div")[0].getElementsByTagName("span"); var oli=document.getElementsByTagName("ul")[0].getElementsByTagName("li"); var oshu=document.getElementsByClassName("shu"); function mytime(){ var n=Math.floor(Math.random()*7+1); ospan[0].innerText=n; ospan[1].innerText=oli[n-1].innerText; for(let i=0;i<oli.length;i++) { oli[i].className=""; } oli[n-1].className="active"; } function mytime2() { var a=Math.floor(Math.random()*3+1); shu.innerText=a; } ostp.onclick=function(){ clearInterval(t); t=setInterval(mytime,50); at=setInterval(mytime2,1000) } ooff.onclick=function(){ clearInterval(t); clearInterval(at); } </script> </body> </html>
效果
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。