抽奖程序

/*
* @Author: winyh
* @Date: 2017-09-26 10:06:27
* @Last Modified by: winyh
* @Last Modified time: 2017-09-28 11:47:24
*/
$(function(){
var draw = null;
var prizeDeg = 0;
//进页面初始化
$.ajax({
type: 'GET',
url: '/get_prize.php',
success: function(data) {
var arr = data;
var stak = 8-arr.length;
var sort = [];
if(arr.length<8){
for(var i=0;i<stak;i++){
var to = 2*(i+1)-1;
sort.push(to);
var data = {"id":(arr.length+1).toString(),"prize_name":"谢谢参与"}
arr.splice(sort[i],0,data)
}
}
$(".write .name").each(function(index,e){
$(this).text(arr[index].prize_name);
})
},
error: function(data) {
console.log(data)
}
});

//进入页面初始化跑马灯偶数位高亮
$(".horse .dot").each(function(index,e){
if (index%2 ==0) {
$(this).addClass("active");
}
})

//跑马灯切换效果
function Horse(){
$(".horse .dot").each(function(index,e){
if($(this).hasClass("active")){
$(this).removeClass("active");
}else{
$(this).addClass("active");
}
})
}

function Draw(){
var hs = 200; //跑马灯切换速度ms
var cc = 5; //转盘旋转圈数
var init = 22.5+prizeDeg; //中奖角度
// 一等奖135 二等奖 45 三等奖 -45 四等奖 -135 特等奖 -180
var deg = 360*cc+init+'deg'; //旋转总角度
var time = 2000; //旋转总时间ms
var fn = 'ease'; //运动函数

var timer = setInterval(function(){
Horse();
},hs);

$(".wheel").animate({
rotate:deg
}, time , fn ,function(){
clearInterval(timer);
if(draw.status && draw.data.prize_id){
//中奖弹出奖励
//alert(draw.data.prize_name);
setTimeout(function(){
$(".start").attr('disabled', false);
location.href="../views/success.html";
},500)
}

if(draw.status && draw.data.prize_id == 0){
//未中奖
setTimeout(function(){
$(".start").attr('disabled', false);
location.href="../views/fail.html";
},500)
//alert("未中奖,谢谢参与");
}
})
}

//获取抽奖数据
function getDraw(){
var info = JSON.parse(localStorage.getItem("info"));
var name = info.name;
var mobile = info.mobile;
$.ajax({
type: 'POST',
url: '/start_winning.php',
data:{"name":name,"mobile":mobile},
success: function(data) {
localStorage.setItem("draw", JSON.stringify(data));
draw = data;
if(draw.status && draw.data.prize_id){
// 一等奖135 二等奖 45 三等奖 -45 四等奖 -135 特等奖 -180
switch (draw.data.prize_name) {
case '一等奖':
prizeDeg = 135;
break;
case '二等奖':
prizeDeg = 45;
break;
case '三等奖':
prizeDeg = -45;
break;
case '四等奖':
prizeDeg = -135;
break;
case '特等奖':
prizeDeg = -180;
break;
default:
return;
}

Draw();
}

if(draw.status && draw.data.prize_id ==0){
Draw();
}

if(draw.status == 0){
alert(draw.message);
return;
}
},
error: function(data) {
draw = data;
}
});
}

 

$('.start').on('click',function(){
$(".start").attr('disabled', true);
getDraw();
})

})

posted @ 2017-10-27 17:32  winyh  阅读(279)  评论(0编辑  收藏  举报