Advertisement
vedranvinko

JK

May 12th, 2019
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var allDivs = document.getElementsByTagName('div');
  2.  
  3. var c = 0;
  4.  
  5. function next() {
  6.     c = c + 1;
  7.     c = c % allDivs.length;
  8.     return allDivs[c];
  9. }
  10.  
  11. function prev() {
  12.     if (c === 0) {
  13.         c = allDivs.length;
  14.     }
  15.     c = c - 1;
  16.     return allDivs[c];
  17. }
  18.  
  19. function checkKey(e) {
  20.     e = e || window.event;
  21.  
  22.     // J
  23.     if (e.keyCode == 74) {
  24.         var current = allDivs[c];
  25.         current.classList.remove('active');
  26.         var p = prev();
  27.         p.classList.add('active');
  28.     }
  29.     else if (e.keyCode == 75) {
  30.         var current = allDivs[c];
  31.         current.classList.remove('active');
  32.         var n = next();
  33.         n.classList.add('active');
  34.     }
  35.     else if (e.keyCode == 67) {
  36.         for (var i = 0; i < allDivs.length; i++) {
  37.             allDivs[i].classList.remove('active');
  38.         }
  39.     }
  40. }
  41.  
  42. document.onkeydown = checkKey;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement