mirror of
https://github.com/elyby/accounts-frontend.git
synced 2025-05-31 14:11:58 +05:30
#131: simple scrollTo animation
This commit is contained in:
28
src/components/ui/scrollTo.js
Normal file
28
src/components/ui/scrollTo.js
Normal file
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Implements scroll to animation with momentum effect
|
||||
*/
|
||||
|
||||
const requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame
|
||||
|| window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
|
||||
|
||||
const DURATION = 500;
|
||||
export default function scrollTo(y) {
|
||||
const start = Date.now();
|
||||
const {scrollTop} = document.body;
|
||||
const delta = y - scrollTop;
|
||||
|
||||
requestAnimationFrame(function animateScroll() {
|
||||
const elapsed = Date.now() - start;
|
||||
|
||||
let interpolatedY = scrollTop + delta * (elapsed / DURATION);
|
||||
|
||||
if (interpolatedY < y) {
|
||||
requestAnimationFrame(animateScroll);
|
||||
} else {
|
||||
interpolatedY = y;
|
||||
}
|
||||
|
||||
window.scrollTo(0, interpolatedY);
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user