utils.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.scrollLeftTo = scrollLeftTo;
  4. exports.scrollTopTo = scrollTopTo;
  5. var _raf = require("../utils/dom/raf");
  6. var _scroll = require("../utils/dom/scroll");
  7. var scrollLeftRafId;
  8. function scrollLeftTo(scroller, to, duration) {
  9. (0, _raf.cancelRaf)(scrollLeftRafId);
  10. var count = 0;
  11. var from = scroller.scrollLeft;
  12. var frames = duration === 0 ? 1 : Math.round(duration * 1000 / 16);
  13. function animate() {
  14. scroller.scrollLeft += (to - from) / frames;
  15. if (++count < frames) {
  16. scrollLeftRafId = (0, _raf.raf)(animate);
  17. }
  18. }
  19. animate();
  20. }
  21. function scrollTopTo(scroller, to, duration, callback) {
  22. var current = (0, _scroll.getScrollTop)(scroller);
  23. var isDown = current < to;
  24. var frames = duration === 0 ? 1 : Math.round(duration * 1000 / 16);
  25. var step = (to - current) / frames;
  26. function animate() {
  27. current += step;
  28. if (isDown && current > to || !isDown && current < to) {
  29. current = to;
  30. }
  31. (0, _scroll.setScrollTop)(scroller, current);
  32. if (isDown && current < to || !isDown && current > to) {
  33. (0, _raf.raf)(animate);
  34. } else if (callback) {
  35. (0, _raf.raf)(callback);
  36. }
  37. }
  38. animate();
  39. }