3 Ways To Disable Scrolling In Javascript

Welcome to a tutorial on how to disable scrolling in Javascript. Need to disable scrolling on the page? Let the user fill something up before moving on? Sadly, there are no native implementations to disable scrolling in Javascript; We can only “simulate” a scroll lock using various alternatives.

The common methods to disable scrolling in Javascript are:

  1. Force scroll the user back to a specific spot or element – window.onscroll = () => window.scroll(0, 0);
  2. Hide the overflow (no scrollbars) – document.body.style.overflow = "hidden";
  3. Show the content in a fixed full-screen container – <div style="width: 100vw; height: 100vh; position: fixed; top: 0; left: 0; z-index: 999"></div>

That covers the quick basics, but read on for detailed examples!

 

 

TLDR – QUICK SLIDES

[web_stories_embed url=”https://code-boxx.com/web-stories/disable-scrolling-javascript/” title=”How To Disable Scrolling In Javascript” poster=”https://code-boxx.com/wp-content/uploads/2021/11/STORY-JS-20230518.webp” width=”360″ height=”600″ align=”center”]

Fullscreen Mode – Click Here

 

TABLE OF CONTENTS

 

 

DISABLE SCROLL

All right, let us now get into the various examples of disabling scroll in Javascript.

 

METHOD 1) FORCE SCROLL

1-force-scroll.html
<!-- (A) LOCK TO CURRENT POSITION -->
<script>
function lockA () {
  var xPos = window.scrollX;
  var yPos = window.scrollY;
  window.onscroll = () => window.scroll(xPos, yPos);
}
</script>
<input type="button" value="Lock Current" onclick="lockA()">
 
<!-- (B) LOCK TO SPECIFIC ELEMENT -->
<script>
function lockB () {
  var target = document.getElementById("demoB");
  target.scrollIntoView();
  window.onscroll = () => target.scrollIntoView();
}
</script>
<input id="demoB" type="button" value="Lock To This Button" onclick="lockB()">
 
<!-- (C) UNLOCK -->
<script>
function unlock () { window.onscroll = ""; }
</script>
<input type="button" value="Unlock" onclick="unlock()">

What’s the next best thing to do when there are no native “disable scroll” functions?

  • We add a scroll listener – window.onscroll.
  • Force scroll the user back to a certain spot – window.scroll(X, Y).
  • Or force scroll to a specific element – ELEMENT.scrollIntoView().

 

 

METHOD 2) HIDE OVERFLOW

2-no-overflow.html
<script>
// (A) DISABLE SCROLL
function noflow () {
  document.body.style.overflow = "hidden";
  document.body.style.userSelect = "none";
}

// (B) ENABLE SCROLL
function haveflow () {
  document.body.style.overflow = "auto";
  document.body.style.userSelect = "auto";
}
</script>
<input type="button" value="Lock" onclick="noflow()">
<input type="button" value="Unlock" onclick="haveflow()">

This is the other “common trick” that involves the use of CSS instead.

  • By setting overflow = "none", we will hide the scrollbars and effectively disable scrolling.
  • But users can still scroll by highlighting the text, so we add another userSelect = "none" to prevent that.

 

 

METHOD 3) FULLSCREEN DIALOG BOX

3-dialog.html
<style>
/* (A) DIALOG BOX CSS */
#dialog {
  position: fixed;
  top: 0; left: 0;
  z-index: 999;
  width: 100vw; height: 100vh;
  background: #fff;
  display: none;
}
body.dopen { overflow: hidden; }
body.dopen #dialog { display: block; }
</style>
 
<script>
// (B) SHOW & HIDE DIALOG BOX
function dshow () {
  document.body.classList.add("dopen");
}
function dhide () {
  document.body.classList.remove("dopen");
}
</script>
 
<!-- (C) HTML DIALOG BOX -->
<input type="button" value="Lock" onclick="dshow()">
<div id="dialog">
  <input type="button" value="Unlock" onclick="dhide()">
</div>

Lastly for you guys who are looking for a less “hacky” method, just put the contents into a fullscreen wrapper or dialog box.

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

EXAMPLE CODE DOWNLOAD

Click here for the source code on GitHub gist, just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

EXTRA BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

DON’T LOCK UNNECESSARILY!

Lastly, don’t lock the scroll for the sake of locking it… That is kind of a bad interface design and will probably confuse the users even more. If you just need the user to fill in a form field, just highlight it. If you want a step-by-step wizard, create multiple <div> containers instead.

 

COMPATIBILITY CHECKS

All 3 methods will work on any modern browser.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

How To Disable Scrolling  In Javascript (click to enlarge)

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

2 thoughts on “3 Ways To Disable Scrolling In Javascript”

Leave a Comment

Your email address will not be published. Required fields are marked *