在解決ul居中問題時想到的幾點
738
2024-10-22
(-1)寫在前面
我用的chrome49,jquery3.0,我得到過399分,信不信由你。
(1)設計思路
兩個p元素放在div里,每個p元素的高度和寬度都和div一樣,當鼠標放在div上時,第一個p向上移動(更改marginTop值),當鼠標離開div上時,第一個p向下移動。
(2)知識儲備
a. :nth-child
#lol p:nth-child(1),當前元素p相對于他的父元素的所有子元素,如果第一個元素是p則匹配成功。
#lol :nth-child(1) 相當于#lol *:nth-child(1)
(3) 代碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset=utf-8> <script type="text/javascript" src="http://www.gimoo.net/t/1812/debug-jquery-3.0.0.js"></script> <link rel="shortcut icon" 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("沒有設置有效的position值"); } }) return this; } </script> </head <body> <div>當前得分:<span id="score">0</span>分</div> <div id="lol"> <p></p> <p></p> </div> </body> </html>
以上所述是小編給大家介紹的JQuery手速測試小游戲實現思路詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對綠夏網網站的支持!
#免責聲明#
本站[綠夏技術導航]提供的一切軟件、教程和內容信息僅限用于學習和研究目的;不得將上述內容用于商業或者非法用途,否則,一切后果請用戶自負。本站信息來自網絡收集整理,版權爭議與本站無關。您必須在下載后的24個小時之內,從您的電腦或手機中徹底刪除上述內容。如果您喜歡該程序或內容,請支持正版,購買注冊,得到更好的正版服務。我們非常重視版權問題,如有侵權請郵件[admin@lxwl520.com]與我們聯系進行刪除處理。敬請諒解!