Filling Out the Cases


I got through the exercise but I just wanted to ask why doesn't my code work when in order to move right, I subtract 10px from the right instead of adding 10px to the left?

Was just experimenting so a doubt came in my mind....

$(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:
				$('img').animate({top: "-=10px"}, 'fast');
			// Right Arrow Pressed
			case 39:
				$('img').animate({right: "-=10px"}, 'fast');
			// Down Arrow Pressed
			case 40:
				$('img').animate({top: "+=10px"}, 'fast');


.animate() takes only top and left axes of reference. Change the direction and use the one for vertical or horizontal translation, respectively.


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