在keydown,使用jQuery按键后停止滚动

Luk*_*kas 3 jquery scroll keypress

在keydown,按下按键后我有一些滚动动作.所以这就是它的样子:

$(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
    }
});
Run Code Online (Sandbox Code Playgroud)

Everithing工作正常,但是当我滚动时,使用我的div按键也会整页滚动.有没有选择阻止这个身体滚动?

Arc*_*her 5

你需要.preventDefault()在那里......

$(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);
    }
});
Run Code Online (Sandbox Code Playgroud)