Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Jquery - Navigation of Object with Arrow Keys

I am trying to create code so I may navigate a picture of a ball (in div on my html) with arrow keys. I read somewhere that using "KeyCode" may cause cross browser compatibility issues. Does anyone know anything about that? - I have thus amended my code to use "which". But more importantly ... my below code seems to work fine in the beginning. But as soon as I use either the "right" or "down" arrow, I can not use the "up" and "left" arrow anymore. They work fine if I use the "up" and "left" arrow before I use either of the other two. I am thinking this somehow has to do with defining the object position, but could not find the solution online. Your help is much appreciated.

$(document).ready(function() {
  $("body").keydown(function(e) {
    e.preventDefault(); // prevent the default action (scroll / move caret)
    switch(e.which) {
        case 37: // left
          $("div").animate({
          right: "+=100"
          });
        break;
        case 38: // up
          $("div").animate({
          bottom: "+=100"
          });
        break;
        case 39: // right
          $("div").animate({
          left: "+=100"
          });
        break;
        case 40: // down
          $("div").animate({
          top: "+=100"
          }); 
        break;
    }
    });
});

2 Answers

L B
L B
28,323 Points

left and top are not valid parameters that are part of animate. Instead you need -=100. Here is my revised code:

$(document).ready(function () {
    $div_name = "#item"; //Name of the div you wish to control
    $speed = 200; //Higher is slower
    $move = "100"; //Amount you want to move the element by
    $("body").keydown(function (e) {
        e.preventDefault();
        switch (e.keyCode) {
            case 37:
                //left
                $($div_name).animate({
                    right: "+=" + $move
                }, $speed, function () {});
                break;
            case 38:
                //up
                $($div_name).animate({
                    bottom: "+=" + $move
                }, $speed, function () {});
                break;
            case 39:
                //right
                $($div_name).animate({
                    right: "-=" + $move
                }, $speed, function () {});
                break;
            case 40:
                //down
                $($div_name).animate({
                    bottom: "-=" + $move
                }, $speed, function () {});
                break;
        }
    });
});

Or if you wish to see it in action: https://jsfiddle.net/7gpubyk3/2/

Thanks again!