简体   繁体   English

按下箭头键是否可以防止文档滚动?

[英]Is it possible to prevent document scrolling when arrow keys are pressed?

Very straightforward: 非常简单:

Is it possible to prevent the browser from scrolling when a user pressed arrow keys? 当用户按下箭头键时,是否可以防止浏览器滚动?

yes. 是。 use something like: document.getElementById('yourID').onkeypress = HandleKeyPress; 使用类似的东西:document.getElementById('yourID')。onkeypress = HandleKeyPress;

function HandleKeyPress(e) {
    var e = e || window.event;
    switch (e.keyCode) {

        case e.DOM_VK_LEFT:
        case e.DOM_VK_RIGHT:
        case e.DOM_VK_UP:
        case e.DOM_VK_DOWN:
            if (e.preventDefault)
                e.preventDefault();
            else e.returnValue = false;
    }
}

Though it may not be a good idea to do so. 尽管这样做可能不是一个好主意。 just be sure that you're not overlooking a better approach. 只要确保您没有忽略更好的方法即可。

Very straightforward: Yes, but don't do it. 非常简单:是的,但是不要这样做。

Changing fundamental operation of how a browser operates just confuses or angers users, and makes the whole experience less user-friendly. 改变浏览器操作方式的基本操作只会使用户感到困惑或激怒,并使整个体验变得不那么用户友好。 The user should have ultimate control of the way his or her browser functions, not you. 用户应该完全控制自己的浏览器功能,而不是您自己。 This is similar to blocking menu items from being accessed, or removing context menus etc. 这类似于阻止菜单项被访问或删除上下文菜单等。

It seems many are saying not to break what the browser does. 似乎许多人都在说不要破坏浏览器的功能。

I would argue the same, unless you are replacing it with similar or better functionality. 我会说同样的话,除非您要用相似或更好的功能替换它。

I frequently kill the keyboard scroll in elements with an overflow because I've added a keyboard event to the element where using the keyboard "down" and "up" keys selects the items (think of a finder or windows explorer window). 我经常在溢出时杀死元素中的键盘滚动,因为我已经向元素添加了键盘事件,在该事件中,使用键盘的“向下”和“向上”键可以选择项目(例如finder或Windows Explorer窗口)。

The default scroll made the interaction all whacky. 默认的滚动使交互变得很怪异。 If the top item were selected, then you key 'down', it would select the next item but then scroll the element down and hide what was just selected. 如果选择了第一项,则您按下“向下”键,它将选择下一项,但是向下滚动元素并隐藏刚刚选择的内容。

So I break the default scroll, and then add my own to scroll to the selected item if it is below (or above) the current scrolling view. 因此,我中断了默认滚动,然后添加自己的滚动到所选项目(如果它位于当前滚动视图的下方(或上方))。 That way it behaves exactly as any OS out there when key-ing up and down through files. 这样,当通过文件向上和向下键入文件时,它的行为就与任何其他操作系统完全一样。

Just saying, break it all you want, but make sure you know why you're breaking it and the user doesn't notice a thing. 只是说,将其破坏就可以了,但是要确保知道为什么要破坏它,并且用户不会注意到任何东西。

在jQuery中,您可以执行以下操作:

$().keydown(function(e) { return e.keyCode != 38 && e.keyCode != 40; });

listen to the keydown event and prevent the default behavior. 侦听keydown事件并阻止默认行为。

In jquery 在jQuery中

$(document).on('keydown.yournamespace', function(e) {e.preventDefault();});

可能吧,但是打破一个大家都知道和理解的约定通常不是一个好主意:D

在jQuery中

$("*").keypress(function(e){e.preventDefault(); return false;});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM