在以下代码中;按左箭头键时,#sprite
应设为动画left: 5%
;但没有做任何事情。请帮忙;谢谢!这是我的代码:
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link href='http://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
<style>
#sprite {
background-color:#0000ff;
height:75px;
width:75px;
top:75%;
left:50%;
position:fixed;
margin-left:-37px;
}
</style>
<script>
$(function() {
$(document).keypress(function(e) {
if (e.which == 37) {
$("#sprite").animate({ "left": "+=5%" }, "slow" );
}
});
});
</script>
</head>
<body>
<div id="game">
<div id="sprite"></div>
</div>
</body>
答案 0 :(得分:1)
你有一个错字:
$( "#spirte" )
应该是:
$( "#sprite" ).animate({ "left": "+=5%" }, "slow" );
完整代码:
$(document).keydown(function(event) {
console.log(event.which);
if (event.which == 37)
{
$("#sprite").animate({ "left": "+=5%" }, "slow" );
}
});