Html 在 Firefox 上按下空格键时禁用向下滚动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18522864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable scroll down when spacebar is pressed on firefox
提问by Alberto Castro Yepiz
I want to disable the scroll down when i pressed the spacebar. This only happens in firefox.
我想在按下空格键时禁用向下滚动。这只发生在 Firefox 中。
I already use overflow:hidden and meta tag viewport.
我已经使用了溢出:隐藏和元标记视口。
Thanks.
谢谢。
回答by Michael Schwartz
This should do the trick. It states that when the spacebar is pressed on the page/document it doesn't just prevent its default behavior, but reverts back to original state.
这应该可以解决问题。它指出,当在页面/文档上按下空格键时,它不仅会阻止其默认行为,还会恢复到原始状态。
return false seems to include preventDefault. Source
return false 似乎包括 preventDefault。来源
Check JQuery API's for more information about keydown events - http://api.jquery.com/keydown/
检查 JQuery API 以获取有关 keydown 事件的更多信息 - http://api.jquery.com/keydown/
window.onkeydown = function(e) {
return !(e.keyCode == 32);
};
JQuery example
JQuery 示例
$(document).keydown(function(e) {
if (e.which == 32) {
return false;
}
});
EDIT:
编辑:
As @amber-de-black stated "the above code will block pressing space key on HTML inputs". To fix this you e.target
where exactly you want spacebar blocked. This can prevent the spacebar blocking other elements like HTML inputs.
正如@amber-de-black 所说“上面的代码将阻止在 HTML 输入上按空格键”。为了解决这个问题,你e.target
到底想要空格键被阻止。这可以防止空格键阻塞其他元素,如 HTML 输入。
In this case we specify the spacebar along with the body target. This will prevent inputs being blocked.
在这种情况下,我们指定空格键和身体目标。这将防止输入被阻塞。
window.onkeydown = function(e) {
if (e.keyCode == 32 && e.target == document.body) {
e.preventDefault();
}
};
NOTE: If you're using JQuery use e.which
instead of e.keyCode
Source.
注意:如果您使用的是 JQuery,请使用e.which
而不是e.keyCode
Source。
The event.which property normalizes event.keyCode and event.charCode
The event.which property normalizes event.keyCode and event.charCode
JQuery acts as a normalizer for a variety of events. If that comes to a surprise to anyone reading this. I recommend reading their Event Objectdocumentation.
JQuery 充当各种事件的规范化器。如果这让任何阅读本文的人感到惊讶。我建议阅读他们的事件对象文档。
回答by banzomaikaka
Detect if the spacebar is being pressed. If it is, then prevent its default behaviour.
检测空格键是否被按下。如果是,则阻止其默认行为。
document.documentElement.addEventListener('keydown', function (e) {
if ( ( e.keycode || e.which ) == 32) {
e.preventDefault();
}
}, false);
回答by S.E.T.
Have you tried capturing the keydown event in javascript? If you are using jQuery you can read more about capturing key events here: http://api.jquery.com/keydown/
您是否尝试过在 javascript 中捕获 keydown 事件?如果您使用 jQuery,您可以在此处阅读有关捕获关键事件的更多信息:http: //api.jquery.com/keydown/
If you aren't you can capture and ignore the space bar keypress as described here: https://stackoverflow.com/a/2343597/1019092
如果您不是,您可以按照此处所述捕获并忽略空格键按键:https: //stackoverflow.com/a/2343597/1019092