Pressing key switch


#1

https://www.codecademy.com/courses/web-beginner-en-JwhI1/2/3

This code is not efficient. What i am missing is the key up down, etc. Need help coming up with solution to fix this, which was in the last lesson. Can’t go back in the lesson.

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


#2

animate does not have a sense of direction, but relative offsets from the top and left boundaries.

top: '-=10px'  => move up
top: '+=10px'  => move down

left: '-=10px'  => move left
left: '+=10px'  => move right

#3

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