jquery抽奖转盘java,jquery——九宫格大转盘抽奖实例

一、用到的图片

d512cba1bdd0af6991ffece6a212b24a.png

a615712e79c06eb4afa49acd97966c89.png

f3deafc6ab3301f9718c98b5e3a671b6.png

6419c4ce98f9fa13eb6a315336603d93.png

de575e118a482545cae746421c3833a3.png

0d8b34b1fa8fe160c7ed8140237ced31.png

7434eb98a7a4b0c3d8d853427f1039c7.png

c6a70c8032cc0d01d47670d009ee1589.png

1b54d305690609224535298e118d9c26.png

二、代码如下,重点是js部分

jQuery九宫格大转盘抽奖

#lottery{width:570px;height:510px;margin:0px auto;border:4px solid #ba1809;}

#lottery table{background-color:yellow;}

#lottery table td{position:relative;width:190px;height:170px;text-align:center;color:#333;font-index:-999}

#lottery table td img{display:block;width:190px;height:170px;}

#lottery table td a{width:190px;height:170px;display:block;text-decoration:none;background:url(images/lottery1.jpg) no-repeat top center;}

#lottery table td a:hover{background-image:url(images/lottery2.jpg);}

#lottery table td.active .mask{display:block;}

.mask{

width:100%;

height:100%;

position:absolute;

left:0;

top:0;

background:url(images/mask.png) no-repeat;

display:none;

}

gift0.jpggift1.jpggift2.jpg
gift7.jpggift3.jpg
gift6.jpggift5.jpggift4.jpg

var lottery={

index:-1, //当前转动到哪个位置,起点位置

count:0, //总共有多少个位置

timer:0, //setTimeout的ID,用clearTimeout清除

speed:20, //初始转动速度

times:0, //转动次数

cycle:50, //转动基本次数:即至少需要转动多少次再进入抽奖环节

prize:-1, //中奖位置

init:function(id){

if ($("#"+id).find(".lottery-unit").length>0) {

$lottery = $("#"+id);

$units = $lottery.find(".lottery-unit");

this.obj = $lottery;

this.count = $units.length;

$lottery.find(".lottery-unit-"+this.index).addClass("active");

};

},

roll:function(){

var index = this.index;

var count = this.count;

var lottery = this.obj;

$(lottery).find(".lottery-unit-"+index).removeClass("active");

index += 1;

if (index>count-1) {

index = 0;

};

$(lottery).find(".lottery-unit-"+index).addClass("active");

this.index=index;

return false;

},

stop:function(index){

this.prize=index;

return false;

}

};

function roll(){

lottery.times += 1;

lottery.roll();//转动过程调用的是lottery的roll方法,这里是第一次调用初始化

if (lottery.times > lottery.cycle+10 && lottery.prize==lottery.index) {

clearTimeout(lottery.timer);

lottery.prize=-1;

lottery.times=0;

click=false;

}else{

if (lottery.times

lottery.speed -= 10;

}else if(lottery.times==lottery.cycle) {

var index = Math.random()*(lottery.count)|0;

lottery.prize = index;

}else{

if (lottery.times > lottery.cycle+10 && ((lottery.prize==0 && lottery.index==7) || lottery.prize==lottery.index+1)) {

lottery.speed += 110;

}else{

lottery.speed += 20;

}

}

if (lottery.speed<40) {

lottery.speed=40;

};

//console.log(lottery.times+'^^^^^^'+lottery.speed+'^^^^^^^'+lottery.prize);

lottery.timer = setTimeout(roll,lottery.speed);//循环调用

}

return false;

}

var click=false;

window.οnlοad=function(){

lottery.init('lottery');

$("#lottery a").click(function(){

if (click) {//click控制一次抽奖过程中不能重复点击抽奖按钮,后面的点击不响应

return false;

}else{

lottery.speed=100;

roll(); //转圈过程不响应click事件,会将click置为false

click=true; //一次抽奖完成后,设置click为true,可继续抽奖

return false;

}

});

};

