Created
December 5, 2019 14:15
-
-
Save dbasilioesp/f057de27ef772b08cd213f9046d446c2 to your computer and use it in GitHub Desktop.
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
function onScroll(callbackDown, callbackUp) { | |
let lastScrollTop = 0; | |
let scrollDownLock = false; | |
let scrollUpLock = false; | |
let lockProcess = false; | |
let isScrolling = null; | |
let tl; | |
return function () { | |
window.clearTimeout(isScrolling); | |
isScrolling = setTimeout(() => run(), 66); | |
} | |
function run() { | |
let scrollTop = window.pageYOffset || document.documentElement.scrollTop; | |
if (lockProcess === false) { | |
if (scrollTop > (lastScrollTop + 50) && scrollDownLock === false) { | |
scrollDownLock = true; | |
scrollUpLock = false; | |
lockProcess = true; | |
tl = callbackDown(); | |
} | |
else if (scrollTop < (lastScrollTop - 50) && scrollUpLock === false) { | |
scrollUpLock = true; | |
scrollDownLock = false; | |
lockProcess = true; | |
tl = callbackUp(); | |
} | |
} | |
lastScrollTop = scrollTop <= 0 ? 0 : scrollTop; | |
if (tl && lockProcess) { | |
tl.add(() => lockProcess = false) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment