JS實(shí)現(xiàn)轉(zhuǎn)動(dòng)隨機(jī)數(shù)抽獎(jiǎng)特效代碼
大家都玩過抽獎(jiǎng)游戲,或者夢想抽到大獎(jiǎng)吧,但是有沒有想過抽獎(jiǎng)游戲是怎么實(shí)現(xiàn)的吶?今天就給大家分享一款轉(zhuǎn)動(dòng)隨機(jī)數(shù)抽獎(jiǎng)的JS特效代碼。
實(shí)現(xiàn)代碼如下
<!Doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <script src="http://code.jquery.com/jquery-1.7.1.js" type="text/javascript"></script> <style type="text/css"> body,h1,h2,h3,h4,h5,h6,p,ul,ol,li,form,img,dl,dt,dd,table,th,td,blockquote,fieldset,div,strong,label,em{margin:0;padding:0;border:0;} ul,ol,li{list-style:none;} input,button{margin:0;font-size:12px;vertical-align:middle;} body{font-size:12px;font-family:Arial, Helvetica, sans-serif; text-align:center; margin:0 auto; background:url(../images/BG.jpg) repeat-y;} table{border-collapse:collapse;border-spacing:0;} .box{ padding:9px 0 0 11px; width:759px; height:611px; background:#ccc; margin:20px auto; } .inbox{ width:751px; height:596px;position:relative;} #random_box li{ position:absolute; width:144px; overflow:hidden; height:144px; border:3px solid #c7c5ca; background:#fff;} #random_box li img{ width:144px; display:block; height:144px;} .ok{ display:block; width:442px; height:294px; background:#c00; color:#fff; font-size:48px; position:absolute; top:149px; left:148px; cursor:pointer; text-align:center; line-height:280px;} #random_1{ left:0; top:0;} #random_2{ left:148px;top:0;} #random_3{ left:296px;top:0;} #random_4{ left:444px;top:0;} #random_5{ left:592px;top:0; } #random_6{ left:592px;top:148px;} #random_7{ left:592px; top:298px;} #random_8{ left:592px; top:446px;} #random_9{ left:444px; top:446px;} #random_10{ left:296px; top:446px;} #random_11{ left:148px; top:446px;} #random_12{ left:0; top:446px;} #random_13{ left:0; top:298px;} #random_14{ left:0; top:148px;} #random_box li.random_current{border:1px solid red;} </style> </head> <body> <div class="box"> <div class="inbox"> <ul id="random_box"> <li id="random_1">1</li> <li id="random_2">2</li> <li id="random_3">3</li> <li id="random_4">4</li> <li id="random_5">5</li> <li id="random_6">6</li> <li id="random_7">7</li> <li id="random_8">8</li> <li id="random_9">9</li> <li id="random_10">10</li> <li id="random_11">11</li> <li id="random_12">12</li> <li id="random_13">13</li> <li id="random_14">14</li> </ul> <b class="ok" onClick="StartGame()">抽獎(jiǎng)</b> </div> </div> <script> var index=1, //當(dāng)前亮區(qū)位置 prevIndex=14, //前一位置 Speed=300, //初始速度 Time, //定義對象 arr_length = 14; //GetSide(5,5), //初始化數(shù)組 EndIndex=1, //決定在哪一格變慢 cycle=0, //轉(zhuǎn)動(dòng)圈數(shù) EndCycle=3, //計(jì)算圈數(shù) flag=false, //結(jié)束轉(zhuǎn)動(dòng)標(biāo)志 random_num=1, //中獎(jiǎng)數(shù) quick=0; //加速 function StartGame(){ $("#random_box li").removeClass("random_current"); //取消選中 //random_num = parseInt($("#txtnum").val());// random_num = Math.floor(Math.random()*13+2); //產(chǎn)出隨機(jī)中獎(jiǎng)數(shù)2--12之間 index=1; //再來一次,從1開始 cycle=0; flag=false; //EndIndex=Math.floor(Math.random()*12); if(random_num>5) { EndIndex = random_num - 5; //前5格開始變慢 } else { EndIndex = random_num + 14 - 5; //前5格開始變慢 } //EndCycle=Math.floor(Math.random()*3); Time = setInterval(Star,Speed); } function Star(num){ //跑馬燈變速 if(flag==false){ //走五格開始加速 if(quick==5){ clearInterval(Time); Speed=50; Time=setInterval(Star,Speed); } //跑N圈減速 if(cycle==EndCycle+1 && index-1==EndIndex){ clearInterval(Time); Speed=300; flag=true; //觸發(fā)結(jié)束 Time=setInterval(Star,Speed); } } if(index>arr_length){ index=1; cycle++; } //結(jié)束轉(zhuǎn)動(dòng)并選中號碼 if(flag==true && index==parseInt(random_num)){ quick=0; clearInterval(Time); } $("#random_"+index).addClass('random_current'); //設(shè)置當(dāng)前選中樣式 if(index>1) prevIndex=index-1; else{ prevIndex=arr_length; } $("#random_"+prevIndex).removeClass('random_current'); //取消上次選擇樣式 index++; quick++; } </script> </body> </html>
實(shí)現(xiàn)的效果圖:
現(xiàn)在抽獎(jiǎng)游戲應(yīng)用的非常廣泛,我們每個(gè)人都會接觸到,尤其是節(jié)假日的時(shí)候各大商場都會組織這種類型的游戲活動(dòng)吸引顧客,我上面分享的這段代碼其實(shí)還有很多可以改進(jìn),豐富的地方,比如我們可以美化它的界面,功能更加豐富,還有我們可以考慮轉(zhuǎn)動(dòng)隨機(jī)數(shù)抽獎(jiǎng)的速度,決定在哪一格變慢等。
以上就是本文的全部內(nèi)容,希望大家可以喜歡,可以動(dòng)手實(shí)踐一下。
- js HTML5 Canvas繪制轉(zhuǎn)盤抽獎(jiǎng)
- js輪盤抽獎(jiǎng)實(shí)例分析
- javascript+HTML5 Canvas繪制轉(zhuǎn)盤抽獎(jiǎng)
- 基于javascript實(shí)現(xiàn)簡單的抽獎(jiǎng)系統(tǒng)
- js實(shí)現(xiàn)網(wǎng)頁抽獎(jiǎng)實(shí)例
- js和html5實(shí)現(xiàn)手機(jī)端刮刮卡抽獎(jiǎng)效果完美兼容android/IOS
- js抽獎(jiǎng)實(shí)現(xiàn)隨機(jī)抽獎(jiǎng)代碼效果
- JS模擬抽獎(jiǎng)序效果實(shí)現(xiàn)代碼
- js實(shí)現(xiàn)大轉(zhuǎn)盤抽獎(jiǎng)游戲?qū)嵗?/a>
- 原生JS實(shí)現(xiàn)九宮格抽獎(jiǎng)效果
相關(guān)文章
js登錄滑動(dòng)驗(yàn)證的實(shí)現(xiàn)(不滑動(dòng)無法登陸)
下面小編就為大家分享一篇js登錄滑動(dòng)驗(yàn)證的實(shí)現(xiàn)(不滑動(dòng)無法登陸),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01JS實(shí)現(xiàn)排行榜文字向上滾動(dòng)輪播效果
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)排行榜文字向上滾動(dòng)輪播效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11JS實(shí)現(xiàn)導(dǎo)出Excel和CSV文件操作
這篇文章介紹了JS實(shí)現(xiàn)導(dǎo)出Excel和CSV文件的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05