I have a simple click function, that when a button is clicked the user can't scroll. This all works OK. The button works as toggle though, and on the 'else' part of the toggle, i.e. when the user clicks again, I want to remove the event listener that stop the user scrolling with the removeEventListener();
method. The function I am calling within this method isn't working though?
I can't seem to work out what the problem is?
codepen: https://codepen.io/emilychews/pen/MrBvzP
JS
var button = document.getElementById("button");
// function to be called and removed when button is clicked
function noscroll() {
window.scrollTo(0, 0);
}
// TOGGLE EVENT LISTENER
var scrollToggle = false;
function toggleEventListener() {
if (scrollToggle === false) {
window.addEventListener("scroll", function(){
noscroll();
}, false);
scrollToggle = true;
} else {
window.removeEventListener("scroll", function(){
noscroll();
}, false);
scrollToggle = false;
}
}
button.addEventListener("click", function() {
toggleEventListener();
}, false)
body {margin: 0; padding: 0; height: 150vh;}
#button {font-family: arial;
margin: 10px 0 0 30px;
width: 100px;
height: 50px;
background: red;
display: flex;
justify-content: center;
align-items: center;
color: white;
cursor: pointer;
}
<div id="button">Click Me</div>