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 = function () { 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 the detailed examples!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Disable Scroll Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

DISABLE SCROLL

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

 

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 = function () {
    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 = function () {
    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().

 

 

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 highlight the text, so we add another userSelect = "none" to prevent that.

 

 

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.

 

USEFUL 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.

 

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!

Leave a Comment

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