From 95e7c0ae87465a56df764f2f79a766a15b236ff8 Mon Sep 17 00:00:00 2001 From: SleepWalker Date: Tue, 28 Jun 2016 12:30:02 +0300 Subject: [PATCH] #142: stop auto-scroll if user has tries to scroll manually --- src/components/ui/scrollTo.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/components/ui/scrollTo.js b/src/components/ui/scrollTo.js index 9b343f7..97859b7 100644 --- a/src/components/ui/scrollTo.js +++ b/src/components/ui/scrollTo.js @@ -7,24 +7,37 @@ const requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame; -const TIME_CONSTANT = 100; +const TIME_CONSTANT = 100; // heigher numbers - slower animation export default function scrollTo(y, viewPort) { const start = Date.now(); const {scrollTop} = viewPort; const amplitude = y - scrollTop; + let scrollWasTouched = false; requestAnimationFrame(function animateScroll() { const elapsed = Date.now() - start; let delta = -amplitude * Math.exp(-elapsed / TIME_CONSTANT); - if (Math.abs(delta) > 0.5) { + if (Math.abs(delta) > 0.5 && !scrollWasTouched) { requestAnimationFrame(animateScroll); } else { delta = 0; + document.removeEventListener('mousewheel', markScrollTouched); + document.removeEventListener('touchstart', markScrollTouched); + } + + if (scrollWasTouched) { + return; } viewPort.scrollTop = y + delta; }); + + document.addEventListener('mousewheel', markScrollTouched); + document.addEventListener('touchstart', markScrollTouched); + function markScrollTouched() { + scrollWasTouched = true; + } }