You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
645 B

  1. const easingOutQuint = (x, t, b, c, d) => c * ((t = t / d - 1) * t * t * t * t + 1) + b;
  2. const scrollTop = (node) => {
  3. const startTime = Date.now();
  4. const offset = node.scrollTop;
  5. const targetY = -offset;
  6. const duration = 1000;
  7. let interrupt = false;
  8. const step = () => {
  9. const elapsed = Date.now() - startTime;
  10. const percentage = elapsed / duration;
  11. if (percentage > 1 || interrupt) {
  12. return;
  13. }
  14. node.scrollTop = easingOutQuint(0, elapsed, offset, targetY, duration);
  15. requestAnimationFrame(step);
  16. };
  17. step();
  18. return () => {
  19. interrupt = true;
  20. };
  21. };
  22. export default scrollTop;