


Write a golden egg winning program based on JQuery+PHP, jquery wins_PHP tutorial
Write a golden egg winning program based on JQuery+PHP, jquery wins the prize
First of all, I will show you the rendering:
View demo Download source code
Preparation
We need to prepare props (materials), that is, related pictures, including pictures of golden eggs, pictures of smashed eggs, pictures of smashed flowers, and pictures of hammers.
HTML
What we want to show on our page is a golden egg smashing platform. There are three golden eggs numbered 1, 2, and 3 on the platform, as well as a hammer. We build the following html code:
<div class="egg"> <ul class="eggList"> <p class="hammer" id="hammer">锤子</p> <p class="resultTip" id="resultTip"><b id="result"></b></p> <li><span>1</span><sup></sup></li> <li><span>2</span><sup></sup></li> <li><span>3</span><sup></sup></li> </ul> </div>
In the above code, .hammer is used to place the hammer, .resultTip is used to display the results after smashing the eggs, that is, whether there is a prize or not. Three li's are used to place 3 golden eggs respectively. We use CSS to decorate the effect.
CSS
.egg{width:660px; height:400px; margin:50px auto 20px auto;} .egg ul li{z-index:999;} .eggList{padding-top:110px;position:relative;width:660px;} .eggList li{float:left;background:url(images/egg_1.png) no-repeat bottom;width:158px; height:187px;cursor:pointer;position:relative;margin-left:35px;} .eggList li span{position:absolute; width:30px; height:60px; left:68px; top:64px; color:#ff0; font-size:42px; font-weight:bold} .eggList li.curr{background:url(images/egg_2.png) no-repeat bottom;cursor:default;z-index:300;} .eggList li.curr sup{position:absolute;background:url(images/img-4.png) no-repeat;width:232px; height:181px;top:-36px;left:-34px;z-index:800;} .hammer{background:url(images/img-6.png) no-repeat;width:74px;height:87px;position:absolute; text-indent:-9999px;z-index:150;left:168px;top:100px;} .resultTip{position:absolute; background:#ffc ;width:148px;padding:6px;z-index:500;top:200px; left:10px; color:#f60; text-align:center;overflow:hidden;display:none;z-index:500;} .resultTip b{font-size:14px;line-height:24px;}
According to the above code, we can see a complete golden egg breaking scene on the page. Note that we use png images. If your customers are still using IE6, you may need to process the transparency of the png images. This article will not deal with it.
jQuery
Next, we will use jQuery code to implement the entire process of smashing golden eggs, breaking eggs, and displaying the winning results. Of course, the old rule is that for example programs implemented with jQuery, you must first load the jQuery library file.
First of all, when the mouse slides towards the golden egg, the hammer used to smash the golden egg will only be on the upper right side of the golden egg, which can be positioned using position().
$(".eggList li").hover(function() { var posL = $(this).position().left + $(this).width(); $("#hammer").show().css('left', posL); })
Then, click on the golden egg, which is the process of swinging the hammer to hit the golden egg. We first hide the numbers in the golden eggs in click, and then call the custom function eggClick().
$(".eggList li").click(function() { $(this).children("span").hide(); eggClick($(this)); });
Finally, in the custom function eggClick(), we use jQuery’s $.getJSON method to send an ajax request to the background data.php. The background PHP program will handle the award distribution and return the winning results. We use animate() to realize the animation of smashing the hammer, and achieve a simple animation effect by changing the top and left positions of the hammer. After the hammer hits, the golden egg style changes to .curr, while gold flowers splash, and then the winning result. resultTip shows that whether you win or not depends on your luck and the winning probability set by the background awards. Let’s look at the code of the golden egg function eggClick():
function eggClick(obj) { var _this = obj; $.getJSON("data.php",function(res){//ajax请求 _this.unbind('click'); //解除click $(".hammer").css({"top":_this.position().top-55,"left":_this.position().left+185}); $(".hammer").animate({//锤子动画 "top":_this.position().top-25, "left":_this.position().left+125 },30,function(){ _this.addClass("curr"); //蛋碎效果 _this.find("sup").show(); //金花四溅 $(".hammer").hide();//隐藏锤子 $('.resultTip').css({display:'block',top:'100px',left:_this.position(). left+45,opacity:0}) .animate({top: '50px',opacity:1},300,function(){//中奖结果动画 if(res.msg==1){//返回结果 $("#result").html("恭喜,您中得"+res.prize+"!"); }else{ $("#result").html("很遗憾,您没能中奖!"); } }); } ); }); }
In order to integrate the golden egg smashing program into your website more truly, you can verify your membership status before smashing eggs, limit the number of eggs smashed, leave your contact information after smashing eggs and winning, etc., depending on the needs of the website. .
PHP
data.php processes the ajax request sent by the front end. We use probability algorithm to output the winning results in json format according to the set winning probability. For examples of probability calculations, please refer to: PHP+jQuery to implement flip lottery
$prize_arr = array( '0' => array('id'=>1,'prize'=>'平板电脑','v'=>3), '1' => array('id'=>2,'prize'=>'数码相机','v'=>5), '2' => array('id'=>3,'prize'=>'音箱设备','v'=>10), '3' => array('id'=>4,'prize'=>'4G优盘','v'=>12), '4' => array('id'=>5,'prize'=>'Q币10元','v'=>20), '5' => array('id'=>6,'prize'=>'下次没准就能中哦','v'=>50), ); foreach ($prize_arr as $key => $val) { $arr[$val['id']] = $val['v']; } $rid = getRand($arr); //根据概率获取奖项id $res['msg'] = ($rid==6)?0:1; //如果为0则没中 $res['prize'] = $prize_arr[$rid-1]['prize']; //中奖项 echo json_encode($res); //计算概率 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; }
By setting the probability, we can see that the probability of hitting the tablet is 3%, and the probability of not hitting it is 50%. Click on the demo to try your luck.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,
