onTouchMove.js 6.2 KB

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