Home php教程 php手册 分享jQuery+PHP实现的掷色子抽奖实例

分享jQuery+PHP实现的掷色子抽奖实例

Jun 02, 2016 am 09:13 AM
foreach

本文我们来分享一个由jQuery+PHP开发的掷色子抽奖实例,开发者可以将本实例稍作修改即可运用到网站中的抽奖活动场景中。

本文以大富翁游戏为背景,综合运用jQuery和PHP知识,设计出以掷色子点数来达成抽奖的效果,当然抽奖概率是可控的,开发者可以将本实例稍作修改即可运用到网站中的抽奖活动场景中。

 

 jQuery+PHP掷色子抽奖

 

HTML

首先我们需要准备两粒色子和奖品素材,这些作者已经打包上传了,请大家放心下载。我们将在html页面中写下如下的html结构代码,.wrap用来放置色子和提示信息,#prize则是用来放置奖品的。

<div class="demo"> 
    <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="/static/imghw/default1.png"  data-src="images/0.gif"  class="lazy"   alt="开始"></li> 
        <li id="d_1"><img src="/static/imghw/default1.png"  data-src="images/1.gif"  class="lazy"   alt="现金100元"></li> 
        <li id="d_2"><img src="/static/imghw/default1.png"  data-src="images/2.gif"  class="lazy"   alt="泰迪熊宝宝"></li> 
        <li id="d_3"><img src="/static/imghw/default1.png"  data-src="images/7.gif"  class="lazy"   alt="谢谢参与"></li> 
        <li id="d_4"><img src="/static/imghw/default1.png"  data-src="images/3.gif"  class="lazy"   alt="iphone 5s"></li> 
        <li id="d_5"><img src="/static/imghw/default1.png"  data-src="images/4.gif"  class="lazy"   alt="笔记本电脑"></li> 
        <li id="d_6"><img src="/static/imghw/default1.png"  data-src="images/7.gif"  class="lazy"   alt="谢谢参与"></li> 
        <li id="d_7"><img src="/static/imghw/default1.png"  data-src="images/5.gif"  class="lazy"   alt="单反相机"></li> 
        <li id="d_8"><img src="/static/imghw/default1.png"  data-src="images/6.gif"  class="lazy"   alt="轿车"></li> 
        <li id="d_9"><img src="/static/imghw/default1.png"  data-src="images/7.gif"  class="lazy"   alt="谢谢参与"></li> 
    </ul> 
</div>
Copy after login

我们要用CSS技术来将页面布局合理规范化,我们将奖品围成一个矩形,共10个位置,将两粒色子定位在矩形的中央,抽奖时可以直接点击中间的色子。这些我们可以用CSS的定位技术来实现页面布局。CSS

.demo{width:650px; height:420px; margin:60px auto 10px auto; position:relative; } 
.wrap{width:200px; height:100px; position:absolute; margin-left:220px; margin-top:140px; z-index:1000;} 
#msg{display:none;width:50px; height:20px; padding:4px; background:#ffc; border:1px solid #fc9;  
text-align:center; color:#f30; font-size:18px; position:absolute; z-index:1001; right:-20px; top:-10px} 
.dice{width:90px; height:90px; display:block; float:left; background:url(http://pic1.phprm.com/2015/01/01/dice.jpg) no-repeat; cursor:pointer} 
#dice_mask{width:200px; height:100px; background:#fff; opacity:0; position:absolute; top:0; left:0; z-index:999} 
.dice_1{background-position:-5px -4px} 
.dice_2{background-position:-5px -107px} 
.dice_3{background-position:-5px -212px} 
.dice_4{background-position:-5px -317px} 
.dice_5{background-position:-5px -427px} 
.dice_6{background-position:-5px -535px} 
.dice_t{background-position:-5px -651px} 
.dice_s{background-position:-5px -763px} 
.dice_e{background-position:-5px -876px} 
#prize{position:relative} 
#prize li{position:absolute; width:150px; height:112px; border:1px solid #d3d3d3} 
#d_0{left:0; top:0} 
#d_1{left:160px; top:0} 
#d_2{left:320px; top:0} 
#d_3{left:480px; top:0} 
#d_4{left:480px; top:128px} 
#d_5{left:480px; top:256px} 
#d_6{left:320px; top:256px} 
#d_7{left:160px; top:256px} 
#d_8{left:0; top:256px} 
#d_9{left:0; top:128px} 
.mask{opacity: 0.6; width:150px; height:112px; line-height:32px; background:#ffc;  
z-index:1001; position:absolute; top:0; left:0; text-align:center; font-size:16px}
Copy after login

jQuery

我们使用jQquery来完成前端动作,包括掷色子动画,仿大富翁奖品逐步运动动画,其中有防重复点击知识、ajax交互知识,动画提示知识。整个 操作流程可简单概括为:点击色子->向dice.php发送ajax请求->完成掷色子动画->提示点数->逐步运动动画到最终 奖品位置停止->完成抽奖。

$(function(){ 
    $("#dice").click(function(){ 
        $("#prize li .mask").remove(); 
        $(".wrap").append("<div id=&#39;dice_mask&#39;></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+&#39;点&#39;).animate({top:&#39;-50px&#39;},&#39;1000&#39;).fadeOut(500);
            roll(0, num);//逐步运动动画 
        }); 
    }); 
});
Copy after login

函数diceroll()是一个色子运动动画,在本站前面的文章中已讲解过,就是通过jQuery的animate()实现的位移、延时、变化背景样式来实现的动画效果。

function diceroll(dice,num){ 
    dice.attr("class","dice");//清除上次动画后的点数 
    dice.css(&#39;cursor&#39;,&#39;default&#39;); 
    dice.animate({left: &#39;+2px&#39;}, 100,function(){ 
        dice.addClass("dice_t"); 
    }).delay(200).animate({top:&#39;-2px&#39;},100,function(){ 
        dice.removeClass("dice_t").addClass("dice_s"); 
    }).delay(200).animate({opacity: &#39;show&#39;},600,function(){ 
        dice.removeClass("dice_s").addClass("dice_e"); 
    }).delay(100).animate({left:&#39;-2px&#39;,top:&#39;2px&#39;},100,function(){ 
        dice.removeClass("dice_e").addClass("dice_"+num); 
        dice.css(&#39;cursor&#39;,&#39;pointer&#39;); 
    }); 
}
Copy after login

函数roll()至关重要,通过setInterval()设置一个间隔动画,每隔0.5秒时间执行一次。参数i代表初始位置,参数step代表需要执行 的步数,在本例中就是色子的点数,即需要走的步数。我们根据i给当前奖品加上.mask,当i的值与step相等时,停止动画,并且移除色子的遮罩(防止 重复点击)。

function roll(i,step){ 
    var time = setInterval(function(){ 
        if(i>9){ 
            var t = i - 10; 
            $("#d_"+t).append("<div class=&#39;mask&#39;></div>"); 
            $("#d_"+(t-1)+" .mask").remove(); 
        } 
        $("#d_"+i).append("<div class=&#39;mask&#39;></div>"); 
        $("#d_"+(i-1)+" .mask").remove(); 
        if(i==step){ 
             clearInterval(time); //如果到达指定位置则停止 
             $("#dice_mask").remove();//移除遮罩 
        } 
        i++;//继续前进 
    },500); 
}
Copy after login

PHP

dice.php需要做的事情有:根据配置好的奖品概率,得到总点数,根据总点数进行两粒色子的点数分配,最后返回给前端页面两粒色子的点数。

//设置中奖概率 
$prize_arr = array( 
    &#39;2&#39; => array(&#39;id&#39;=>2,&#39;v&#39;=>10), 
    &#39;3&#39; => array(&#39;id&#39;=>3,&#39;v&#39;=>20), 
    &#39;4&#39; => array(&#39;id&#39;=>4,&#39;v&#39;=>5), 
    &#39;5&#39; => array(&#39;id&#39;=>5,&#39;v&#39;=>5), 
    &#39;6&#39; => array(&#39;id&#39;=>6,&#39;v&#39;=>20), 
    &#39;7&#39; => array(&#39;id&#39;=>7,&#39;v&#39;=>2), 
    &#39;8&#39; => array(&#39;id&#39;=>8,&#39;v&#39;=>3), 
    &#39;9&#39; => array(&#39;id&#39;=>9,&#39;v&#39;=>20), 
    &#39;10&#39; => array(&#39;id&#39;=>10,&#39;v&#39;=>0), 
    &#39;11&#39; => array(&#39;id&#39;=>11,&#39;v&#39;=>10), 
    &#39;12&#39; => array(&#39;id&#39;=>12,&#39;v&#39;=>5), 
); 
foreach ($prize_arr as $key => $val) { 
    $arr[$val[&#39;id&#39;]] = $val[&#39;v&#39;]; 
} 
$sum = getRand($arr); //根据概率获取奖项id,得到总点数 
//分配色子点数 
$arrs = array( 
    &#39;2&#39; => array(array(1,1)), 
    &#39;3&#39; => array(array(1,2)), 
    &#39;4&#39; => array(array(1,3),array(2,2)), 
    &#39;5&#39; => array(array(1,4),array(2,3)), 
    &#39;6&#39; => array(array(1,5),array(2,4),array(3,3)), 
    &#39;7&#39; => array(array(1,6),array(2,7),array(3,4)), 
    &#39;8&#39; => array(array(2,6),array(3,5),array(4,4)), 
    &#39;9&#39; => array(array(3,6),array(4,5)), 
    &#39;10&#39; => array(array(4,6),array(5,5)), 
    &#39;11&#39; => array(array(5,6)), 
    &#39;12&#39; => array(array(6,6)) 
); 
$arr_rs = $arrs[$sum]; 
$i = array_rand($arr_rs);//随机取数组 
$arr_a = $arr_rs[$i]; 
shuffle($arr_a);//打乱顺序 
echo json_encode($arr_a);
Copy after login

函数getRand()用来计算概率

//计算概率 
function getRand($proArr) { 
    $result = &#39;&#39;; 
    //概率数组的总概率精度 
    $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; 
}
Copy after login

教程链接:

随意转载~但请保留教程地址★

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? Apr 27, 2023 pm 03:40 PM

1. The difference between Iterator and foreach is the polymorphic difference (the bottom layer of foreach is Iterator) Iterator is an interface type, it does not care about the type of collection or array; both for and foreach need to know the type of collection first, even the type of elements in the collection; 1. Why is it said that the bottom layer of foreach is the code written by Iterator: Decompiled code: 2. The difference between remove in foreach and iterator. First, look at the Alibaba Java Development Manual, but no error will be reported in case 1, and an error will be reported in case 2 (java. util.ConcurrentModificationException) first

How to determine the number of foreach loop in php How to determine the number of foreach loop in php Jul 10, 2023 pm 02:18 PM

​The steps for PHP to determine the number of the foreach loop: 1. Create an array of "$fruits"; 2. Create a counter variable "$counter" with an initial value of 0; 3. Use "foreach" to loop through the array, and Increase the value of the counter variable in the loop body, and then output each element and their index; 4. Output the value of the counter variable outside the "foreach" loop to confirm which element the loop reaches.

PHP returns an array with key values ​​flipped PHP returns an array with key values ​​flipped Mar 21, 2024 pm 02:10 PM

This article will explain in detail how PHP returns an array after key value flipping. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP Key Value Flip Array Key value flip is an operation on an array that swaps the keys and values ​​in the array to generate a new array with the original key as the value and the original value as the key. Implementation method In PHP, you can perform key-value flipping of an array through the following methods: array_flip() function: The array_flip() function is specially used for key-value flipping operations. It receives an array as argument and returns a new array with the keys and values ​​swapped. $original_array=[

PHP returns the current element in an array PHP returns the current element in an array Mar 21, 2024 pm 12:36 PM

This article will explain in detail about the current element in the array returned by PHP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. Get the current element in a PHP array PHP provides a variety of methods for accessing and manipulating arrays, including getting the current element in an array. The following introduces several commonly used techniques: 1. current() function The current() function returns the element currently pointed to by the internal pointer of the array. The pointer initially points to the first element of the array. Use the following syntax: $currentElement=current($array);2.key() function key() function returns the array internal pointer currently pointing to the element

What is the difference between foreach and for loop What is the difference between foreach and for loop Jan 05, 2023 pm 04:26 PM

Difference: 1. for loops through each data element through the index, while forEach loops through the data elements of the array through the JS underlying program; 2. for can terminate the execution of the loop through the break keyword, but forEach cannot; 3. for can control the execution of the loop by controlling the value of the loop variable, but forEach cannot; 4. for can call loop variables outside the loop, but forEach cannot call loop variables outside the loop; 5. The execution efficiency of for is higher than forEach.

How to iterate through the properties of an object using the forEach function? How to iterate through the properties of an object using the forEach function? Nov 18, 2023 pm 06:10 PM

How to iterate through the properties of an object using the forEach function? In JavaScript, we often need to traverse the properties of objects. If you want to use a concise way to iterate over the properties of an object, the forEach function is a very good choice. In this article, we will explain how to use the forEach function to iterate over the properties of an object and provide specific code examples. First, let's understand the basic usage of the forEach function. forEach function is Java

How to get the index value of the current element in php foreach loop How to get the index value of the current element in php foreach loop Mar 23, 2023 am 09:17 AM

In PHP, the foreach statement is widely used to traverse arrays and objects. During the loop, we sometimes need to get the current element of the loop. This article will introduce how to get the index value of the current element in the PHP foreach loop.

Detailed explanation of the function and usage of break keyword in PHP Detailed explanation of the function and usage of break keyword in PHP Jun 28, 2023 pm 06:39 PM

Detailed explanation of the role and usage of the break keyword in PHP In PHP programming, break is a control flow statement used to interrupt the current loop or switch statement and jump out of the loop or switch. This article will introduce in detail the role and usage of the break keyword. 1. Break in a loop In a loop structure, the function of break is to terminate the loop early and jump out of the loop body to execute the code after the loop. Common loop structures include for, while and do...while. in for loop

See all articles