首页>>Jquery文字>>Jquery九宫格抽奖(2021-05-24)

Jquery九宫格抽奖

Jquery九宫格抽奖
赞赏支持
立刻微信赞赏支持 关闭

 

XML/HTML Code
  1. <div id="lottery">  
  2.     <table border="0" cellpadding="0" cellspacing="0">  
  3.         <tr>  
  4.             <td class="lottery-unit lottery-unit-0"><img src="img/1.jpg"><div class="mask"></div></td>  
  5.             <td class="lottery-unit lottery-unit-1"><img src="img/2.jpg"><div class="mask"></div></td>  
  6.             <td class="lottery-unit lottery-unit-2"><img src="img/3.jpg"><div class="mask"></div></td>  
  7.         </tr>  
  8.         <tr>  
  9.             <td class="lottery-unit lottery-unit-7"><img src="img/4.jpg"><div class="mask"></div></td>  
  10.             <td><a href="#"></a></td>  
  11.             <td class="lottery-unit lottery-unit-3"><img src="img/5.jpg"><div class="mask"></div></td>  
  12.         </tr>  
  13.         <tr>  
  14.             <td class="lottery-unit lottery-unit-6"><img src="img/6.jpg"><div class="mask"></div></td>  
  15.             <td class="lottery-unit lottery-unit-5"><img src="img/7.jpg"><div class="mask"></div></td>  
  16.             <td class="lottery-unit lottery-unit-4"><img src="img/8.jpg"><div class="mask"></div></td>  
  17.         </tr>  
  18.     </table>  
  19. </div>  

 

JavaScript Code
  1. <script type="text/javascript">  
  2.     var lottery={  
  3.         index:-1,    //当前转动到哪个位置,起点位置  
  4.         count:0,    //总共有多少个位置  
  5.         timer:0,    //setTimeout的ID,用clearTimeout清除  
  6.         speed:20,    //初始转动速度  
  7.         times:0,    //转动次数  
  8.         cycle:50,    //转动基本次数:即至少需要转动多少次再进入抽奖环节  
  9.         prize:-1,    //中奖位置  
  10.         init:function(id){  
  11.             if ($("#"+id).find(".lottery-unit").length>0) {  
  12.                 $lottery = $("#"+id);  
  13.                 $units = $lottery.find(".lottery-unit");  
  14.                 this.obj = $lottery;  
  15.                 this.count = $units.length;  
  16.                 $lottery.find(".lottery-unit-"+this.index).addClass("active");  
  17.             };  
  18.         },  
  19.         roll:function(){  
  20.             var index = this.index;  
  21.             var count = this.count;  
  22.             var lottery = this.obj;  
  23.             $(lottery).find(".lottery-unit-"+index).removeClass("active");  
  24.             index += 1;  
  25.             if (index>count-1) {  
  26.                 index = 0;  
  27.             };  
  28.             $(lottery).find(".lottery-unit-"+index).addClass("active");  
  29.             this.index=index;  
  30.             return false;  
  31.         },  
  32.         stop:function(index){  
  33.             this.prize=index;  
  34.             return false;  
  35.         }  
  36.     };  
  37.   
  38.     function roll(){  
  39.         lottery.times += 1;  
  40.         lottery.roll();//转动过程调用的是lottery的roll方法,这里是第一次调用初始化  
  41.         if (lottery.times > lottery.cycle+10 && lottery.prize==lottery.index) {  
  42.             clearTimeout(lottery.timer);  
  43.             lottery.prize=-1;  
  44.             lottery.times=0;  
  45.             click=false;  
  46.         }else{  
  47.             if (lottery.times<lottery.cycle) {  
  48.                 lottery.speed -= 10;  
  49.             }else if(lottery.times==lottery.cycle) {  
  50.                 var index = Math.random()*(lottery.count)|0;//中奖物品通过一个随机数生成  
  51.                 lottery.prize = index;          
  52.             }else{  
  53.                 if (lottery.times > lottery.cycle+10 && ((lottery.prize==0 && lottery.index==7) || lottery.prize==lottery.index+1)) {  
  54.                     lottery.speed += 110;  
  55.                 }else{  
  56.                     lottery.speed += 20;  
  57.                 }  
  58.             }  
  59.             if (lottery.speed<40) {  
  60.                 lottery.speed=40;  
  61.             };  
  62.             //console.log(lottery.times+'^^^^^^'+lottery.speed+'^^^^^^^'+lottery.prize);  
  63.             lottery.timer = setTimeout(roll,lottery.speed);//循环调用  
  64.         }  
  65.         return false;  
  66.     }  
  67.   
  68.     var click=false;  
  69.   
  70.     window.onload=function(){  
  71.         lottery.init('lottery');  
  72.         $("#lottery a").click(function(){  
  73.             if (click) {//click控制一次抽奖过程中不能重复点击抽奖按钮,后面的点击不响应  
  74.                 return false;  
  75.             }else{  
  76.                 lottery.speed=100;  
  77.                 roll();    //转圈过程不响应click事件,会将click置为false  
  78.                 click=true//一次抽奖完成后,设置click为true,可继续抽奖  
  79.                 return false;  
  80.             }  
  81.         });  
  82.     };  
  83. </script>  

 


原文地址:http://www.freejs.net/article_jquerywenzi_906.html