Last active
April 12, 2018 10:12
-
-
Save kekkorider/39a072ffb71583b27e8fc1b24d080c8e to your computer and use it in GitHub Desktop.
Disabling scroll the hard way
This file contains hidden or 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
| // Thanks to Hokascha fot the `{passive: false}` trick. | |
| // https://stackoverflow.com/a/46423584 | |
| let touchStart = {x: 0, y: 0} | |
| let touchOffset = {x: 0, y: 0} | |
| let beforeTouchScrollY = 0 | |
| const setTouchstartCoords = e => { | |
| touchStart = { | |
| x: e.touches[0].pageX, | |
| y: e.touches[0].pageY | |
| } | |
| } | |
| const preventDefault = e => { | |
| touchOffset = { | |
| x: e.touches[0].pageX, | |
| y: e.touches[0].pageY | |
| } | |
| if ( Math.abs(touchStart.x - touchOffset.x) < 50 || Math.abs(touchStart.x - touchOffset.y) < 50 ) { | |
| e.preventDefault() | |
| document.body.scrollTo(0, beforeTouchScrollY) | |
| } | |
| } | |
| const disableScroll = () => { | |
| document.body.addEventListener("touchmove", preventDefault, {passive: false}) | |
| document.body.addEventListener("touchstart", setTouchstartCoords) | |
| beforeTouchScrollY = window.pageYOffset || window.scrollY | |
| } | |
| const enableScroll = () => { | |
| document.body.removeEventListener("touchmove", preventDefault, {passive: false}) | |
| document.body.removeEventListener("touchstart", setTouchstartCoords) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment