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 –