HTML:
<div id="Container" class="Container">
<div id="PLayer" class="player" ></div>
</div>
CSS:
<style type="text/css">
.Container
{
width:200px;
height:200px;
}
.player
{
width:10px;
height:10px;
background-color:Red;
position: absolute;
}
</style>
JS:
$("body").keydown(function (e) {
var KeyID = e.KeyCode || e.which;
if (KeyID === 39) //right
{
$("#Player").animate({ 'right': '20px' });
}
});
但球員唐似乎沒有任何建議?
它不適用於大寫的「P」 - 類有小寫的「p」。 – nnnnnn
謝謝你這個完美的答案,但我怎麼做一個測試,以檢查玩家是否超過父級的邊界? – Sora
您可以通過在執行此行之前檢查條件來執行此操作。 你可以像這樣獲得你的parrent div的位置。 var a = $(yourparrentDiv).position()因此在此之後,您可以訪問像 a.top或a.ftft –