F
F
faragly2015-07-12 09:29:06
JavaScript
faragly, 2015-07-12 09:29:06

How not to scroll div when pressing UP and DOWN keys?

Hello! There is a dropdown div and it has overflow-x: auto; and you need to navigate through the elements using the up, down, left and right keys, but at the same time so that the div does not scroll, how to do this? I plan to use angular-hotkeys to control keystrokes .

Solution:
I wrote ng-keydown="arrows($event)" in the div, in the function

switch (event.keyCode) {
      case 40:
// здесь выполняю действия по нажатию
      event.preventDefault();
      break;
.....
}

I needed to navigate through the items in the dropdown dropdown div using the arrows on the keyboard, but at the same time the div scrolled.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Inchin ☢, 2015-07-15
@faragly

Vash_div.addEventListener("keydown", function(e){
e = e||event;
e.preventDefault ? e.preventDefault() : (e.returnValue=false);
});

L
Levan, 2015-07-16
@mazgolom

You can do the following to find out the key code and then cancel the default action....... event.keyCode();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question