Home > WeChat Applet > WeChat Development > WeChat public platform development hits golden eggs

WeChat public platform development hits golden eggs

高洛峰
Release: 2017-03-07 10:30:53
Original
1773 people have browsed it

Smashing golden eggs is widely used in celebrations, business promotions, TV entertainment and other occasions. Its fun and suspense can quickly activate the atmosphere of the scene. Similarly, we can also apply Golden Egg Smashing to WEB websites to carry out online activities. This article will use jQuery and PHP to explain how to implement a WEB golden egg smashing program.

WeChat public platform development hits golden eggs

Preparation

We need to prepare props (materials), that is, related pictures, including golden egg pictures , 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-breaking platform. There are three golden eggs numbered 1, 2, and 3 on the platform, as well as a hammer. We construct the following html code:

<p 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> </p>
Copy after login

In the above code, .hammer is used to place the hammer, and .resultTip is used to display the result after smashing the egg, that is, whether there is a prize or not. Place 3 golden eggs in each of the three li's. 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;}
Copy after login


According to the above code we can see a complete golden egg breaking scene on the page. Note that we use png Pictures, if your customers are still using IE6, you may need to process the transparency of png pictures, which is not dealt with in this article.

jQuery

Next, we will use jQuery code to realize 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(&#39;left&#39;, posL); 
})
Copy after login

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)); 
});
Copy after login

Finally, in the custom function eggClick(), we use jQuery’s $.getJSON method to send an ajax request to the background data.php, and the background php The program will handle the prize 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 is smashed, the golden egg style changes to .curr, while gold flowers splash, and then the winning result is achieved. 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-breaking function eggClick()

function eggClick(obj) { 
    var _this = obj; 
    $.getJSON("data.php",function(res){//ajax请求 
        _this.unbind(&#39;click&#39;); //解除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();//隐藏锤子 
                $(&#39;.resultTip&#39;).css({display:&#39;block&#39;,top:&#39;100px&#39;,left:_this.position(). 
                left+45,opacity:0}) 
                .animate({top: &#39;50px&#39;,opacity:1},300,function(){//中奖结果动画 
                    if(res.msg==1){//返回结果 
                        $("#result").html("恭喜,您中得"+res.prize+"!"); 
                    }else{ 
                        $("#result").html("很遗憾,您没能中奖!"); 
                    } 
                });     
            } 
        ); 
    }); 
}
Copy after login

In order to integrate the golden egg-breaking program into your website more realistically, you can Measures such as verifying membership status before making an egg, limiting the number of times you smash an egg, leaving your contact information after winning a prize, etc. It all depends on the needs of the website.

PHP

data.php processes the ajax request sent by the front end. We use the probability algorithm to output the winning results in json format according to the set winning probability. <br/><br/>

$prize_arr = array( 
    &#39;0&#39; => array(&#39;id&#39;=>1,&#39;prize&#39;=>&#39;平板电脑&#39;,&#39;v&#39;=>3), 
    &#39;1&#39; => array(&#39;id&#39;=>2,&#39;prize&#39;=>&#39;数码相机&#39;,&#39;v&#39;=>5), 
    &#39;2&#39; => array(&#39;id&#39;=>3,&#39;prize&#39;=>&#39;音箱设备&#39;,&#39;v&#39;=>10), 
    &#39;3&#39; => array(&#39;id&#39;=>4,&#39;prize&#39;=>&#39;4G优盘&#39;,&#39;v&#39;=>12), 
    &#39;4&#39; => array(&#39;id&#39;=>5,&#39;prize&#39;=>&#39;Q币10元&#39;,&#39;v&#39;=>20), 
    &#39;5&#39; => array(&#39;id&#39;=>6,&#39;prize&#39;=>&#39;下次没准就能中哦&#39;,&#39;v&#39;=>50), ); 
 
foreach ($prize_arr as $key => $val) { 
    $arr[$val[&#39;id&#39;]] = $val[&#39;v&#39;]; 
} 
 
$rid = getRand($arr); //根据概率获取奖项id $res[&#39;msg&#39;] = ($rid==6)?0:1; //如果为0则没中 $res[&#39;prize&#39;] = $prize_arr[$rid-1][&#39;prize&#39;]; //中奖项 echo json_encode($res); 
 
//计算概率 function getRand($proArr) { 
    $result = &#39;&#39;; 
 
    //概率数组的总概率精度 
    $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; 
}
Copy after login

By setting the probability, we can see that the chance of hitting the tablet is 3%. The chance of missing is 50%.

For more articles related to WeChat public platform development and smashing golden eggs, please pay attention to the PHP Chinese website!

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template