更新 2020-09-22 浏览 1036次
HTML大富翁色子样式布局
<div class="cont-1"> <div class="wrap"> <div id="msg"></div> <div id="dice"><span class="dice dice_1" id="dice1"></span><span class="dice dice_6" id="dice2"></span></div> </div> <ul id="prize"> <li id="d_0"><img src="images/0.gif" alt="开始"></li> <li id="d_1"><img src="images/1.gif" alt="现金100元"></li> <li id="d_2"><img src="images/2.gif" alt="泰迪熊宝宝"></li> <li id="d_3"><img src="images/7.gif" alt="谢谢参与"></li> <li id="d_4"><img src="images/3.gif" alt="iphone 5s"></li> <li id="d_5"><img src="images/4.gif" alt="笔记本电脑"></li> <li id="d_6"><img src="images/7.gif" alt="谢谢参与"></li> <li id="d_7"><img src="images/5.gif" alt="单反相机"></li> <li id="d_8"><img src="images/6.gif" alt="轿车"></li> <li id="d_9"><img src="images/7.gif" alt="谢谢参与"></li> </ul> </div>
Jquery代码
引入jQuery文件,添加dice色子点击动作并且做了防重复点击处理,点击色子后通过getJSON请求后端PHP程序代码处理获取中奖结果。
<script src="js/jquery.min.js"></script> <script type="text/javascript"> $(function() { $("#dice").click(function() { $("#prize li .mask").remove(); $(".wrap").append("<div id='dice_mask'></div>");//加遮罩 var dice1 = $("#dice1"); var dice2 = $("#dice2"); $.getJSON("dice.php", function(json) { var num1 = json[0]; var num2 = json[1]; diceroll(dice1, num1);//掷色子1动画 diceroll(dice2, num2);//掷色子2动画 var num = parseInt(num1) + parseInt(num2); $("#msg").css("top", "-10px").fadeIn(500).text(num + '点').animate({top: '-50px'}, '1000').fadeOut(500); roll(0, num); }); }); }); function diceroll(dice, num) { dice.attr("class", "dice");//清除上次动画后的点数 dice.css('cursor', 'default'); dice.animate({left: '+2px'}, 100, function() { dice.addClass("dice_t"); }).delay(200).animate({top: '-2px'}, 100, function() { dice.removeClass("dice_t").addClass("dice_s"); }).delay(200).animate({opacity: 'show'}, 600, function() { dice.removeClass("dice_s").addClass("dice_e"); }).delay(100).animate({left: '-2px', top: '2px'}, 100, function() { dice.removeClass("dice_e").addClass("dice_" + num); dice.css('cursor', 'pointer'); }); } function roll(i, step) { var time = setInterval(function() { if (i > 9) { var t = i - 10; $("#d_" + t).append("<div class='mask'></div>"); $("#d_" + (t - 1) + " .mask").remove(); } $("#d_" + i).append("<div class='mask'></div>"); $("#d_" + (i - 1) + " .mask").remove(); if (i == step) { clearInterval(time); //如果到达指定位置则停止 $("#dice_mask").remove();//移除遮罩 } i++;//继续前进 }, 500); } </script>
PHP程序处理代码:
$prize_arr = array( '2' => array('id'=>2,'v'=>10), '3' => array('id'=>3,'v'=>20), '4' => array('id'=>4,'v'=>5), '5' => array('id'=>5,'v'=>5), '6' => array('id'=>6,'v'=>20), '7' => array('id'=>7,'v'=>2), '8' => array('id'=>8,'v'=>3), '9' => array('id'=>9,'v'=>20), '10' => array('id'=>10,'v'=>0), '11' => array('id'=>11,'v'=>10), '12' => array('id'=>12,'v'=>5), ); foreach ($prize_arr as $key => $val) { $arr[$val['id']] = $val['v']; } $sum = getRand($arr); //根据概率获取奖项id $arrs = array( '2' => array(array(1,1)), '3' => array(array(1,2)), '4' => array(array(1,3),array(2,2)), '5' => array(array(1,4),array(2,3)), '6' => array(array(1,5),array(2,4),array(3,3)), '7' => array(array(1,6),array(2,7),array(3,4)), '8' => array(array(2,6),array(3,5),array(4,4)), '9' => array(array(3,6),array(4,5)), '10' => array(array(4,6),array(5,5)), '11' => array(array(5,6)), '12' => array(array(6,6)) ); $arr_rs = $arrs[$sum]; $i = array_rand($arr_rs);//随机取数组 $arr_a = $arr_rs[$i]; shuffle($arr_a);//打乱顺序 //print_r($arr_a); echo json_encode($arr_a); //计算概率 function getRand($proArr) { $result = ''; //概率数组的总概率精度 $proSum = array_sum($proArr); //概率数组循环 foreach ($proArr as $key => $proCur) { $randNum = mt_rand(1, $proSum); if ($randNum <= $proCur) { $result = $key; break; } else { $proSum -= $proCur; } } unset ($proArr); return $result; }