On a webpage, certain keyboard keys will control the scrolling of the webpage such like spacebar, arrowup, arrowdown.
I intend to prevent the user from scrolling with these keys when an overlay element is displayed. This overlay element has a default of display: none;.
I understand the sentiment of "do not alter browser's behavior," but scrolling will still be allowed by scroll wheel and/or touchpad (and of course, the scrollbar).
When the overlay is displayed, these keys are only intended for controlling the overlay contents, and not the page scrolling.
Respectively, when the overlay is not displayed, the keys should resume their default behavior of scrolling the page.
Due to this, I'm thinking of using an EventListener, and I found one here:
window.addEventListener("keydown", function(e) { if([" ","ArrowUp","ArrowDown","ArrowLeft","ArrowRight"].indexOf(e.code) > -1) { e.preventDefault(); } }, false); Snippets of example code:
function launchOverlay() { overlay.style.display = "flex"; window.addEventListener("keydown", function(e) { if([" ","ArrowUp","ArrowDown","ArrowLeft","ArrowRight"].indexOf(e.code) > -1) { e.preventDefault(); } }, false); } function closeOverlay() { overlay.style.display = "none"; window.removeEventListener("keydown", function(e) { if([" ","ArrowUp","ArrowDown","ArrowLeft","ArrowRight"].indexOf(e.code) > -1) { e.preventDefault(); } }, false); } document.addEventListener('keydown', function(event) { if (overlay.style.display == "flex") { if (event.code === "Space") { space(); } else if (event.code === "ArrowUp") { aUp(); } else if (event.code === "ArrowDown") { aDown(); } else if (event.code === "ArrowLeft") { aLeft(); } else if (event.code === "ArrowRight") { aRight(); } else if (event.code === "Escape") { closeOverlay(); } } }); I'm wondering how I can assign the EventListener only if the overlay element is showing. At the current, my arrow keys remain "hijacked" and will not function, after the overlay is closed. If I open the overlay again, it functions as I have assigned it.