JQuery手速測試小游戲?qū)崿F(xiàn)思路詳解
(-1)寫在前面
我用的chrome49,jquery3.0,我得到過399分,信不信由你。
(1)設(shè)計思路
兩個p元素放在div里,每個p元素的高度和寬度都和div一樣,當(dāng)鼠標(biāo)放在div上時,第一個p向上移動(更改marginTop值),當(dāng)鼠標(biāo)離開div上時,第一個p向下移動。
(2)知識儲備
a. :nth-child
#lol p:nth-child(1),當(dāng)前元素p相對于他的父元素的所有子元素,如果第一個元素是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("沒有設(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手速測試小游戲?qū)崿F(xiàn)思路詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
jquery實(shí)現(xiàn)頁面圖片等比例放大縮小功能
本文將利用jquery實(shí)現(xiàn)頁面圖片等比例放大和縮小。說明: 頁面中經(jīng)常需要將未知大小的圖片展示在有限的空間里, 如果直接指定圖片的width和height值, 就有可能造成圖片走樣, 這段代碼就是為解決這個問題設(shè)計2014-02-02
jquery 面包屑導(dǎo)航 具體實(shí)現(xiàn)
jquery 面包屑導(dǎo)航 具體實(shí)現(xiàn),需要的朋友可以參考一下2013-06-06
Jquery+Ajax+PHP+MySQL實(shí)現(xiàn)分類列表管理(下)
本文將采用Jquery+Ajax+PHP+MySQL來實(shí)現(xiàn)一個客戶分類列表的管理,如何利用Ajax和Json技術(shù)讓用戶操作起來覺得更輕松,感興趣的小伙伴們可以參考一下2015-10-10
jquery實(shí)現(xiàn)全選功能效果的實(shí)現(xiàn)代碼
下面小編就為大家?guī)硪黄猨query實(shí)現(xiàn)全選功能效果的實(shí)現(xiàn)代碼。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05
jQuery實(shí)現(xiàn)的縱向下拉菜單實(shí)例詳解【附demo源碼下載】
這篇文章主要介紹了jQuery實(shí)現(xiàn)的縱向下拉菜單,結(jié)合實(shí)例形式分析了jQuery動態(tài)操作頁面元素實(shí)現(xiàn)縱向下拉菜單的步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-07-07
jQuery中outerWidth()方法用法實(shí)例
這篇文章主要介紹了jQuery中outerWidth()方法用法,實(shí)例分析了outerWidth()方法的功能、定義及獲取第一個匹配元素外部寬度的使用技巧,需要的朋友可以參考下2015-01-01

