mirror of
https://github.com/elyby/accounts-frontend.git
synced 2025-01-01 17:52:10 +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
|
const requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame
|
||||||
|| window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
|
|| window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
|
||||||
|
|
||||||
const TIME_CONSTANT = 100;
|
const TIME_CONSTANT = 100; // heigher numbers - slower animation
|
||||||
export default function scrollTo(y, viewPort) {
|
export default function scrollTo(y, viewPort) {
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
const {scrollTop} = viewPort;
|
const {scrollTop} = viewPort;
|
||||||
const amplitude = y - scrollTop;
|
const amplitude = y - scrollTop;
|
||||||
|
let scrollWasTouched = false;
|
||||||
|
|
||||||
requestAnimationFrame(function animateScroll() {
|
requestAnimationFrame(function animateScroll() {
|
||||||
const elapsed = Date.now() - start;
|
const elapsed = Date.now() - start;
|
||||||
|
|
||||||
let delta = -amplitude * Math.exp(-elapsed / TIME_CONSTANT);
|
let delta = -amplitude * Math.exp(-elapsed / TIME_CONSTANT);
|
||||||
|
|
||||||
if (Math.abs(delta) > 0.5) {
|
if (Math.abs(delta) > 0.5 && !scrollWasTouched) {
|
||||||
requestAnimationFrame(animateScroll);
|
requestAnimationFrame(animateScroll);
|
||||||
} else {
|
} else {
|
||||||
delta = 0;
|
delta = 0;
|
||||||
|
document.removeEventListener('mousewheel', markScrollTouched);
|
||||||
|
document.removeEventListener('touchstart', markScrollTouched);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (scrollWasTouched) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
viewPort.scrollTop = y + delta;
|
viewPort.scrollTop = y + delta;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
document.addEventListener('mousewheel', markScrollTouched);
|
||||||
|
document.addEventListener('touchstart', markScrollTouched);
|
||||||
|
function markScrollTouched() {
|
||||||
|
scrollWasTouched = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user