经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » jQuery » 查看文章
jquery+h5实现九宫格抽奖特效(前后端代码)
来源:jb51  时间:2021/8/16 16:34:49  对本文有异议

前言:

前端:jq+h5 实现九宫格动效

后端:thinkphp3.2.3 实现中奖概率算法

功能:支持读取数据库预设的中奖率及奖品池,中奖率可以自定义,必须是整数

最终效果如下:

代码:

choujiang.html代码如下:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <meta name="author" content="武当山道士" />
  6. <title>九宫格抽奖转盘</title>
  7. <style type="text/css">
  8. .container{width:100%;height:auto;line-height: 100%;text-align: center;}
  9. #lottery{width:425px;height:425px;margin:auto;background:#e5e5e5;}
  10. #lottery table td{width:142px;height:142px;text-align:center;vertical-align:middle;font-size:24px;color:#333;font-index:-999;
  11. }
  12. /*#lottery table td a{width:284px;height:284px;line-height:150px;display:block;text-decoration:none;}*/
  13. #lottery table td.active{background-color:#fff333;border-radius: 10px;}
  14.  
  15. #start {color:white;background:orange;
  16. border-radius: 10px;
  17. height:130px;
  18. line-height: 130px;
  19. width:130px;
  20. margin: auto;
  21. /*margin: 10% 10% 10% 10%;*/
  22. text-align: center;
  23. display: block;
  24. text-decoration: none;
  25. }
  26. #con_prize{display: none;margin-top: 10px;}
  27. #pname{color:red;margin-left: 5px;margin-right: 10px;font-size: 20px;}
  28. .prize{background:#000 ;opacity: 0.5;
  29. height:130px;width: 130px;
  30. border-radius: 5px;margin: auto;line-height: 130px;
  31. text-shadow: 1px 1px 2px;
  32. }
  33. .on{opacity: 1;color:#fff;background: #a5a5d1}
  34. </style>
  35. </head>
  36. <body>
  37. <div class="container">
  38. <div id="lottery">
  39. <table border="0" cellpadding="0" cellspacing="0" style="background:#e3f4a1">
  40. <tr>
  41. <td class="lottery-unit lottery-unit-0"><div class="prize prize-0">安慰奖</div></td>
  42. <td class="lottery-unit lottery-unit-1"><div class="prize prize-1">玩具车</div></td>
  43. <td class="lottery-unit lottery-unit-2"><div class="prize prize-2">自行车</div></td>
  44. </tr>
  45. <tr>
  46. <td class="lottery-unit lottery-unit-7"><div class="prize prize-7">奥迪</div></td>
  47. <td ><a href="#" rel="external nofollow" class = "lottery-unit" id="start">开始抽奖</a></td>
  48. <td class="lottery-unit lottery-unit-3"><div class="prize prize-3">电动车</div></td>
  49. </tr>
  50. <tr>
  51. <td class="lottery-unit lottery-unit-6"><div class="prize prize-6">夏利</div></td>
  52. <td class="lottery-unit lottery-unit-5"><div class="prize prize-5">拖拉机</div></td>
  53. <td class="lottery-unit lottery-unit-4"><div class="prize prize-4">摩托</div></td>
  54. </tr>
  55. </table>
  56. </div>
  57. <div id="con_prize" data-pname="长宜太盛100元优惠券" data-pid="1">恭喜您获得奖品:<span id="pname"></span><button onclick="getPrize()">领取奖品</button></div>
  58. </div>
  59.  
  60. <script type="text/javascript" src="jquery-1.11.1.min.js"></script>
  61. <script type="text/javascript">
  62.  
  63.  
  64. var lottery={
  65. index:-1, //当前转动到哪个位置,起点位置
  66. count:8, //总共有多少个奖品位置,9宫格就是8个奖品位置
  67. timer:0, //setTimeout的ID,用clearTimeout清除
  68. speed:10, //初始转动速度
  69. times:0, //转动次数
  70. cycle:50, //转动基本次数:即至少需要转动多少次再进入抽奖环节
  71. prize:0, //默认中奖位置,放默认奖品
  72. init:function(id){
  73. if ($("#"+id).find(".lottery-unit").length>0) {
  74. $lottery = $("#"+id);
  75. $units = $lottery.find(".lottery-unit");
  76. this.obj = $lottery;
  77. this.count = $units.length;
  78. $lottery.find(".prize-"+this.index).addClass("on");
  79. };
  80. },
  81. roll:function(){
  82. var index = this.index;
  83. var count = this.count;
  84. var lottery = this.obj;
  85. $(lottery).find(".prize-"+index).removeClass("on");
  86. index += 1;
  87. if (index>count-1) {
  88. index = 0;
  89. };
  90. $(lottery).find(".prize-"+index).addClass("on");
  91. this.index=index;
  92. return false;
  93. },
  94. stop:function(index){
  95. this.prize=index;
  96. return false;
  97. }
  98. };
  99.  
  100. //存储奖品信息
  101. var prize_data = {
  102. pname:'默认奖品', //奖品名称
  103. pnum:0, //中奖位置 默认0,重要,转盘据此来定位获奖商品
  104. pid:1, //奖品id 默认1
  105. };
  106.  
  107. function roll(){
  108. lottery.times += 1;
  109. lottery.roll();
  110. if (lottery.times > lottery.cycle+10 && lottery.prize==lottery.index) {
  111. clearTimeout(lottery.timer);
  112. lottery.times=0;
  113. click=false;
  114. //显示中奖信息
  115. showDetail();
  116. }else{
  117. //速度控制
  118. if (lottery.times<lottery.cycle) {
  119. lottery.speed -= 10;
  120. }else if(lottery.times==lottery.cycle) {
  121. index = lottery.prize;
  122. }else{
  123. if (lottery.times > lottery.cycle+10 && ((lottery.prize==0 && lottery.index==7) || lottery.prize==lottery.index+1)) {
  124. lottery.speed += 110;
  125. }else{
  126. lottery.speed += 20;
  127. }
  128. }
  129. if (lottery.speed<40) {
  130. lottery.speed=40;
  131. };
  132. //延时递归调用
  133. lottery.timer = setTimeout(roll,lottery.speed);
  134. }
  135. return false;
  136. }
  137. /*
  138. * 获取中奖位置
  139. * @param {string} name 用户昵称(必须)
  140. * @param {string} avatar 微信头像网址(必须)
  141. * @param {string} openid 微信openid(必须,用于验证唯一性,一个用户只能抽奖一次)
  142. * @return {bool}
  143. */
  144. function doRoll(url,name,avatar,openid){
  145. $.ajax({ url: url, data:{name:name,avatar:avatar,openid:openid}, async:false,dataType:'json',success: function(rst){
  146. lottery.speed=100;
  147. var data = rst.data;
  148. lottery.prize = data.pnum;
  149. prize_data = {
  150. pname:data.pname,
  151. pnum:data.pnum,
  152. pid:data.pid
  153. };
  154. roll();
  155. click=true;
  156. return false;
  157.  
  158. }});
  159. }
  160.  
  161. //领奖(跳转收货地址页面,或者弹出收货地址页面)
  162. function getPrize(){
  163. alert("请填写收货地址");
  164. }
  165. //清空中奖信息
  166. function clearDetail(){
  167. $("#con_prize").hide();
  168. $("#pname").html("");
  169. }
  170. //显示中奖信息
  171. function showDetail(){
  172. $("#con_prize").show();
  173. $("#pname").html(prize_data.pname);
  174. }
  175.  
  176. var click=false;
  177.  
  178. window.function(){
  179. var url = 'http://test.com/api/Shop/ex/';//这里改成实际后台抽奖接口
  180. lottery.init('lottery');
  181. $("#start").click(function(){
  182. if (click) {
  183. return false;
  184. }else{
  185. clearDetail();
  186. doRoll(url,"name","avatar","openid");
  187. }
  188. });
  189. };
  190.  
  191. </script>
  192. </body>
  193. </html>

thinkphp接口代码如下:

  1. namespace Api\Controller;
  2. use Think\Controller;
  3. class ShopController Extends Controller{
  4. /**
  5. * 抽奖后台接口
  6. * @author 武当山道士
  7. */
  8. public function ex(){
  9. $nick = I('nick','','trim');
  10. $avatar = I('avatar','','trim');
  11. $openid = I('openid','','trim');
  12. //if(empty($nick)) $this->error('empty nick');
  13. //if(empty($avatar)) $this->error('empty avatar');
  14. //if(empty($openid)) $this->error('empty openid');
  15. /*自己封装的error函数,正常应该这样写:
  16. $this->ajaxReturn(array(
  17. 'data'=>'',
  18. 'info'=>$info,
  19. 'status'=>$status
  20. ));*/
  21. //初始化奖品池,8个奖品,满概率100,最小概率为1(id,name以实际数据库取出的数据为准,percent之和等于100)
  22. $arr8 = array(
  23. array("id"=>1,"name"=>"安慰奖","percent"=>69),
  24. array("id"=>2,"name"=>"玩具车","percent"=>10),
  25. array("id"=>3,"name"=>"自行车","percent"=>6),
  26. array("id"=>4,"name"=>"电动车","percent"=>5),
  27. array("id"=>5,"name"=>"摩托","percent"=>4),
  28. array("id"=>6,"name"=>"拖拉机","percent"=>3),
  29. array("id"=>7,"name"=>"夏利","percent"=>2),
  30. array("id"=>8,"name"=>"奥迪","percent"=>1),
  31. );
  32. //下标存储数组100个下表,0-7 按概率分配对应的数量
  33. $indexArr = array();
  34. for($i=0;$i<sizeof($arr8);$i++){
  35. for($j=0;$j<$arr8[$i]['percent'];$j++){
  36. //index 追加到数组indexArr
  37. array_push($indexArr, $i);
  38. }
  39. }
  40. //数组乱序
  41. shuffle($indexArr);
  42. //从下标数组中随机取一个下标作为中奖下标,$rand_index 是$indexArr的随机元素的下标(0-99)
  43. $rand_index = array_rand($indexArr,1);
  44. //获取中奖信息
  45. $prize_index = $indexArr[$rand_index];
  46. $prizeInfo = $arr8[$prize_index];
  47.  
  48. $data['pnum'] = $prize_index;//对应前端奖品编号
  49. $data['pid'] = $prizeInfo['id'];
  50. $data['pname'] = $prizeInfo['name'];
  51. $this->success($data);/*自己封装的success,正常应该这样写
  52. $this->ajaxReturn(array(
  53. 'data'=>$data,
  54. 'info'=>'成功',
  55. 'status'=>1
  56. ));*/
  57. }
  58. }

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号