基于javascript實(shí)現(xiàn)簡(jiǎn)單的抽獎(jiǎng)系統(tǒng)

字號(hào):


    這篇文章主要為大家詳細(xì)介紹了基于javascript實(shí)現(xiàn)簡(jiǎn)單的抽獎(jiǎng)系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    本文實(shí)例為大家分享了js實(shí)現(xiàn)簡(jiǎn)單的抽獎(jiǎng)系統(tǒng),供大家參考,具體內(nèi)容如下
    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="gb2312">
      <title>抽獎(jiǎng)活動(dòng)</title>
      <style>
    *{
     margin:0;padding:0;
    }
    #title{
     color:red;text-align:center;margin:0 auto;width:240px;height:70px;padding-top:10px;background:opacity(0);
    }
    .btns{
     width:190px;height:30px;margin:0px auto;
    }
    .btns span{
     display:block;float:left;width:80px;height:28px;text-align:center;background:#036;color:#fff;cursor:pointer;border:1px solid #eee;border-radius:8px;font-family:"微軟雅黑";font-size:14px;line-height:28px;margin-right:10px;
    }
    #txt{
     font-size:14px;color:#ccc999;text-align:center;margin:0 auto;width:190px;height:50px;padding-top:10px;
    }
      </style>
      <script>
     var mytype=["iPhone6s","iPad Air2","DELL外星人","鍵鼠套裝","1000元超市購(gòu)物卡","200元話費(fèi)充值卡","謝謝參與","品牌耳機(jī)","港澳臺(tái)7日游","50元優(yōu)惠券"],//定義獎(jiǎng)品池
       timer=null,
       count=0;
    //加載時(shí)觸發(fā)
     window.onload=function(){
     var start = document.getElementById("start");
     var stop = document.getElementById("stop");
     start.onclick=startFun;//這個(gè)函數(shù)后面加括號(hào),就直接調(diào)用了該函數(shù),所以不要加
     stop.onclick=stopFun;
     //綁定鍵盤(pán)事件
     document.onkeyup=function(e){
       e = e || window.event;
      if(e.keyCode==13){
        if(count==0){
        startFun();
        count=1;
        }
      else{  
        stopFun();
        count=0;
        }
      }
     }
     }
    //點(diǎn)擊開(kāi)始,標(biāo)題欄開(kāi)始輪動(dòng)
     function startFun(){
      clearInterval(timer);//開(kāi)始時(shí),清除計(jì)時(shí)器,避免二次觸發(fā)
      var title = document.getElementById("title");
      var start = document.getElementById("start");
      timer = setInterval(function(){
        var num= Math.floor(Math.random()*mytype.length);
        title.innerHTML=mytype[num];
      },50);
      start.style.background="#ccc";
     }
     //點(diǎn)擊停止,標(biāo)題欄停止輪動(dòng)并輸出輪動(dòng)結(jié)果
     function stopFun(){
      var start = document.getElementById("start"),
        txt = document.getElementById("txt"),
        title = document.getElementById("title");
      clearInterval(timer);//清除計(jì)時(shí)器,停止計(jì)時(shí)器
      start.style.background="#036";
     }
      </script>
    <body>
     <div>
     <h2 id="title">開(kāi)始抽獎(jiǎng)!</h2>
     </div>
     <div>
     <span id="start">開(kāi)始</span>
     <span id="stop">停止</span>
     </div>
     <div id="txt">支持回車(chē)鍵(Enter)開(kāi)始/停止。</div>
    </body>
    </html>
    效果圖:
    名單