search
Homephp教程php手册基于JQuery+PHP编写砸金蛋中奖程序

砸金蛋被广泛应用于庆典活动、商家促销、电视娱乐等场合,它的趣味、悬念能迅速活跃现场气氛,同样,我们也可以将砸金蛋应用到WEB网站上,用于开展线上活动,本

首先给大家展示效果图:

基于JQuery+PHP编写砸金蛋中奖程序

查看演示 下载源码

准备工作

我们需要准备道具(素材),即相关图片,包括金蛋图片、蛋砸碎后的图片、砸碎后的碎花图片、以及锤子图片。

HTML

我们页面上要展现的是一个砸金蛋的台子,台上放了编号为1,2,3的三个金蛋,以及一把锤子。我们构建以下html代码:

    锤子

  • 1
  • 2
  • 3

上述代码中,.hammer放置锤子,.resultTip用于砸蛋后显示的结果,即有没有中奖,三个li分别放置3个金蛋,我们用CSS来装饰下效果。

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;}

按照上面的代码我们可以在页面中看到一个完整的砸金蛋场景,注意我们使用了png图片,如果你的客户仍在使用ie6的话,,你可能需要对png图片的透明做处理,本文不做处理。

jQuery

接下来,我们要用jQuery代码来实现砸金蛋、碎蛋、展示中奖结果的整个过程。当然,老规矩,对于才用jQuery实现的实例程序,你必须先载入jQuery库文件。

首先,当鼠标滑向金蛋时,用于砸金蛋的锤子会仅靠金蛋右上方,可以使用position()来定位。

$(".eggList li").hover(function() { var posL = $(this).position().left + $(this).width(); $("#hammer").show().css('left', posL); })

然后,点击金蛋,即挥动锤子砸向金蛋的过程。我们在click中先把金蛋中的编号数字隐藏,然后调用自定义函数eggClick()。

$(".eggList li").click(function() { $(this).children("span").hide(); eggClick($(this)); });

最后,在自定义函数eggClick()中,我们使用jQuery的$.getJSON方法向后台data.php发送一个ajax请求,后台php程序会处理奖项分配并把中奖结果返回。我们使用animate()来实现砸锤子的动画,通过改变锤子的top和left位子来实现简单的动画效果,锤子砸下去后,金蛋样式变为.curr,同时金花四溅,然后中奖结果.resultTip展示,有没有中奖要看你的运气和后台奖项设置的中奖几率了。来看砸金蛋函数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("很遗憾,您没能中奖!"); } }); } ); }); }

为了将砸金蛋程序更真实的结合到你的网站中,你可以在砸蛋前验证会员身份,限制砸蛋次数、砸蛋中奖后留下联系方式等等措施,具体看网站需求了。

PHP

data.php处理前端发送的ajax请求,我们才用概率算法,根据设置好的中奖概率,将中奖结果以json的格式输出。关于概率计算的例子可以参照:PHP+jQuery实现翻板抽奖

$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

通过设置概率,我们可以看出,砸中平板电脑的几率占3%,砸不中的几率占50%,点击演示demo来试试你的运气吧。

Statement
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.