Removing body scroll after pressing a key, pressing a jQuery button

I have some scrolling after keystrokes, keystrokes. So it looks like this:

$(document).keydown(function(e) {
    e.stopPropagation();

    if (e.keyCode === 40) {
        // some scrolling actions in specifie div
    } else if (e.keyCode === 38) {
        // some scrolling actions in specifie div
    }
});

Everithing works fine, but when scrolling im, keystrokes with my scrolling divs are also a whole page. Is there any way to stop the scrolling of this body?

+3
source share
2 answers

You need .preventDefault()there ...

$(document).keydown(function(e) {
    e.stopPropagation();
    if (e.keyCode === 40) {
        e.preventDefault();
        console.log(40);
    } else if (e.keyCode === 38) {
        e.preventDefault();
        console.log(38);
    }
});
+5
source

If your body has scroll animation, use stop(). http://api.jquery.com/stop/

$('body').stop();
-2
source

All Articles