JQuery手速測(cè)試小游戲?qū)崿F(xiàn)思路詳解
(-1)寫(xiě)在前面
我用的chrome49,jquery3.0,我得到過(guò)399分,信不信由你。
(1)設(shè)計(jì)思路
兩個(gè)p元素放在div里,每個(gè)p元素的高度和寬度都和div一樣,當(dāng)鼠標(biāo)放在div上時(shí),第一個(gè)p向上移動(dòng)(更改marginTop值),當(dāng)鼠標(biāo)離開(kāi)div上時(shí),第一個(gè)p向下移動(dòng)。
(2)知識(shí)儲(chǔ)備
a. :nth-child
#lol p:nth-child(1),當(dāng)前元素p相對(duì)于他的父元素的所有子元素,如果第一個(gè)元素是p則匹配成功。
#lol :nth-child(1) 相當(dāng)于#lol *:nth-child(1)
(3) 代碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset=utf-8> <script type="text/javascript" src="debug-jquery-3.0.0.js"></script> <link rel="shortcut icon" href="favicon.ico" type="image/x-icon"> <title>為了生活</title> <style type="text/css"> * { margin:0px; padding:0; } body { position:absolute; } #lol { width:400px; height:400px; position:absolute; overflow:hidden; cursor:pointer; } #lol p { width:400px; height:400px; } #lol p:nth-child(1) { background:blue; } #lol p:nth-child(2) { background:orange; } </style> <script type="text/javascript"> $(function() { var $lol = $("#lol"), $oneP = $lol.children().first(), $score = $("#score"); $lol.centerPos(); $lol.hover(function() { $oneP.animate({marginTop:-$oneP.height()},400) },function() { var number = -parseInt($oneP.css("marginTop")); if(number == $oneP[0].offsetHeight) { number = 0; } $(score).text(number); $oneP.stop(true,false).animate({marginTop:0},400); }) }) $.fn.centerPos = function() { var parent; this.each(function(index) { parent = this.parentNode; if(parent == document.body) { parent = window; } var position = $(this).css("position"); if(position == "fixed" || position=="absolute") { $(this).css("left",($(parent).width()-this.offsetWidth)/2+"px") .css("top",($(parent).height()-this.offsetHeight)/2+"px"); } else { window.console.error("沒(méi)有設(shè)置有效的position值"); } }) return this; } </script> </head <body> <div>當(dāng)前得分:<span id="score">0</span>分</div> <div id="lol"> <p></p> <p></p> </div> </body> </html>
以上所述是小編給大家介紹的JQuery手速測(cè)試小游戲?qū)崿F(xiàn)思路詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
jquery實(shí)現(xiàn)頁(yè)面圖片等比例放大縮小功能
本文將利用jquery實(shí)現(xiàn)頁(yè)面圖片等比例放大和縮小。說(shuō)明: 頁(yè)面中經(jīng)常需要將未知大小的圖片展示在有限的空間里, 如果直接指定圖片的width和height值, 就有可能造成圖片走樣, 這段代碼就是為解決這個(gè)問(wèn)題設(shè)計(jì)2014-02-02jquery 面包屑導(dǎo)航 具體實(shí)現(xiàn)
jquery 面包屑導(dǎo)航 具體實(shí)現(xiàn),需要的朋友可以參考一下2013-06-06Jquery+Ajax+PHP+MySQL實(shí)現(xiàn)分類(lèi)列表管理(下)
本文將采用Jquery+Ajax+PHP+MySQL來(lái)實(shí)現(xiàn)一個(gè)客戶分類(lèi)列表的管理,如何利用Ajax和Json技術(shù)讓用戶操作起來(lái)覺(jué)得更輕松,感興趣的小伙伴們可以參考一下2015-10-10jquery實(shí)現(xiàn)全選功能效果的實(shí)現(xiàn)代碼
下面小編就為大家?guī)?lái)一篇jquery實(shí)現(xiàn)全選功能效果的實(shí)現(xiàn)代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05jQuery實(shí)現(xiàn)的縱向下拉菜單實(shí)例詳解【附demo源碼下載】
這篇文章主要介紹了jQuery實(shí)現(xiàn)的縱向下拉菜單,結(jié)合實(shí)例形式分析了jQuery動(dòng)態(tài)操作頁(yè)面元素實(shí)現(xiàn)縱向下拉菜單的步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-07-07jQuery中outerWidth()方法用法實(shí)例
這篇文章主要介紹了jQuery中outerWidth()方法用法,實(shí)例分析了outerWidth()方法的功能、定義及獲取第一個(gè)匹配元素外部寬度的使用技巧,需要的朋友可以參考下2015-01-01