Skip to content

Instantly share code, notes, and snippets.

@ChrisLTD
Last active July 17, 2019 19:15
Show Gist options
  • Save ChrisLTD/4dcd90927a4a961ea481d31f46b1fb9d to your computer and use it in GitHub Desktop.
Save ChrisLTD/4dcd90927a4a961ea481d31f46b1fb9d to your computer and use it in GitHub Desktop.
Scrolling Locking
<script>
// Scroll locking
var overlayShown = false;
var scrollPosition = 0;
function toggleScrollLock() {
if (!overlayShown) {
showOverlay();
} else {
removeOverlay();
}
overlayShown = !overlayShown;
}
function showOverlay() {
scrollPosition = window.pageYOffset;
$('body').addClass('lock-scrolling');
$('#wrapper').css('marginTop', -(scrollPosition));
}
function removeOverlay() {
$('body').removeClass('lock-scrolling');
window.scrollTo(0, scrollPosition);
$('#wrapper').css('marginTop', 0);
}
$('.js-toggle-lock-scrolling').on('click', function() {
toggleScrollLock();
});
</script>
<style>
body.lock-scrolling {
position: fixed;
overflow: hidden;
height: 100vh;
width: 100vw;
}
</style>
<body>
<div id="wrapper">
</div>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment