最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to EnableDisable mouse scroll with Javascript - Stack Overflow

programmeradmin5浏览0评论

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

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

Share Improve this question asked Nov 26, 2018 at 21:13 Asım GündüzAsım Gündüz 1,2973 gold badges19 silver badges50 bronze badges 4
  • 1 Can't you just hide the scrollbar instead? – Icepickle 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 – Dacre Denny Commented Nov 26, 2018 at 21:29
  • thx I'll try both approaches! and let you know – Asım Gündüz Commented Nov 26, 2018 at 21:38
  • You still haven't explained what you are asking. – connexo Commented Nov 26, 2018 at 23:56
Add a ment  | 

2 Answers 2

Reset to default 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.

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>

发布评论

评论列表(0)

  1. 暂无评论