I have a function that scrolls to the next section of the article when the down arrow on the keyboard is pressed and scrolls when the up arrow is pressed. It works fine, but there is a slight βbounceβ or βshiverβ before scrolling.
I was able to partially fix this problem if the function returns false, however returning false swallows the event with the key pressed, preventing my keyboard from interacting with the browser.
Any ideas on how to eliminate jitter and release the keyboard at the same time?
var $sections = $('.section'); var curr = -1; $(document).keydown(function(e){ prev = (curr < 0)? $sections.length-1: curr-1; next = (curr >= $sections.length-1)? -1: curr+1 ; switch (e.keyCode) { case 38: // up key s = $sections.eq(prev).offset().top; curr = prev; break; case 40: // down key s = $sections.eq(next).offset().top; curr = next; break; default: break; } if (curr == -1 ){ $('html, body').animate({scrollTop: 0}, 'slow'); } else{ $('html, body').animate({scrollTop: s}, 'slow'); } return e; });
source share