onTouchEnd.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { now, nextTick } from '../../shared/utils.js';
  2. export default function onTouchEnd(event) {
  3. const swiper = this;
  4. const data = swiper.touchEventsData;
  5. const pointerIndex = data.evCache.findIndex(cachedEv => cachedEv.pointerId === event.pointerId);
  6. if (pointerIndex >= 0) {
  7. data.evCache.splice(pointerIndex, 1);
  8. }
  9. if (['pointercancel', 'pointerout', 'pointerleave'].includes(event.type)) {
  10. const proceed = event.type === 'pointercancel' && (swiper.browser.isSafari || swiper.browser.isWebView);
  11. if (!proceed) {
  12. return;
  13. }
  14. }
  15. const {
  16. params,
  17. touches,
  18. rtlTranslate: rtl,
  19. slidesGrid,
  20. enabled
  21. } = swiper;
  22. if (!enabled) return;
  23. if (!params.simulateTouch && event.pointerType === 'mouse') return;
  24. let e = event;
  25. if (e.originalEvent) e = e.originalEvent;
  26. if (data.allowTouchCallbacks) {
  27. swiper.emit('touchEnd', e);
  28. }
  29. data.allowTouchCallbacks = false;
  30. if (!data.isTouched) {
  31. if (data.isMoved && params.grabCursor) {
  32. swiper.setGrabCursor(false);
  33. }
  34. data.isMoved = false;
  35. data.startMoving = false;
  36. return;
  37. }
  38. // Return Grab Cursor
  39. if (params.grabCursor && data.isMoved && data.isTouched && (swiper.allowSlideNext === true || swiper.allowSlidePrev === true)) {
  40. swiper.setGrabCursor(false);
  41. }
  42. // Time diff
  43. const touchEndTime = now();
  44. const timeDiff = touchEndTime - data.touchStartTime;
  45. // Tap, doubleTap, Click
  46. if (swiper.allowClick) {
  47. const pathTree = e.path || e.composedPath && e.composedPath();
  48. swiper.updateClickedSlide(pathTree && pathTree[0] || e.target);
  49. swiper.emit('tap click', e);
  50. if (timeDiff < 300 && touchEndTime - data.lastClickTime < 300) {
  51. swiper.emit('doubleTap doubleClick', e);
  52. }
  53. }
  54. data.lastClickTime = now();
  55. nextTick(() => {
  56. if (!swiper.destroyed) swiper.allowClick = true;
  57. });
  58. if (!data.isTouched || !data.isMoved || !swiper.swipeDirection || touches.diff === 0 || data.currentTranslate === data.startTranslate) {
  59. data.isTouched = false;
  60. data.isMoved = false;
  61. data.startMoving = false;
  62. return;
  63. }
  64. data.isTouched = false;
  65. data.isMoved = false;
  66. data.startMoving = false;
  67. let currentPos;
  68. if (params.followFinger) {
  69. currentPos = rtl ? swiper.translate : -swiper.translate;
  70. } else {
  71. currentPos = -data.currentTranslate;
  72. }
  73. if (params.cssMode) {
  74. return;
  75. }
  76. if (params.freeMode && params.freeMode.enabled) {
  77. swiper.freeMode.onTouchEnd({
  78. currentPos
  79. });
  80. return;
  81. }
  82. // Find current slide
  83. let stopIndex = 0;
  84. let groupSize = swiper.slidesSizesGrid[0];
  85. for (let i = 0; i < slidesGrid.length; i += i < params.slidesPerGroupSkip ? 1 : params.slidesPerGroup) {
  86. const increment = i < params.slidesPerGroupSkip - 1 ? 1 : params.slidesPerGroup;
  87. if (typeof slidesGrid[i + increment] !== 'undefined') {
  88. if (currentPos >= slidesGrid[i] && currentPos < slidesGrid[i + increment]) {
  89. stopIndex = i;
  90. groupSize = slidesGrid[i + increment] - slidesGrid[i];
  91. }
  92. } else if (currentPos >= slidesGrid[i]) {
  93. stopIndex = i;
  94. groupSize = slidesGrid[slidesGrid.length - 1] - slidesGrid[slidesGrid.length - 2];
  95. }
  96. }
  97. let rewindFirstIndex = null;
  98. let rewindLastIndex = null;
  99. if (params.rewind) {
  100. if (swiper.isBeginning) {
  101. rewindLastIndex = params.virtual && params.virtual.enabled && swiper.virtual ? swiper.virtual.slides.length - 1 : swiper.slides.length - 1;
  102. } else if (swiper.isEnd) {
  103. rewindFirstIndex = 0;
  104. }
  105. }
  106. // Find current slide size
  107. const ratio = (currentPos - slidesGrid[stopIndex]) / groupSize;
  108. const increment = stopIndex < params.slidesPerGroupSkip - 1 ? 1 : params.slidesPerGroup;
  109. if (timeDiff > params.longSwipesMs) {
  110. // Long touches
  111. if (!params.longSwipes) {
  112. swiper.slideTo(swiper.activeIndex);
  113. return;
  114. }
  115. if (swiper.swipeDirection === 'next') {
  116. if (ratio >= params.longSwipesRatio) swiper.slideTo(params.rewind && swiper.isEnd ? rewindFirstIndex : stopIndex + increment);else swiper.slideTo(stopIndex);
  117. }
  118. if (swiper.swipeDirection === 'prev') {
  119. if (ratio > 1 - params.longSwipesRatio) {
  120. swiper.slideTo(stopIndex + increment);
  121. } else if (rewindLastIndex !== null && ratio < 0 && Math.abs(ratio) > params.longSwipesRatio) {
  122. swiper.slideTo(rewindLastIndex);
  123. } else {
  124. swiper.slideTo(stopIndex);
  125. }
  126. }
  127. } else {
  128. // Short swipes
  129. if (!params.shortSwipes) {
  130. swiper.slideTo(swiper.activeIndex);
  131. return;
  132. }
  133. const isNavButtonTarget = swiper.navigation && (e.target === swiper.navigation.nextEl || e.target === swiper.navigation.prevEl);
  134. if (!isNavButtonTarget) {
  135. if (swiper.swipeDirection === 'next') {
  136. swiper.slideTo(rewindFirstIndex !== null ? rewindFirstIndex : stopIndex + increment);
  137. }
  138. if (swiper.swipeDirection === 'prev') {
  139. swiper.slideTo(rewindLastIndex !== null ? rewindLastIndex : stopIndex);
  140. }
  141. } else if (e.target === swiper.navigation.nextEl) {
  142. swiper.slideTo(stopIndex + increment);
  143. } else {
  144. swiper.slideTo(stopIndex);
  145. }
  146. }
  147. }