按下按键后,主体停止滚动,使用jQuery按键 [英] Body stop scrolling after keydown, keypress with jQuery

查看:77
本文介绍了按下按键后,主体停止滚动,使用jQuery按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
    }
});

一切正常,但是在即时滚动时,用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?

推荐答案

您需要在其中.preventDefault() ...

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);
    }
});

这篇关于按下按键后,主体停止滚动,使用jQuery按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