Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
109
rated 0 times [  113] [ 4]  / answers: 1 / hits: 31313  / 11 Years ago, fri, august 30, 2013, 12:00:00

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.


More From » html

 Answers
20

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.


[#76016] Thursday, August 29, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brodyfrancisi

Total Points: 1
Total Questions: 102
Total Answers: 89

Location: Marshall Islands
Member since Mon, May 31, 2021
3 Years ago
brodyfrancisi questions
;