I want to disable the scroll down when i pressed the spacebar. This only happens in firefox.
I already use overflow:hidden and meta tag viewport.
Thanks.
Prevent the Pressing of the Space Bar from Scrolling the Page with JavaScript. To prevent the pressing of the space bar from scrolling the page with JavaScript, we can listen to the keydown event with window. addEventListener . Then in the event handler, we can detect pressing the space bar on the body element.
Here's how: orange Firefox button (or Tools menu) > Options > Advanced On the General mini-tab, uncheck the box for "Use autoscrolling" and OK out.
You can disable this action by setting the related mousewheel pref to 0 (zero) on the about:config page. See: https://support.mozilla.org/en-US/kb/about-config-editor-firefox.
In many applications and browsers pressing the space bar (when you are not in a text entering situation) results in a scroll down. When your mouse cursor is focused on a text box or when typing in a word processor that default function is disabled.
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
Check JQuery API's for more information about keydown events - http://api.jquery.com/keydown/
window.onkeydown = function(e) {
return !(e.keyCode == 32);
};
JQuery example
$(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.
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.
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 Object documentation.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With