我是jQuery的新手,一直在Codecademy上学习。我正在网站上创建一个'codebit'(网站),并尝试在按下上、下、左、右键时使图像精灵做出反应(移动)。
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Sprite</title>
<link rel='stylesheet' type='text/css' href='style.css'/>
<script type='text/javascript' src='script.js'></script>
</head>
<body>
<img src="[img]"/>
</body>
</html>
CSS:
img {
position: relative;
left: 0;
top: 0;
}
Javascript:
$(document).ready(function() {
$(document).keydown(function(key) {
switch(parseInt(key.which,10)) {
//LEFT
case 37:
$('img').animate({left: "-=10px"}, 500);
break;
//RIGHT
case 39:
$('img').animate({left: "+=10px"}, 500);
break;
//UP
case 38:
$('img').animate({top: "-=10px"}, 500);
break;
//DOWN
case 40:
$('img').animate({top: "+=10px"}, 500);
break;
}
});
});
我已经检查了一些网站的语法错误,但似乎没有找到任何明显的问题。希望能得到帮助。