utils.js 997 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { raf } from '../utils/dom/raf';
  2. import { getScrollTop, setScrollTop } from '../utils/dom/scroll';
  3. export function scrollLeftTo(scroller, to, duration) {
  4. var count = 0;
  5. var from = scroller.scrollLeft;
  6. var frames = duration === 0 ? 1 : Math.round(duration * 1000 / 16);
  7. function animate() {
  8. scroller.scrollLeft += (to - from) / frames;
  9. if (++count < frames) {
  10. raf(animate);
  11. }
  12. }
  13. animate();
  14. }
  15. export function scrollTopTo(scroller, to, duration, callback) {
  16. var current = getScrollTop(scroller);
  17. var isDown = current < to;
  18. var frames = duration === 0 ? 1 : Math.round(duration * 1000 / 16);
  19. var step = (to - current) / frames;
  20. function animate() {
  21. current += step;
  22. if (isDown && current > to || !isDown && current < to) {
  23. current = to;
  24. }
  25. setScrollTop(scroller, current);
  26. if (isDown && current < to || !isDown && current > to) {
  27. raf(animate);
  28. } else if (callback) {
  29. raf(callback);
  30. }
  31. }
  32. animate();
  33. }