onTouchEnd.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import {
  2. now,
  3. nextTick
  4. } from '../../shared/utils.js';
  5. export default function onTouchEnd(event) {
  6. const swiper = this;
  7. const data = swiper.touchEventsData;
  8. const {
  9. params,
  10. touches,
  11. rtlTranslate: rtl,
  12. slidesGrid,
  13. enabled
  14. } = swiper;
  15. if (!enabled) return;
  16. let e = event;
  17. if (e.originalEvent) e = e.originalEvent;
  18. if (data.allowTouchCallbacks) {
  19. swiper.emit('touch-end', e);
  20. }
  21. data.allowTouchCallbacks = false;
  22. if (!data.isTouched) {
  23. if (data.isMoved && params.grabCursor) {
  24. swiper.setGrabCursor(false);
  25. }
  26. data.isMoved = false;
  27. data.startMoving = false;
  28. return;
  29. }
  30. if (params.grabCursor && data.isMoved && data.isTouched && (swiper.allowSlideNext === true || swiper
  31. .allowSlidePrev === true)) {
  32. swiper.setGrabCursor(false);
  33. }
  34. const touchEndTime = now();
  35. const timeDiff = touchEndTime - data.touchStartTime; // Tap, doubleTap, Click
  36. if (swiper.allowClick) {
  37. const pathTree = e.path || e.composedPath && e.composedPath();
  38. // swiper.updateClickedSlide(pathTree && pathTree[0] || e.target);
  39. swiper.emit('tap click', e);
  40. if (timeDiff < 300 && touchEndTime - data.lastClickTime < 300) {
  41. swiper.emit('doubleTap doubleClick', e);
  42. }
  43. }
  44. data.lastClickTime = now();
  45. nextTick(() => {
  46. if (!swiper.destroyed) swiper.allowClick = true;
  47. });
  48. if (!data.isTouched || !data.isMoved || !swiper.swipeDirection || touches.diff === 0 || data.currentTranslate ===
  49. data.startTranslate) {
  50. data.isTouched = false;
  51. data.isMoved = false;
  52. data.startMoving = false;
  53. return;
  54. }
  55. data.isTouched = false;
  56. data.isMoved = false;
  57. data.startMoving = false;
  58. let currentPos;
  59. if (params.followFinger) {
  60. currentPos = rtl ? swiper.translate : -swiper.translate;
  61. } else {
  62. currentPos = -data.currentTranslate;
  63. }
  64. if (params.cssMode) {
  65. return;
  66. }
  67. if (swiper.params.freeMode && params.freeMode.enabled) {
  68. swiper.freeMode.onTouchEnd({
  69. currentPos
  70. });
  71. return;
  72. }
  73. let stopIndex = 0;
  74. let groupSize = swiper.slidesSizesGrid[0];
  75. for (let i = 0; i < slidesGrid.length; i += i < params.slidesPerGroupSkip ? 1 : params.slidesPerGroup) {
  76. const increment = i < params.slidesPerGroupSkip - 1 ? 1 : params.slidesPerGroup;
  77. if (typeof slidesGrid[i + increment] !== 'undefined') {
  78. if (currentPos >= slidesGrid[i] && currentPos < slidesGrid[i + increment]) {
  79. stopIndex = i;
  80. groupSize = slidesGrid[i + increment] - slidesGrid[i];
  81. }
  82. } else if (currentPos >= slidesGrid[i]) {
  83. stopIndex = i;
  84. groupSize = slidesGrid[slidesGrid.length - 1] - slidesGrid[slidesGrid.length - 2];
  85. }
  86. }
  87. const ratio = (currentPos - slidesGrid[stopIndex]) / groupSize;
  88. const increment = stopIndex < params.slidesPerGroupSkip - 1 ? 1 : params.slidesPerGroup;
  89. if (timeDiff > params.longSwipesMs) {
  90. if (!params.longSwipes) {
  91. swiper.slideTo(swiper.activeIndex);
  92. return;
  93. }
  94. if (swiper.swipeDirection === 'next') {
  95. if (ratio >= params.longSwipesRatio) swiper.slideTo(stopIndex + increment);
  96. else swiper.slideTo(stopIndex);
  97. }
  98. if (swiper.swipeDirection === 'prev') {
  99. if (ratio > 1 - params.longSwipesRatio) swiper.slideTo(stopIndex + increment);
  100. else swiper.slideTo(stopIndex);
  101. }
  102. } else {
  103. if (!params.shortSwipes) {
  104. swiper.slideTo(swiper.activeIndex);
  105. return;
  106. }
  107. const isNavButtonTarget = swiper.navigation && (e.target === swiper.navigation.nextEl || e.target === swiper
  108. .navigation.prevEl);
  109. if (!isNavButtonTarget) {
  110. if (swiper.swipeDirection === 'next') {
  111. swiper.slideTo(stopIndex + increment);
  112. }
  113. if (swiper.swipeDirection === 'prev') {
  114. swiper.slideTo(stopIndex);
  115. }
  116. } else if (e.target === swiper.navigation.nextEl) {
  117. swiper.slideTo(stopIndex + increment);
  118. } else {
  119. swiper.slideTo(stopIndex);
  120. }
  121. }
  122. }