Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Enable/Disable mouse scroll with Javascript

Tags:

javascript

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

like image 582
Asım Gündüz Avatar asked Jan 26 '26 10:01

Asım Gündüz


2 Answers

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.

like image 70
Dacre Denny Avatar answered Jan 28 '26 22:01

Dacre Denny


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>
like image 37
connexo Avatar answered Jan 29 '26 00:01

connexo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!