onTouchMove.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import { getDocument } from 'ssr-window';
  2. import $ from '../../shared/dom.js';
  3. import { now } from '../../shared/utils.js';
  4. export default function onTouchMove(event) {
  5. const document = getDocument();
  6. const swiper = this;
  7. const data = swiper.touchEventsData;
  8. const {
  9. params,
  10. touches,
  11. rtlTranslate: rtl,
  12. enabled
  13. } = swiper;
  14. if (!enabled) return;
  15. let e = event;
  16. if (e.originalEvent) e = e.originalEvent;
  17. if (!data.isTouched) {
  18. if (data.startMoving && data.isScrolling) {
  19. swiper.emit('touchMoveOpposite', e);
  20. }
  21. return;
  22. }
  23. if (data.isTouchEvent && e.type !== 'touchmove') return;
  24. const targetTouch = e.type === 'touchmove' && e.targetTouches && (e.targetTouches[0] || e.changedTouches[0]);
  25. const pageX = e.type === 'touchmove' ? targetTouch.pageX : e.pageX;
  26. const pageY = e.type === 'touchmove' ? targetTouch.pageY : e.pageY;
  27. if (e.preventedByNestedSwiper) {
  28. touches.startX = pageX;
  29. touches.startY = pageY;
  30. return;
  31. }
  32. if (!swiper.allowTouchMove) {
  33. if (!$(e.target).is(data.focusableElements)) {
  34. swiper.allowClick = false;
  35. }
  36. if (data.isTouched) {
  37. Object.assign(touches, {
  38. startX: pageX,
  39. startY: pageY,
  40. currentX: pageX,
  41. currentY: pageY
  42. });
  43. data.touchStartTime = now();
  44. }
  45. return;
  46. }
  47. if (data.isTouchEvent && params.touchReleaseOnEdges && !params.loop) {
  48. if (swiper.isVertical()) {
  49. // Vertical
  50. if (pageY < touches.startY && swiper.translate <= swiper.maxTranslate() || pageY > touches.startY && swiper.translate >= swiper.minTranslate()) {
  51. data.isTouched = false;
  52. data.isMoved = false;
  53. return;
  54. }
  55. } else if (pageX < touches.startX && swiper.translate <= swiper.maxTranslate() || pageX > touches.startX && swiper.translate >= swiper.minTranslate()) {
  56. return;
  57. }
  58. }
  59. if (data.isTouchEvent && document.activeElement) {
  60. if (e.target === document.activeElement && $(e.target).is(data.focusableElements)) {
  61. data.isMoved = true;
  62. swiper.allowClick = false;
  63. return;
  64. }
  65. }
  66. if (data.allowTouchCallbacks) {
  67. swiper.emit('touchMove', e);
  68. }
  69. if (e.targetTouches && e.targetTouches.length > 1) return;
  70. touches.currentX = pageX;
  71. touches.currentY = pageY;
  72. const diffX = touches.currentX - touches.startX;
  73. const diffY = touches.currentY - touches.startY;
  74. if (swiper.params.threshold && Math.sqrt(diffX ** 2 + diffY ** 2) < swiper.params.threshold) return;
  75. if (typeof data.isScrolling === 'undefined') {
  76. let touchAngle;
  77. if (swiper.isHorizontal() && touches.currentY === touches.startY || swiper.isVertical() && touches.currentX === touches.startX) {
  78. data.isScrolling = false;
  79. } else {
  80. // eslint-disable-next-line
  81. if (diffX * diffX + diffY * diffY >= 25) {
  82. touchAngle = Math.atan2(Math.abs(diffY), Math.abs(diffX)) * 180 / Math.PI;
  83. data.isScrolling = swiper.isHorizontal() ? touchAngle > params.touchAngle : 90 - touchAngle > params.touchAngle;
  84. }
  85. }
  86. }
  87. if (data.isScrolling) {
  88. swiper.emit('touchMoveOpposite', e);
  89. }
  90. if (typeof data.startMoving === 'undefined') {
  91. if (touches.currentX !== touches.startX || touches.currentY !== touches.startY) {
  92. data.startMoving = true;
  93. }
  94. }
  95. if (data.isScrolling) {
  96. data.isTouched = false;
  97. return;
  98. }
  99. if (!data.startMoving) {
  100. return;
  101. }
  102. swiper.allowClick = false;
  103. if (!params.cssMode && e.cancelable) {
  104. e.preventDefault();
  105. }
  106. if (params.touchMoveStopPropagation && !params.nested) {
  107. e.stopPropagation();
  108. }
  109. if (!data.isMoved) {
  110. if (params.loop && !params.cssMode) {
  111. swiper.loopFix();
  112. }
  113. data.startTranslate = swiper.getTranslate();
  114. swiper.setTransition(0);
  115. if (swiper.animating) {
  116. swiper.$wrapperEl.trigger('webkitTransitionEnd transitionend');
  117. }
  118. data.allowMomentumBounce = false; // Grab Cursor
  119. if (params.grabCursor && (swiper.allowSlideNext === true || swiper.allowSlidePrev === true)) {
  120. swiper.setGrabCursor(true);
  121. }
  122. swiper.emit('sliderFirstMove', e);
  123. }
  124. swiper.emit('sliderMove', e);
  125. data.isMoved = true;
  126. let diff = swiper.isHorizontal() ? diffX : diffY;
  127. touches.diff = diff;
  128. diff *= params.touchRatio;
  129. if (rtl) diff = -diff;
  130. swiper.swipeDirection = diff > 0 ? 'prev' : 'next';
  131. data.currentTranslate = diff + data.startTranslate;
  132. let disableParentSwiper = true;
  133. let resistanceRatio = params.resistanceRatio;
  134. if (params.touchReleaseOnEdges) {
  135. resistanceRatio = 0;
  136. }
  137. if (diff > 0 && data.currentTranslate > swiper.minTranslate()) {
  138. disableParentSwiper = false;
  139. if (params.resistance) data.currentTranslate = swiper.minTranslate() - 1 + (-swiper.minTranslate() + data.startTranslate + diff) ** resistanceRatio;
  140. } else if (diff < 0 && data.currentTranslate < swiper.maxTranslate()) {
  141. disableParentSwiper = false;
  142. if (params.resistance) data.currentTranslate = swiper.maxTranslate() + 1 - (swiper.maxTranslate() - data.startTranslate - diff) ** resistanceRatio;
  143. }
  144. if (disableParentSwiper) {
  145. e.preventedByNestedSwiper = true;
  146. } // Directions locks
  147. if (!swiper.allowSlideNext && swiper.swipeDirection === 'next' && data.currentTranslate < data.startTranslate) {
  148. data.currentTranslate = data.startTranslate;
  149. }
  150. if (!swiper.allowSlidePrev && swiper.swipeDirection === 'prev' && data.currentTranslate > data.startTranslate) {
  151. data.currentTranslate = data.startTranslate;
  152. }
  153. if (!swiper.allowSlidePrev && !swiper.allowSlideNext) {
  154. data.currentTranslate = data.startTranslate;
  155. } // Threshold
  156. if (params.threshold > 0) {
  157. if (Math.abs(diff) > params.threshold || data.allowThresholdMove) {
  158. if (!data.allowThresholdMove) {
  159. data.allowThresholdMove = true;
  160. touches.startX = touches.currentX;
  161. touches.startY = touches.currentY;
  162. data.currentTranslate = data.startTranslate;
  163. touches.diff = swiper.isHorizontal() ? touches.currentX - touches.startX : touches.currentY - touches.startY;
  164. return;
  165. }
  166. } else {
  167. data.currentTranslate = data.startTranslate;
  168. return;
  169. }
  170. }
  171. if (!params.followFinger || params.cssMode) return; // Update active index in free mode
  172. if (params.freeMode && params.freeMode.enabled && swiper.freeMode || params.watchSlidesProgress) {
  173. swiper.updateActiveIndex();
  174. swiper.updateSlidesClasses();
  175. }
  176. if (swiper.params.freeMode && params.freeMode.enabled && swiper.freeMode) {
  177. swiper.freeMode.onTouchMove();
  178. } // Update progress
  179. swiper.updateProgress(data.currentTranslate); // Update translate
  180. swiper.setTranslate(data.currentTranslate);
  181. }