HTML prevent space bar from scrolling page

后端 未结 4 1135
粉色の甜心
粉色の甜心 2020-12-01 13:51

I\'m using the code:

window.onkeydown = function(e) { 
    return !(e.keyCode == 32);
};

which does exactly what I want, stops the page fro

4条回答
  •  广开言路
    2020-12-01 14:19

    Try checking if target is the body:

    window.addEventListener('keydown', function(e) {
      if(e.keyCode == 32 && e.target == document.body) {
        e.preventDefault();
      }
    });
    body { height: 100000px; }
    
    

    Demo

提交回复
热议问题