jquery转盘抽奖功能实现


Posted in Javascript onNovember 13, 2015

 一、用到的素材

jquery转盘抽奖功能实现jquery转盘抽奖功能实现jquery转盘抽奖功能实现jquery转盘抽奖功能实现jquery转盘抽奖功能实现jquery转盘抽奖功能实现jquery转盘抽奖功能实现

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

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery九宫格大转盘抽奖</title>
<style>
#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;
}
</style>
</head>
<body class="keBody">
<!--效果html开始-->
<div id="lottery">
 <table border="0" cellpadding="0" cellspacing="0">
  <tr>
   <td class="lottery-unit lottery-unit-0"><img src="images/gift0.jpg"><div class="mask"></div></td>
   <td class="lottery-unit lottery-unit-1"><img src="images/gift1.jpg"><div class="mask"></div></td>
   <td class="lottery-unit lottery-unit-2"><img src="images/gift2.jpg"><div class="mask"></div></td>
  </tr>
  <tr>
   <td class="lottery-unit lottery-unit-7"><img src="images/gift7.jpg"><div class="mask"></div></td>
   <td><a href="#"></a></td>
   <td class="lottery-unit lottery-unit-3"><img src="images/gift3.jpg"><div class="mask"></div></td>
  </tr>
  <tr>
   <td class="lottery-unit lottery-unit-6"><img src="images/gift6.jpg"><div class="mask"></div></td>
   <td class="lottery-unit lottery-unit-5"><img src="images/gift5.jpg"><div class="mask"></div></td>
   <td class="lottery-unit lottery-unit-4"><img src="images/gift4.jpg"><div class="mask"></div></td>
  </tr>
 </table>
</div>

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
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.cycle) {
   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.onload=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;
  }
 });
};
</script>
<!--效果html结束-->

</body>
</html>

效果如下:

jquery转盘抽奖功能实现

三、注意事项
.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
}

希望大家可以从小编整理的这篇文章中有所收获,对大家实现抽奖功能有所帮助。

Javascript 相关文章推荐
javascript 清除输入框中的数据
Apr 13 Javascript
javascript实现图片切换的幻灯片效果源代码
Dec 12 Javascript
Node.js和PHP根据ip获取地理位置的方法
Mar 14 Javascript
jsPDF导出pdf示例
May 02 Javascript
JavaScript中的全局对象介绍
Jan 01 Javascript
Node.js模块封装及使用方法
Mar 06 Javascript
微信js-sdk上传与下载图片接口用法示例
Oct 12 Javascript
Bootstrap3 图片(响应式图片&amp;图片形状)
Jan 04 Javascript
解决linux下node.js全局模块找不到的问题
May 15 Javascript
D3.js实现拓扑图的示例代码
Jun 30 Javascript
在vue中阻止浏览器后退的实例
Nov 06 Javascript
浅谈webpack和webpack-cli模块源码分析
Jan 19 Javascript
javascript生成随机数方法汇总
Nov 12 #Javascript
js正则表达式验证邮件地址
Nov 12 #Javascript
每天一篇javascript学习小结(Boolean对象)
Nov 12 #Javascript
为何JS操作的href都是javascript:void(0);呢
Nov 12 #Javascript
基于jquery实现左右按钮点击的图片切换效果
Jan 27 #Javascript
js图片轮播效果实现代码
Apr 18 #Javascript
javascript伸缩菜单栏实现代码分享
Nov 12 #Javascript
You might like
个人站长制做网页常用的php代码
2007/03/03 PHP
PHP在线生成二维码代码(google api)
2013/06/03 PHP
PHP+Mysql+Ajax+JS实现省市区三级联动
2014/05/23 PHP
php实现统计网站在线人数的方法
2015/05/12 PHP
浅谈PHP中output_buffering
2015/07/13 PHP
Yii2框架数据验证操作实例详解
2018/05/02 PHP
json简单介绍
2008/06/10 Javascript
JavaScript脚本性能优化注意事项
2008/11/18 Javascript
通过正则格式化url查询字符串实现代码
2012/12/28 Javascript
jQuery中size()方法用法实例
2014/12/27 Javascript
JS模仿手机端九宫格登录功能实现代码
2016/04/28 Javascript
JS实现全屏的四种写法
2016/12/30 Javascript
JavaScript校验Number(4,1)格式的数字实例代码
2017/03/13 Javascript
jQuery实现div跟随鼠标移动
2020/08/20 jQuery
详解webpack + vue + node 打造单页面(入门篇)
2017/09/23 Javascript
Node.js学习之查询字符串解析querystring详解
2017/09/28 Javascript
web前端vue实现插值文本和输出原始html
2018/01/19 Javascript
layui实现动态和静态分页
2018/04/28 Javascript
javascript使用Blob对象实现的下载文件操作示例
2020/04/18 Javascript
VSCode搭建React Native环境
2020/05/07 Javascript
提升Python程序性能的7个习惯
2019/04/14 Python
Python之数据序列化(json、pickle、shelve)详解
2019/08/30 Python
实现Python3数组旋转的3种算法实例
2020/09/16 Python
详解Python调用系统命令的六种方法
2021/01/28 Python
使paramiko库执行命令时在给定的时间强制退出功能的实现
2021/03/03 Python
新西兰领先的鞋类和靴子网上商城:Merchant 1948
2017/09/08 全球购物
英国工作场所设备购买网站:Slingsby
2019/05/03 全球购物
英国现代、当代和设计师家具店:Furntastic
2020/07/18 全球购物
怎么写好自荐信
2013/10/30 职场文书
投标单位介绍信
2014/01/09 职场文书
绿化先进工作者事迹材料
2014/01/30 职场文书
政风行风建设责任书
2014/07/23 职场文书
卖房协议书样本
2014/10/30 职场文书
2019终止劳动合同协议书最新范本!
2019/07/09 职场文书
幽默口才训练经典句子(48句)
2019/08/19 职场文书
python中print格式化输出的问题
2021/04/16 Python