本文實例為大家分享了JavaScript實現隨機點名器效果圖的具體代碼,供大家參考,具體內容如下
js實現隨機點名器的思路
利用setTimeout()計時器實現隨機的效果,功能函數
function show(){ var box=window.document.getElementById("box"); var num=Math.floor((Math.random()*100000))%namelist.length; box.innerHTML=namelist[num]; mytime=setTimeout("show()",1); }
完整代碼如下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>點擊按鈕隨機點名-樣式布局參考</title> <style type="text/css"> #box { margin: auto; width: 660px; font-size: 66px; height: 94px; color: #138eee; text-align: center; margin-top: 200px; } #bt { margin: auto; width: 200px; text-align: center; margin-top: 75px; color: #fff; font-size: 25px; cursor: pointer; } </style> <script type="text/javascript"> var namelist=["張三","李四","王五","趙六","孫七"]; var mytime=null; function doit(){ var bt=window.document.getElementById("bt"); if(mytime==null){ bt.innerHTML="停止"; show(); } else{ bt.innerHTML="開始點名"; clearTimeout(mytime); mytime=null; } } function show(){ var box=window.document.getElementById("box"); var num=Math.floor((Math.random()*100000))%namelist.length; box.innerHTML=namelist[num]; mytime=setTimeout("show()",1); } </script> </head> <body id="bodybj" > <div id="box">親,準備好點名了嗎?</div> <div id="bt" onclick="doit()">開始點名</div> </body> </html>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。