【问题标题】:Body stop scrolling after keydown, keypress with jQuery按键后正文停止滚动,使用 jQuery 按键
【发布时间】:2013-01-14 14:47:17
【问题描述】:

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

一切都很好,但是当我滚动时,用我的 div 按键也会滚动整个页面。是否有任何选项可以停止此正文滚动?

【问题讨论】:

  • 你能发布一个 jsfiddle.net 的例子吗?

标签: jquery scroll keypress


【解决方案1】:

你需要.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);
    }
});

【讨论】:

    【解决方案2】:

    如果你的身体是当前有滚动动画的,那么使用stop()http://api.jquery.com/stop/

    $('body').stop();
    

    【讨论】:

    • 这不是动画 - 这是在页面上按向上或向下的默认行为。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多