砸金蛋被广泛应用于庆典活动、商家促销、电视娱乐等场合,它的趣味、悬念能迅速活跃现场气氛。同样,我们也可以将砸金蛋应用到WEB网站上,用于开展线上活动。本文将使用jQuery与PHP讲解如何实现一个WEB砸金蛋程序。


准备

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

HTML

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

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

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

CSS

.egg{width:660pxheight:400pxmargin: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:158pxheight:187px;cursor:pointer;position:relative;margin-left:35px;}  .eggList li span{position:absolutewidth:30pxheight:60pxleft:68pxtop:64pxcolor:#ff0; 
 font-size:42pxfont-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:absolutebackground:#ffc ;width:148px;padding:6px;z-index:500;top:200px;   left:10pxcolor:#f60text-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 <= $proCur) { 
            $result = $key; 
            break; 
        } else { 
            $proSum -= $proCur; 
        } 
    } 
    unset ($proArr); 
 
    return $result; 
} 

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


示例源码下载:立即下载


原文出自:http://www.helloweba.com/view-blog-218.html


版权声明:若无特殊注明,本文皆为( yueshuo )原创,转载请保留文章出处。