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

3 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import clamp from 'lodash-es/clamp';
  2. import { isOneOf, getPosition, eventScope, setStyle, } from '../utils/';
  3. var Direction;
  4. (function (Direction) {
  5. Direction[Direction["X"] = 0] = "X";
  6. Direction[Direction["Y"] = 1] = "Y";
  7. })(Direction || (Direction = {}));
  8. export function mouseHandler(scrollbar) {
  9. var addEvent = eventScope(scrollbar);
  10. var container = scrollbar.containerEl;
  11. var _a = scrollbar.track, xAxis = _a.xAxis, yAxis = _a.yAxis;
  12. function calcOffset(direction, clickPosition) {
  13. var size = scrollbar.size;
  14. if (direction === Direction.X) {
  15. var totalWidth = size.container.width + (xAxis.thumb.realSize - xAxis.thumb.displaySize);
  16. return clickPosition / totalWidth * size.content.width;
  17. }
  18. if (direction === Direction.Y) {
  19. var totalHeight = size.container.height + (yAxis.thumb.realSize - yAxis.thumb.displaySize);
  20. return clickPosition / totalHeight * size.content.height;
  21. }
  22. return 0;
  23. }
  24. function getTrackDirection(elem) {
  25. if (isOneOf(elem, [xAxis.element, xAxis.thumb.element])) {
  26. return Direction.X;
  27. }
  28. if (isOneOf(elem, [yAxis.element, yAxis.thumb.element])) {
  29. return Direction.Y;
  30. }
  31. return void 0;
  32. }
  33. var isMouseDown;
  34. var isMouseMoving;
  35. var startOffsetToThumb;
  36. var startTrackDirection;
  37. var containerRect;
  38. addEvent(container, 'click', function (evt) {
  39. if (isMouseMoving || !isOneOf(evt.target, [xAxis.element, yAxis.element])) {
  40. return;
  41. }
  42. var track = evt.target;
  43. var direction = getTrackDirection(track);
  44. var rect = track.getBoundingClientRect();
  45. var clickPos = getPosition(evt);
  46. var offset = scrollbar.offset, limit = scrollbar.limit;
  47. if (direction === Direction.X) {
  48. var offsetOnTrack = clickPos.x - rect.left - xAxis.thumb.displaySize / 2;
  49. scrollbar.setMomentum(clamp(calcOffset(direction, offsetOnTrack) - offset.x, -offset.x, limit.x - offset.x), 0);
  50. }
  51. if (direction === Direction.Y) {
  52. var offsetOnTrack = clickPos.y - rect.top - yAxis.thumb.displaySize / 2;
  53. scrollbar.setMomentum(0, clamp(calcOffset(direction, offsetOnTrack) - offset.y, -offset.y, limit.y - offset.y));
  54. }
  55. });
  56. addEvent(container, 'mousedown', function (evt) {
  57. if (!isOneOf(evt.target, [xAxis.thumb.element, yAxis.thumb.element])) {
  58. return;
  59. }
  60. isMouseDown = true;
  61. var thumb = evt.target;
  62. var cursorPos = getPosition(evt);
  63. var thumbRect = thumb.getBoundingClientRect();
  64. startTrackDirection = getTrackDirection(thumb);
  65. // pointer offset to thumb
  66. startOffsetToThumb = {
  67. x: cursorPos.x - thumbRect.left,
  68. y: cursorPos.y - thumbRect.top,
  69. };
  70. // container bounding rectangle
  71. containerRect = container.getBoundingClientRect();
  72. // prevent selection, see:
  73. // https://github.com/idiotWu/smooth-scrollbar/issues/48
  74. setStyle(scrollbar.containerEl, {
  75. '-user-select': 'none',
  76. });
  77. });
  78. addEvent(window, 'mousemove', function (evt) {
  79. if (!isMouseDown)
  80. return;
  81. isMouseMoving = true;
  82. var offset = scrollbar.offset;
  83. var cursorPos = getPosition(evt);
  84. if (startTrackDirection === Direction.X) {
  85. // get percentage of pointer position in track
  86. // then tranform to px
  87. // don't need easing
  88. var offsetOnTrack = cursorPos.x - startOffsetToThumb.x - containerRect.left;
  89. scrollbar.setPosition(calcOffset(startTrackDirection, offsetOnTrack), offset.y);
  90. }
  91. if (startTrackDirection === Direction.Y) {
  92. var offsetOnTrack = cursorPos.y - startOffsetToThumb.y - containerRect.top;
  93. scrollbar.setPosition(offset.x, calcOffset(startTrackDirection, offsetOnTrack));
  94. }
  95. });
  96. addEvent(window, 'mouseup blur', function () {
  97. isMouseDown = isMouseMoving = false;
  98. setStyle(scrollbar.containerEl, {
  99. '-user-select': '',
  100. });
  101. });
  102. }
  103. //# sourceMappingURL=mouse.js.map