首先給大家展示效果圖:
查看演示 下載源碼
準備工作
我們需要準備道具(素材),即相關圖片,包括金蛋圖片、蛋砸碎后的圖片、砸碎后的碎花圖片、以及錘子圖片。
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: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); })
新聞熱點
疑難解答