Some keys in jquery don't work


The keys "Up", "Down", "Right" don't work in my code. But the key "Left" works. Why does it work thus?

Replace this line with your code. 
$(document).ready(function() {
    $(document).keydown(function(key) {
        switch(parseInt(key.which,10)) {
			// Left arrow key pressed
			case 37:
				$('img').animate({left: "-=10px"}, 'fast');
			// Up Arrow Pressed
			case 38:
				// Put our code here
				$('img').animate({up: "-=10px"}, 'fast');
			// Right Arrow Pressed
			case 39:
				// Put our code here
				$('img').animate({right: "-=10px"}, 'fast');
			// Down Arrow Pressed
			case 40:
				// Put our code here
				$('img').animate({down: "-=10px"}, 'fast');


I suggest you read the instructions again :slight_smile:

01. The left arrow key on our keyboards translates to number 37 to the computer. When that key is pressed, we animate our image to the left by subtracting 10px
02. To move up we subtract 10px from the top
03. To move right we add 10px to the left
04. Finally, to move down we add 10px to the top

Can you fill in Up, Down, and Right? What happens if you add pixels +=10px instead of subtracting?

so to move down you will use: $('img').animate({top: "+=10px"}, 'fast');
Apply what was said above and change right as well


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.