mirror of
https://github.com/elyby/accounts-frontend.git
synced 2024-12-30 00:40:41 +05:30
#142: stop auto-scroll if user has tries to scroll manually
This commit is contained in:
parent
b74d19454e
commit
95e7c0ae87
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user