Body stop scrolling after keydown, keypress with jQuery

自作多情 提交于 2019-12-10 18:04:25

问题


I have some scrolling actions after keydown, keypress pressing. So this is what it looks like:

$(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 is working fine but when im scrolling, keypressing with my div scrolls also whole page. Is there any option to stop this body scrolling?


回答1:


You need .preventDefault() in 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);
    }
});



回答2:


If your body is what currently has the scrolling animation then use stop(). http://api.jquery.com/stop/

$('body').stop();


来源:https://stackoverflow.com/questions/14320634/body-stop-scrolling-after-keydown-keypress-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!