Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-06 19:44:27
Original
793 people have browsed it

砸金蛋被广泛应用于庆典活动、商家促销、电视娱乐等场合,它的趣味、悬念能迅速活跃现场气氛,同样,我们也可以将砸金蛋应用到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来试试你的运气吧。

source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!