onTouchMove.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import { getDocument } from 'ssr-window';
  2. import { now } from '../../shared/utils.js';
  3. export default function onTouchMove(event) {
  4. const document = getDocument();
  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. if (!params.simulateTouch && event.pointerType === 'mouse') 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. const pointerIndex = data.evCache.findIndex(cachedEv => cachedEv.pointerId === e.pointerId);
  24. if (pointerIndex >= 0) data.evCache[pointerIndex] = e;
  25. const targetTouch = data.evCache.length > 1 ? data.evCache[0] : e;
  26. const pageX = targetTouch.pageX;
  27. const pageY = targetTouch.pageY;
  28. if (e.preventedByNestedSwiper) {
  29. touches.startX = pageX;
  30. touches.startY = pageY;
  31. return;
  32. }
  33. if (!swiper.allowTouchMove) {
  34. if (!e.target.matches(data.focusableElements)) {
  35. swiper.allowClick = false;
  36. }
  37. if (data.isTouched) {
  38. Object.assign(touches, {
  39. startX: pageX,
  40. startY: pageY,
  41. prevX: swiper.touches.currentX,
  42. prevY: swiper.touches.currentY,
  43. currentX: pageX,
  44. currentY: pageY
  45. });
  46. data.touchStartTime = now();
  47. }
  48. return;
  49. }
  50. if (params.touchReleaseOnEdges && !params.loop) {
  51. if (swiper.isVertical()) {
  52. // Vertical
  53. if (pageY < touches.startY && swiper.translate <= swiper.maxTranslate() || pageY > touches.startY && swiper.translate >= swiper.minTranslate()) {
  54. data.isTouched = false;
  55. data.isMoved = false;
  56. return;
  57. }
  58. } else if (pageX < touches.startX && swiper.translate <= swiper.maxTranslate() || pageX > touches.startX && swiper.translate >= swiper.minTranslate()) {
  59. return;
  60. }
  61. }
  62. if (document.activeElement) {
  63. if (e.target === document.activeElement && e.target.matches(data.focusableElements)) {
  64. data.isMoved = true;
  65. swiper.allowClick = false;
  66. return;
  67. }
  68. }
  69. if (data.allowTouchCallbacks) {
  70. swiper.emit('touchMove', e);
  71. }
  72. if (e.targetTouches && e.targetTouches.length > 1) return;
  73. touches.currentX = pageX;
  74. touches.currentY = pageY;
  75. const diffX = touches.currentX - touches.startX;
  76. const diffY = touches.currentY - touches.startY;
  77. if (swiper.params.threshold && Math.sqrt(diffX ** 2 + diffY ** 2) < swiper.params.threshold) return;
  78. if (typeof data.isScrolling === 'undefined') {
  79. let touchAngle;
  80. if (swiper.isHorizontal() && touches.currentY === touches.startY || swiper.isVertical() && touches.currentX === touches.startX) {
  81. data.isScrolling = false;
  82. } else {
  83. // eslint-disable-next-line
  84. if (diffX * diffX + diffY * diffY >= 25) {
  85. touchAngle = Math.atan2(Math.abs(diffY), Math.abs(diffX)) * 180 / Math.PI;
  86. data.isScrolling = swiper.isHorizontal() ? touchAngle > params.touchAngle : 90 - touchAngle > params.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 || swiper.zoom && swiper.params.zoom && swiper.params.zoom.enabled && data.evCache.length > 1) {
  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. let diff = swiper.isHorizontal() ? diffX : diffY;
  113. let touchesDiff = swiper.isHorizontal() ? touches.currentX - touches.previousX : touches.currentY - touches.previousY;
  114. if (params.oneWayMovement) {
  115. diff = Math.abs(diff) * (rtl ? 1 : -1);
  116. touchesDiff = Math.abs(touchesDiff) * (rtl ? 1 : -1);
  117. }
  118. touches.diff = diff;
  119. diff *= params.touchRatio;
  120. if (rtl) {
  121. diff = -diff;
  122. touchesDiff = -touchesDiff;
  123. }
  124. const prevTouchesDirection = swiper.touchesDirection;
  125. swiper.swipeDirection = diff > 0 ? 'prev' : 'next';
  126. swiper.touchesDirection = touchesDiff > 0 ? 'prev' : 'next';
  127. const isLoop = swiper.params.loop && !params.cssMode;
  128. if (!data.isMoved) {
  129. if (isLoop) {
  130. swiper.loopFix({
  131. direction: swiper.swipeDirection
  132. });
  133. }
  134. data.startTranslate = swiper.getTranslate();
  135. swiper.setTransition(0);
  136. if (swiper.animating) {
  137. const evt = new window.CustomEvent('transitionend', {
  138. bubbles: true,
  139. cancelable: true
  140. });
  141. swiper.wrapperEl.dispatchEvent(evt);
  142. }
  143. data.allowMomentumBounce = false;
  144. // Grab Cursor
  145. if (params.grabCursor && (swiper.allowSlideNext === true || swiper.allowSlidePrev === true)) {
  146. swiper.setGrabCursor(true);
  147. }
  148. swiper.emit('sliderFirstMove', e);
  149. }
  150. let loopFixed;
  151. if (data.isMoved && prevTouchesDirection !== swiper.touchesDirection && isLoop && Math.abs(diff) >= 1) {
  152. // need another loop fix
  153. swiper.loopFix({
  154. direction: swiper.swipeDirection,
  155. setTranslate: true
  156. });
  157. loopFixed = true;
  158. }
  159. swiper.emit('sliderMove', e);
  160. data.isMoved = true;
  161. data.currentTranslate = diff + data.startTranslate;
  162. let disableParentSwiper = true;
  163. let resistanceRatio = params.resistanceRatio;
  164. if (params.touchReleaseOnEdges) {
  165. resistanceRatio = 0;
  166. }
  167. if (diff > 0) {
  168. if (isLoop && !loopFixed && data.currentTranslate > (params.centeredSlides ? swiper.minTranslate() - swiper.size / 2 : swiper.minTranslate())) {
  169. swiper.loopFix({
  170. direction: 'prev',
  171. setTranslate: true,
  172. activeSlideIndex: 0
  173. });
  174. }
  175. if (data.currentTranslate > swiper.minTranslate()) {
  176. disableParentSwiper = false;
  177. if (params.resistance) {
  178. data.currentTranslate = swiper.minTranslate() - 1 + (-swiper.minTranslate() + data.startTranslate + diff) ** resistanceRatio;
  179. }
  180. }
  181. } else if (diff < 0) {
  182. if (isLoop && !loopFixed && data.currentTranslate < (params.centeredSlides ? swiper.maxTranslate() + swiper.size / 2 : swiper.maxTranslate())) {
  183. swiper.loopFix({
  184. direction: 'next',
  185. setTranslate: true,
  186. activeSlideIndex: swiper.slides.length - (params.slidesPerView === 'auto' ? swiper.slidesPerViewDynamic() : Math.ceil(parseFloat(params.slidesPerView, 10)))
  187. });
  188. }
  189. if (data.currentTranslate < swiper.maxTranslate()) {
  190. disableParentSwiper = false;
  191. if (params.resistance) {
  192. data.currentTranslate = swiper.maxTranslate() + 1 - (swiper.maxTranslate() - data.startTranslate - diff) ** resistanceRatio;
  193. }
  194. }
  195. }
  196. if (disableParentSwiper) {
  197. e.preventedByNestedSwiper = true;
  198. }
  199. // Directions locks
  200. if (!swiper.allowSlideNext && swiper.swipeDirection === 'next' && data.currentTranslate < data.startTranslate) {
  201. data.currentTranslate = data.startTranslate;
  202. }
  203. if (!swiper.allowSlidePrev && swiper.swipeDirection === 'prev' && data.currentTranslate > data.startTranslate) {
  204. data.currentTranslate = data.startTranslate;
  205. }
  206. if (!swiper.allowSlidePrev && !swiper.allowSlideNext) {
  207. data.currentTranslate = data.startTranslate;
  208. }
  209. // Threshold
  210. if (params.threshold > 0) {
  211. if (Math.abs(diff) > params.threshold || data.allowThresholdMove) {
  212. if (!data.allowThresholdMove) {
  213. data.allowThresholdMove = true;
  214. touches.startX = touches.currentX;
  215. touches.startY = touches.currentY;
  216. data.currentTranslate = data.startTranslate;
  217. touches.diff = swiper.isHorizontal() ? touches.currentX - touches.startX : touches.currentY - touches.startY;
  218. return;
  219. }
  220. } else {
  221. data.currentTranslate = data.startTranslate;
  222. return;
  223. }
  224. }
  225. if (!params.followFinger || params.cssMode) return;
  226. // Update active index in free mode
  227. if (params.freeMode && params.freeMode.enabled && swiper.freeMode || params.watchSlidesProgress) {
  228. swiper.updateActiveIndex();
  229. swiper.updateSlidesClasses();
  230. }
  231. if (params.freeMode && params.freeMode.enabled && swiper.freeMode) {
  232. swiper.freeMode.onTouchMove();
  233. }
  234. // Update progress
  235. swiper.updateProgress(data.currentTranslate);
  236. // Update translate
  237. swiper.setTranslate(data.currentTranslate);
  238. }