25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

scroll-to.js 1.4 KiB

3 년 전
12345678910111213141516171819202122232425262728293031323334353637383940
  1. import clamp from 'lodash-es/clamp';
  2. var animationIDStorage = new WeakMap();
  3. export function scrollTo(scrollbar, x, y, duration, _a) {
  4. if (duration === void 0) { duration = 0; }
  5. var _b = _a === void 0 ? {} : _a, _c = _b.easing, easing = _c === void 0 ? defaultEasing : _c, callback = _b.callback;
  6. var options = scrollbar.options, offset = scrollbar.offset, limit = scrollbar.limit;
  7. if (options.renderByPixels) {
  8. // ensure resolved with integer
  9. x = Math.round(x);
  10. y = Math.round(y);
  11. }
  12. var startX = offset.x;
  13. var startY = offset.y;
  14. var disX = clamp(x, 0, limit.x) - startX;
  15. var disY = clamp(y, 0, limit.y) - startY;
  16. var start = Date.now();
  17. function scroll() {
  18. var elapse = Date.now() - start;
  19. var progress = duration ? easing(Math.min(elapse / duration, 1)) : 1;
  20. scrollbar.setPosition(startX + disX * progress, startY + disY * progress);
  21. if (elapse >= duration) {
  22. if (typeof callback === 'function') {
  23. callback.call(scrollbar);
  24. }
  25. }
  26. else {
  27. var animationID = requestAnimationFrame(scroll);
  28. animationIDStorage.set(scrollbar, animationID);
  29. }
  30. }
  31. cancelAnimationFrame(animationIDStorage.get(scrollbar));
  32. scroll();
  33. }
  34. /**
  35. * easeOutCubic
  36. */
  37. function defaultEasing(t) {
  38. return Math.pow((t - 1), 3) + 1;
  39. }
  40. //# sourceMappingURL=scroll-to.js.map