*/ var haveprize=0; $(function(){ $(".plateBox").css("transform-origin","50% 50%"); $(".plateBox").css("transform","rotate(0deg)"); //添加抽奖事件 addevent(); }) /*开始抽奖函数,参数为中奖编号*/ function plateBoxPlay(code){ deleteevent();//暂时删除抽奖事件 //设置角度间隔 var $plateBox=$(".plateBox"); var angle=360/8; var thiscode=code-1; //生成随机圈数 var number_turns=Math.ceil(Math.random()*5); //获取修正角度 var thisspare=getdeg($plateBox)%360; //计算旋转角度 var this_turns=parseInt(number_turns*360-thiscode*angle-thisspare); //调用动画 play_animate($plateBox,this_turns,code); } //获取当前转盘角度 function getdeg($box){ var style=$box[0].style.transform; console.log(style); //角度取值 var thisdeg=parseInt(style.match(/rotate\(([^)]+)\)/)[1]); return thisdeg; } //抽奖动画 function play_animate($box,turns,code){ //动画初始化 var thisdeg=getdeg($box); var targetturns=thisdeg+turns; var timer=setInterval(function(){ var thisdeg=getdeg($box); //设置速度 var speed=Math.ceil((targetturns-thisdeg)/10); //本次旋转角度 var playdeg=thisdeg+speed; //设置角度 $box.css("transform","rotate("+playdeg+"deg)"); //console.log(playdeg); //判定结束 if(thisdeg>=targetturns){ clearInterval(timer); alert("您抽中了"+code+"等奖!"); addevent();//重新添加抽奖事件 } },100);
} //添加事件 function addevent(){ $(".handBox").on("click",function(){ //设置抽奖次数,0为不限次 var runnumber=0; if(runNumb(runnumber)){ //执行抽奖函数 plateBoxPlay(prize()); }else{ alert("对不起!您今天已经抽了"+runnumber+"次了,请明天再试!"); } }) } //删除事件 function deleteevent(){ $(".handBox").off("click"); }
//每天限制次数函数,参数为限制的次数 function runNumb(nmb){ //获取日期 var thisdate=getdate(); //返回值 var code=true; //抽奖记录 var thisRuncode={date:thisdate,runcode:0} //判断是否有本地数据 if(window.localStorage["runcode"]){ //数据转换 var Runcode=JSON.parse(window.localStorage["runcode"]); //判断是否当天 if(Runcode.date==thisdate){ if(nmb==0){ }else if(Runcode.runcode>=nmb){ code=false; }else{ thisRuncode={date:thisdate,runcode:Runcode.runcode+1}; addlocalStorage(thisRuncode); } }else{ addlocalStorage(thisRuncode); } }else{ addlocalStorage(thisRuncode); } return code; } //本地存储函数 function addlocalStorage(thisRuncode){ window.localStorage["runcode"]=JSON.stringify(thisRuncode); } //获取日期函数 function getdate(){ var thisdate=new Date(); var thisdy=thisdate.getYear()+thisdate.getMonth()+1+thisdate.getDate(); return thisdy; } //随机抽奖刷新一次页面只能抽一次1等奖,返回值为奖项序号 function prize(){ var thisprize=Math.ceil(Math.random()*8); if(thisprize==1){ if(haveprize==thisprize){ prize(); }else{ haveprize=thisprize; return thisprize; } }else{ return thisprize; } }