jQuery+HTML实现砸金蛋抽奖活动前端+后台PHP代码实例教程

 更新 2020-09-02 浏览 858次

jQuery+HTML实现砸金蛋抽奖活动前端+后台PHP代码实例教程
描述:本文内容将介绍如何使用jQuery与PHP实现一个WEB网页版砸金蛋抽奖活动的程序代码设计,首先我们需要准备素材,即金蛋图片、砸碎后的金蛋图片、砸碎后的碎花图片、以及锤子四张图片。
基础属性
详细介绍

1、制作前端所需要的HTML

<div class="egg">  
   <ul class="egg_list">  
    <p class="hammer" id="hammer">锤子</p>  
    <p class="result_tip" id="result_tip"><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>

当鼠标滑向金蛋时,锤子会仅靠金蛋右上方,通过position()来控制位置。

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

当挥动锤子砸向金蛋eggClick()前,我们先把金蛋中的数字编号隐藏起来。

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

然后我们需要向后台ajax.php发送一个ajax请求,处理后台的php程序会将中奖结果返回。砸锤子的动画我们使用animate()来实现,通过改变锤子的top和left位置来实现简单的动画效果,锤子砸下去后,金蛋样式变为.curruent,实现用户锤子砸下去后看到的是金花四溅并展现中奖结果,具体可以看下面的eggClick方法:

function eggClick(obj) { 
    $.get("ajax.php",function(data) { 
        if (obj.hasClass("current")) { 
            alert("蛋都碎了一地,刷新重新来过吧!"); 
            return false; 
        } 
        $(".hammer").css({ 
            "top": obj.position().top - 55, 
            "left": obj.position().left + 185 
        }); 
        $(".hammer").animate({ 
            "top": obj.position().top - 25, 
            "left": obj.position().left + 125 
        },30, function() { 
            obj.addClass("current"); //蛋碎效果 
            obj.find("sup").show(); //金花四溅 
            $(".hammer").hide(); 
            $('.result_tip').css({ 
                display: 'block', 
                top: '100px', 
                left: obj.position().left + 45, 
                opacity: 0 
            }).animate({ 
                top: '50px', 
                opacity: 1 
            }, 
            300, 
            function() { 
                if (data.msg == 1) { 
                    $("#result").html("恭喜您中得" + data.prize_title + "!"); 
                } else { 
                    $("#result").html("Sorry,您没能中奖!"); 
                } 
            }); 
        }); 
    }, 
    "json") 
}

2、PHP代码后台处理

$prize_arr = array( 
    '0' => array('id' => 1, 'title' => 'iphone5s', 'v' => 5), 
    '1' => array('id' => 2, 'title' => '联系笔记本', 'v' => 10), 
    '2' => array('id' => 3, 'title' => '音箱设备', 'v' => 20), 
    '3' => array('id' => 4, 'title' => '30GU盘', 'v' => 30), 
    '4' => array('id' => 5, 'title' => '话费50元', 'v' => 10), 
    '5' => array('id' => 6, 'title' => 'iphone6s', 'v' => 15), 
    '6' => array('id' => 7, 'title' => '谢谢,继续加油哦!~', 'v' => 10), 
); 
  
foreach ($prize_arr as $key => $val) { 
    $arr[$val['id']] = $val['v']; 
} 
  
$prize_id = getRand($arr); //根据概率获取奖品id 
$data['msg'] = ($prize_id == 7) ? 0 : 1; //如果为0则没中  
$data['prize_title'] = $prize_arr[$prize_id - 1]['title']; //中奖奖品 
echo json_encode($data); 
exit; //以json数组返回给前端 
  
function getRand($proArr) { //计算中奖概率 
    $rs = ''; //z中奖结果 
    $proSum = array_sum($proArr); //概率数组的总概率精度 
    //概率数组循环 
    foreach ($proArr as $key => $proCur) { 
        $randNum = mt_rand(1, $proSum); 
        if ($randNum <= $proCur) { 
            $rs = $key; 
            break; 
        } else { 
            $proSum -= $proCur; 
        } 
    } 
    unset($proArr); 
    return $rs; 
}

通过向后台ajax.php文件请求处理获得获取处理结果,我们可以通过修改ajax.php文件来设置我们所需的奖品及中奖概率的调整。

php实例砸金蛋抽奖代码抽奖活动
相关内容