Last active
July 17, 2019 19:15
-
-
Save ChrisLTD/4dcd90927a4a961ea481d31f46b1fb9d to your computer and use it in GitHub Desktop.
Scrolling Locking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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