Share an example of the ghost lottery implemented by jQuery + PHP

Source: Internet
Author: User

This article is based on the rich man's game and combines jQuery and PHP knowledge to design a lottery draw with the number of bonus points to achieve the effect. Of course, the lucky draw probability is controllable, developers can make slight modifications to this instance to apply it to the lottery activity scenario on the website.

 

 

HTML

First, we need to prepare two shards and prize materials. These authors have packed and uploaded them. Please feel free to download them. We will write down the following html structure code on the html page. wrap is used to place tips and prompt information, and # prize is used to place prizes.

 

The code is as follows: Copy code
<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"> </li>
<Li id = "d_1"> </li>
<Li id = "d_2"> </li>
<Li id = "d_3"> </li>
<Li id = "d_4"> </li>
<Li id = "d_5"> </li>
<Li id = "d_6"> </li>
<Li id = "d_7"> </li>
<Li id = "d_8"> </li>
<Li id = "d_9"> </li>
</Ul>
</Div>

CSS

We will use CSS technology to standardize the page layout reasonably. We will enclose the prize into a rectangle with 10 positions in total and locate the two shards in the center of the rectangle, during the lucky draw, you can directly click "middle. We can use CSS positioning technology to implement page layout.

 

The code is as follows: Copy code
. Demo {width: Pixel px; 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(dice.png) 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-pixel px}
. 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: Pixel px; 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: Pixel px; line-height: 32px; background: # ffc;
Z-index: 1001; position: absolute; top: 0; left: 0; text-align: center; font-size: 16px}

 

JQuery

We use jQquery to complete the front-end actions, including rolling animation and step-by-step motion animation of the Monopoly prize. Among them, we have the knowledge of anti-repeated clicks, ajax interaction, and animation prompts. The entire operation process can be summarized as follows: click submit-> Submit to dice. php sends an ajax request-> complete the rolling animation-> Number of points-> gradually move the animation to the final prize location to stop-> complete the lucky draw.

 

The code is as follows: Copy code
$ (Function (){
$ ("# Dice"). click (function (){
$ ("# Prize li. mask"). remove ();
$ (". Wrap"). append ("<div id = 'Dice _ mask'> </div>"); // mask
Var dice1 = $ ("# dice1 ");
Var dice2 = $ ("# dice2 ");
$. GetJSON ("dice. php", function (json ){
Var num1 = json [0];
Var num2 = json [1];
Diceroll (dice1, num1); // throw an animation
Diceroll (dice2, num2); // throw limit 2 Animation
Var num = parseInt (num1) + parseInt (num2 );
$ ("# Msg" ).css ("top", "-10px "). fadeIn (500 ). text (num + 'point '). animate ({top: '-50px'}, '123 '). fadeOut (1, 500 );
Roll (0, num); // progressive motion animation
});
});
});

 

The function diceroll () is an animated animation. It has been explained in the previous article on this site through jQuery's animate () implementation of displacement, latency, and change of the background style to achieve the animation effect.

 

The code is as follows: Copy code
Function diceroll (dice, num ){
Dice. attr ("class", "dice"); // clear the number of points after the last animation
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 ');
});
}

 

The function roll () is crucial. You can use setInterval () to set an interval animation that is executed every 0.5 seconds. Parameter I represents the initial position. Parameter step represents the number of steps to be executed. In this example, it is the number of worker points, that is, the number of steps to be taken. We add. mask to the current prize based on I. When the I value is equal to the step, stop the animation and remove the mask of the cursor (to prevent repeated clicks ).

 

The code is as follows: Copy code
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); // stop if the specified location is reached
$ ("# Dice_mask"). remove (); // remove the mask.
        }
I ++; // proceed
},500 );
}

 

PHP

Dice. php needs to do the following: according to the configured prize probability, get the total number of points, assign the number of two shards based on the total number of points, and finally return the number of two shards to the front-end page.

 

The code is as follows: Copy code
// Set the winning probability
$ 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); // Obtain the award id based on the probability and get the total number of points
 
// Allocate worker points
$ Arrs = array (
'2' => array (1, 1 )),
'3' => array (1, 2 )),
'4' => array (1, 3), array (2, 2 )),
'5' => array (1, 4), array (2, 3 )),
'6' => array (1, 5), array (2, 4), array (3, 3 )),
'7' => array (1, 6), array (2, 7), array (3, 4 )),
'8' => array (2, 6), array (3, 5), array (4, 4 )),
'9' => array (3, 6), array (4, 5 )),
'10' => array (4, 6), array (5, 5 )),
'11' => array (5, 6 )),
'12' => array (6, 6 ))
);
 
$ Arr_rs = $ arrs [$ sum];
$ I = array_rand ($ arr_rs); // random array
$ Arr_a = $ arr_rs [$ I];
Shuffle ($ arr_a); // disrupt the order
Echo json_encode ($ arr_a );

 

The getRand () function is used to calculate the probability.

 

The code is as follows: Copy code
// Calculate the probability
Function getRand ($ proArr ){
$ Result = '';
 
// The total probability precision of the probability array
$ ProSum = array_sum ($ proArr );
 
// Probability array loop
Foreach ($ proArr as $ key => $ proCur ){
$ RandNum = mt_rand (1, $ proSum );
If ($ randNum <= $ proCur ){
$ Result = $ key;
Break;
} Else {
$ ProSum-= $ proCur;
        }
    }
Unset ($ proArr );
Return $ result;
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.