js實現(xiàn)可鍵盤控制的簡單抽獎程序
本文實例為大家分享了js抽獎程序的編寫代碼,以及編寫注意事項,感興趣的小伙伴們可以參考一下
代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>簡單抽獎(可用鍵盤)</title> <style> *{margin:0;padding:0;} .box{width: 400px;height: 300px;margin:50px auto;background: red} .title{color: #fff;font-size: 30px;font-weight:700px;padding: 50px 0;text-align: center;height:40px;} .btm{text-align: center;padding:20px 0;} .btm a{display: inline-block;width: 120px;height:60px;line-height: 60px;background: #FEF097;margin:0 10px;text-decoration: none;} </style> <script> var data=['Iphone','Ipad','筆記本','相機','謝謝參與','充值卡','購物券'], timer=null,//定時器 flag=0;//阻止多次回車 window.onload=function(){ var play=document.getElementById('play'), stop=document.getElementById('stop'); // 開始抽獎 play.onclick=playFun; stop.onclick=stopFun; // 鍵盤事件 document.onkeyup=function(event){ event = event || window.event; // 回車鍵的code值:13 if(event.keyCode==13){ if(flag==0){ playFun(); flag=1; }else{ stopFun(); flag=0; } } } function playFun(){ var title=document.getElementById('title'); var play=document.getElementById('play'); clearInterval(timer); timer=setInterval(function(){ var random=Math.floor(Math.random()*data.length); title.innerHTML=data[random]; },60); play.style.background='#999'; } function stopFun(){ clearInterval(timer); var play=document.getElementById('play'); play.style.background='#FEF097'; } } </script> </head> <body> <div class="box"> <div class="title" id="title">淘家趣抽獎</div> <div class="btm"> <a href="javascript:;" id="play">開始</a> <a href="javascript:;" id="stop">停止</a> </div> </div> </body> </html>
注意點:
1.隨機數(shù),取數(shù)組的其中一個;取0-n之間:Math.random()*(n+1)
2.定時器,開始抽獎時要停止前面的一次抽獎,不然會定時器重疊
3.按鍵操作,要判斷是抽獎進行中,還是未開始,所有設置了變量 flag
想要學習更多關于javascript抽獎功能,請參考此專題:javascript實現(xiàn)抽獎功能
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- js組件SlotMachine實現(xiàn)圖片切換效果制作抽獎系統(tǒng)
- javascript HTML5 Canvas實現(xiàn)圓盤抽獎功能
- 基于javascript實現(xiàn)簡單的抽獎系統(tǒng)
- javascript實現(xiàn)可鍵盤控制的抽獎系統(tǒng)
- 基于JavaScript實現(xiàn)簡單的隨機抽獎小程序
- JS實現(xiàn)轉動隨機數(shù)抽獎特效代碼
- js實現(xiàn)簡易的單數(shù)字隨機抽獎(0-9)
- js和html5實現(xiàn)手機端刮刮卡抽獎效果完美兼容android/IOS
- JS模擬抽獎序效果實現(xiàn)代碼
- js抽獎實現(xiàn)隨機抽獎代碼效果
相關文章
深入淺析JSON.parse()、JSON.stringify()和eval()的作用詳解
這篇文章主要介紹了深入淺析JSON.parse()、JSON.stringify()和eval()的作用詳解的相關資料,需要的朋友可以參考下2016-04-04淺談bootstrap源碼分析之scrollspy(滾動偵聽)
下面小編就為大家?guī)硪黄獪\談bootstrap源碼分析之scrollspy(滾動偵聽)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06JavaScript中rxjs與?Observable?兩大類操作符解析
這篇文章主要介紹了JavaScript中rxjs與?Observable?兩大類操作符解析,運算符是對?Observable?進行操作并返回?Observable?的函數(shù),文章圍繞主題展開詳細內(nèi)容,需要的小伙伴可以參考一下2022-07-07window.location的重寫及判斷l(xiāng)ocation是否被重寫
這篇文章主要介紹了window.location的重寫及判斷l(xiāng)ocation是否被重寫,需要的朋友可以參考下2014-09-09javascritp添加url參數(shù)將參數(shù)加入到url中
javascritp添加url參數(shù)方法,將參數(shù)加入到url中,如果原來url中有則覆蓋,下面是示例代碼,感興趣的朋友可以參考下2014-09-09