Search code examples
javascriptjquerykeyboardkeypressonkeypress

jQuery keypress left/right navigation


I want to give my content slider the ability to respond to keypress (LEFT ARROW key and RIGHT ARROW key) feature. I have read about some conflicts between several browsers and operation systems.

The user can navigate the content while he is on the global website (body).

Pseudo Code:

ON Global Document

IF Key Press LEFT ARROW

THEN animate #showroom css 'left' -980px


IF Key Press RIGHT ARROW

THEN animate #showroom css 'left' +980px

I need a solution without any crossover (Browsers, OSs) conflicts.


Solution

  • $("body").keydown(function(e) {
      if(e.keyCode == 37) { // left
        $("#showroom").animate({
          left: "-=980"
        });
      }
      else if(e.keyCode == 39) { // right
        $("#showroom").animate({
          left: "+=980"
        });
      }
    });