效果如下:

a2e1a73395b9cac44a2067a5137bdeb0.gif

三、注意事项

1、抽奖过程说明

上面只是前端展示的效果。中奖物品通过一个随机数生成。

var index = Math.random()*(lottery.count)|0;

真正开发中中奖物品是通过向后端接口发送请求返回的。

$("#lottery a").click(function(){

var islogin=checkLogin();

if(islogin){//已登录用户才能去抽奖

if (click) {

return false;

}else{

//向后端接口发请求返回中奖结果

var geturl="http://xxxxxx?username="+username+"&token="+token;

$.ajax({

url:geturl,

type:"GET",

dataType:"json",

async:false,

success:function(data){

if(data.errorcode==0){

var rewardid=data["message"]["rewardid"];

var cardno=data["message"]["rewardCardNo"];

var passno=data["message"]["rewardCardPass"];

var prize=-1;

var content="";

if(rewardid=="iphone6"){

lottery.prize=0;

prize=0;

content="一部iphone6手机";

$("#content1").html(content);

}else if(rewardid=="PPTVKING"){

lottery.prize=1;

prize=1;

content="一部PPTV KING7s 3D影音手机";

$("#content1").html(content);

/*... */

}else if(rewardid=="legao"){

lottery.prize=5;

prize=5;

content="一份乐高的玩具";

$("#content1").html(content);

}

lottery.speed=100;

roll();

click=true;

return false;

}else{

/*错误处理*/

if(data.errorcode==3){

$("#novip").show();

}else{

$("#notime").show();

}

}

}/*function结束*/

});/*ajax结束*/

}/*else结束*/

}

});

2、兼容性说明

.mask开始如下,用的是rgba,但是IE8不兼容,改为使用png图片background:url(images/mask.png) no-repeat;

.mask {

width: 100%;

height: 100%;

position: absolute;

left: 0;

top: 0;

background-color: rgba(252,211,4,0.5);

display: none

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
此资源包括抽奖相关所有的配置,中奖概率配置、奖品表、抽奖记录表和通用存储过程算法。 可以指定抽多少次以后在按照正常概率来计算,中奖个数,如果奖品全部被抽完就永远抽不中,中奖率和中奖最大范围有关,数字越大概率越低,反正越高。由于涉及到一些敏感表数据,只提供主要的中奖表,如用户流水账号信息这些表不提供。 规则: --中奖率公式:中奖率 = 奖项数字范围 ÷ 摇奖数字范围 ÷ 中奖数字范围 --1、摇奖数字范围最小值和最大值定义了产生奖项数字的范围。 --2、中奖数字范围是从1到中奖范围的最大值,此范围内产生中奖号码。 /* 比如说,现在有三个选择一等、二等、三等。 可以设置“摇奖数字选项”为 1-30 一等 奖项数字范围1-10 二等 奖项数字范围 11-20 三等 奖项数字范围 21-30 所设置的奖项数字范围必须在 “摇奖数字选项”范围内,而且不得交叉、重叠。 如果我其中一个设置1-12 另外一个设置10-20 那么就重叠了。 而且为了方便调整中奖率,建议把所有的 奖项数字范围全部设置等距离。如现在有十二个选项,那么依次可以设置成为以下: 1-10、11-20、21-30、...、111-120 那么自然 “摇奖数字范围”就是1-120 现在需要调整中奖率的大小,中奖号码不用改了,全部设置成为1. 然后去调整中奖数字范围。 比如说 一等 中奖数字范围 1-30 二等 中奖数字范围 1-20 三等 中奖数字范围 1-10 那么具体这样设置下来,中奖率会有多高呢?算一下便知道。 一等:10/(30*30)=1/90 九十分之一。 二等:10/(30*20)=1/60 六十分之一。 三等:10/(30*10)=1/30 三十分之一。 所以设置的时候就把中奖号码都设置成1,只需要调整中奖数字范围便可。 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值