I am new to jQuery and studied it through Codecademy. I am creating a “code” (site) on a site, and I am trying to make an image sprite reaction (move) when the up, down, left and right keys are pressed.
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)) {
case 37:
$('img').animate({left: "-=10px"}, 500);
break;
case 39:
$('img').animate({left: "+=10px"}, 500);
break;
case 38:
$('img').animate({top: "-=10px"}, 500);
break;
case 40:
$('img').animate({top: "+=10px"}, 500);
break;
}
});
});
I checked several sites for syntax errors and cannot find any obvious ones. Help would be greatly appreciated.
source
share