utils.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. function scrollLeftTo(scroller, to, duration) {
  8. var count = 0;
  9. var from = scroller.scrollLeft;
  10. var frames = duration === 0 ? 1 : Math.round(duration * 1000 / 16);
  11. function animate() {
  12. scroller.scrollLeft += (to - from) / frames;
  13. if (++count < frames) {
  14. (0, _raf.raf)(animate);
  15. }
  16. }
  17. animate();
  18. }
  19. function scrollTopTo(scroller, to, duration, callback) {
  20. var current = (0, _scroll.getScrollTop)(scroller);
  21. var isDown = current < to;
  22. var frames = duration === 0 ? 1 : Math.round(duration * 1000 / 16);
  23. var step = (to - current) / frames;
  24. function animate() {
  25. current += step;
  26. if (isDown && current > to || !isDown && current < to) {
  27. current = to;
  28. }
  29. (0, _scroll.setScrollTop)(scroller, current);
  30. if (isDown && current < to || !isDown && current > to) {
  31. (0, _raf.raf)(animate);
  32. } else if (callback) {
  33. (0, _raf.raf)(callback);
  34. }
  35. }
  36. animate();
  37. }