#142: stop auto-scroll if user has tries to scroll manually

This commit is contained in:
SleepWalker 2016-06-28 12:30:02 +03:00
parent b74d19454e
commit 95e7c0ae87

View File

@ -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;
}
}