3

I've been trying to enable/disable scrolling with javascript,

I managed to disable the scrolling however fail to put it back to working state,

so what I've tried is:

 function noscroll() {
    window.scrollTo(0, 0);
}

and I try to add and remove scrolling with the following lines of code

 window.removeEventListener("scroll", noscroll);

 window.addEventListener("scroll", noscroll);

any help is much appreciated

4
  • 1
    Can't you just hide the scrollbar instead? Commented Nov 26, 2018 at 21:18
  • this is usually solved with CSS, overflow:none - a js based approach like this might cause jittery scrolling effects Commented Nov 26, 2018 at 21:29
  • thx I'll try both approaches! and let you know Commented Nov 26, 2018 at 21:38
  • You still haven't explained what you are asking. Commented Nov 26, 2018 at 23:56

2 Answers 2

2

Your approach appears to work as shown here:

function noscroll() {
  window.scrollTo(0, 0);
}

document.getElementById('enable').addEventListener('click', () => {
  window.removeEventListener("scroll", noscroll);
});
 
document.getElementById('disable').addEventListener('click', () => {
  window.addEventListener("scroll", noscroll);  
});
div {
  background:pink;
  height:1000px;
  padding:50px;
}

header {
  position:fixed;
  top:0;
  left:0;
  z-index:1;
}
<header>
<button id="enable">Enable scrolling</button>
<button id="disable">Disable scrolling</button>
</header>
<div>
Prevent scroll via Javascript (window.scrollTo)
</div>

Another approach worth considering, is to update the overflow property of your document.body element by "hiding" any content overflow. This has the side effect of removing the scrollbar, which in turn, prevents scrolling:

 document.getElementById('enable').addEventListener('click', () => {

    // Remove overflow value from document body to restore default scrolling
    document.body.style.overflow = '';
 })

 document.getElementById('disable').addEventListener('click', () => {
    // When scrolling is disabled, auto scroll to top of screen
    window.scrollTo(0, 0);

    // Apply overflow:hidden to document body to prevent scrolling
    document.body.style.overflow = 'hidden';
 })
div {
  background:pink;
  height:1000px;
  padding:50px;
}

header {
  position:fixed;
  top:0;
  left:0;
  z-index:1;
}
<header>
<button id="enable">Enable scrolling</button>
<button id="disable">Disable scrolling</button>
</header>
<div>
Prevent scroll via CSS
</div>

The main advantage of the CSS based approach shown above is that it's not subject to "jittery" scrolling behaviour that can be noticable with the former "pure-javascript" approach.

Sign up to request clarification or add additional context in comments.

Comments

1

Your approach works, so what is your question?

function noScroll() {
  window.scrollTo(0, 0)
}

forbid.addEventListener('click', () => {
  window.addEventListener('scroll', noScroll)
})

allow.addEventListener('click', () => {
  window.removeEventListener('scroll', noScroll)
})
div { 
  background-color: #f0f0f0;
  height: 150vh;
  line-height: 150vh;
  text-align: center;
}
<button type="button" id="forbid">Forbid scrolling</button>
<button type="button" id="allow">Allow scrolling</button>
<div>scroll me</div>

1 Comment

well the way I wrote add/remove event listeners does not work, however I tried your code and it works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.