在Firefox上按空格键时停用向下滚动 [英] Disable scroll down when spacebar is pressed on firefox
问题描述
我想禁用向下滚动时,我按下空格键。这只发生在firefox。
I want to disable the scroll down when i pressed the spacebar. This only happens in firefox.
我已经使用overflow:hidden和元标记视口。
I already use overflow:hidden and meta tag viewport.
谢谢。 / p>
Thanks.
推荐答案
这应该可以做到。它说,当在页面/文档上按空格键时,它不仅仅防止其默认行为,而是还原到原始状态。
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似乎包括preventDefault。 来源
return false seems to include preventDefault. Source
检查JQuery API了解关键事件事件的详情 - http://api.jquery.com/keydown/
Check JQuery API's for more information about keydown events - http://api.jquery.com/keydown/
window.onkeydown = function(e) {
return !(e.keyCode == 32);
};
JQuery示例
$(document).keydown(function(e) {
if (e.which == 32) {
return false;
}
});
EDIT :
As @ amber-de-black表示上述代码将阻止按HTML输入上的空格键。要解决这个问题,你 e.target
在这里你想要空格键阻塞。这可以防止空格键阻止其他元素(如HTML输入)。
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.
在这种情况下,我们指定空格键和body目标。这将阻止输入被阻止。
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();
}
};
注意:如果您使用JQuery,请使用 e.which
而不是 e.keyCode
来源。
NOTE: If you're using JQuery use e.which
instead of e.keyCode
Source.
event.which属性规范化event.keyCode和event.charCode
JQuery作为各种事件的规范化器。如果这让任何人读到这个惊喜。我建议您阅读他们的事件对象文档。
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 Object documentation.
这篇关于在Firefox上按空格键时停用向下滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!