Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable scroll down when spacebar is pressed on firefox [duplicate]

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.

like image 869
Alberto Castro Yepiz Avatar asked Aug 30 '13 00:08

Alberto Castro Yepiz


People also ask

How do I stop the spacebar scrolling down?

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.

How do I disable middle mouse scrolling in Firefox?

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.

How do I disable scroll wheel in Firefox?

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.

Why is space bar paging down?

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.


2 Answers

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.

like image 126
Michael Schwartz Avatar answered Oct 02 '22 10:10

Michael Schwartz


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);
like image 29
João Paulo Macedo Avatar answered Oct 02 '22 10:10

João Paulo Macedo