slideTo.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { animateCSSModeScroll } from '../../shared/utils.js';
  2. export default function slideTo(index, speed, runCallbacks, internal, initial) {
  3. if (index === void 0) {
  4. index = 0;
  5. }
  6. if (speed === void 0) {
  7. speed = this.params.speed;
  8. }
  9. if (runCallbacks === void 0) {
  10. runCallbacks = true;
  11. }
  12. if (typeof index !== 'number' && typeof index !== 'string') {
  13. throw new Error(`The 'index' argument cannot have type other than 'number' or 'string'. [${typeof index}] given.`);
  14. }
  15. if (typeof index === 'string') {
  16. /**
  17. * The `index` argument converted from `string` to `number`.
  18. * @type {number}
  19. */
  20. const indexAsNumber = parseInt(index, 10);
  21. /**
  22. * Determines whether the `index` argument is a valid `number`
  23. * after being converted from the `string` type.
  24. * @type {boolean}
  25. */
  26. const isValidNumber = isFinite(indexAsNumber);
  27. if (!isValidNumber) {
  28. throw new Error(`The passed-in 'index' (string) couldn't be converted to 'number'. [${index}] given.`);
  29. } // Knowing that the converted `index` is a valid number,
  30. // we can update the original argument's value.
  31. index = indexAsNumber;
  32. }
  33. const swiper = this;
  34. let slideIndex = index;
  35. if (slideIndex < 0) slideIndex = 0;
  36. const {
  37. params,
  38. snapGrid,
  39. slidesGrid,
  40. previousIndex,
  41. activeIndex,
  42. rtlTranslate: rtl,
  43. wrapperEl,
  44. enabled
  45. } = swiper;
  46. if (swiper.animating && params.preventInteractionOnTransition || !enabled && !internal && !initial) {
  47. return false;
  48. }
  49. const skip = Math.min(swiper.params.slidesPerGroupSkip, slideIndex);
  50. let snapIndex = skip + Math.floor((slideIndex - skip) / swiper.params.slidesPerGroup);
  51. if (snapIndex >= snapGrid.length) snapIndex = snapGrid.length - 1;
  52. if ((activeIndex || params.initialSlide || 0) === (previousIndex || 0) && runCallbacks) {
  53. swiper.emit('beforeSlideChangeStart');
  54. }
  55. const translate = -snapGrid[snapIndex]; // Update progress
  56. swiper.updateProgress(translate); // Normalize slideIndex
  57. if (params.normalizeSlideIndex) {
  58. for (let i = 0; i < slidesGrid.length; i += 1) {
  59. const normalizedTranslate = -Math.floor(translate * 100);
  60. const normalizedGrid = Math.floor(slidesGrid[i] * 100);
  61. const normalizedGridNext = Math.floor(slidesGrid[i + 1] * 100);
  62. if (typeof slidesGrid[i + 1] !== 'undefined') {
  63. if (normalizedTranslate >= normalizedGrid && normalizedTranslate < normalizedGridNext - (normalizedGridNext - normalizedGrid) / 2) {
  64. slideIndex = i;
  65. } else if (normalizedTranslate >= normalizedGrid && normalizedTranslate < normalizedGridNext) {
  66. slideIndex = i + 1;
  67. }
  68. } else if (normalizedTranslate >= normalizedGrid) {
  69. slideIndex = i;
  70. }
  71. }
  72. } // Directions locks
  73. if (swiper.initialized && slideIndex !== activeIndex) {
  74. if (!swiper.allowSlideNext && translate < swiper.translate && translate < swiper.minTranslate()) {
  75. return false;
  76. }
  77. if (!swiper.allowSlidePrev && translate > swiper.translate && translate > swiper.maxTranslate()) {
  78. if ((activeIndex || 0) !== slideIndex) return false;
  79. }
  80. }
  81. let direction;
  82. if (slideIndex > activeIndex) direction = 'next';else if (slideIndex < activeIndex) direction = 'prev';else direction = 'reset'; // Update Index
  83. if (rtl && -translate === swiper.translate || !rtl && translate === swiper.translate) {
  84. swiper.updateActiveIndex(slideIndex); // Update Height
  85. if (params.autoHeight) {
  86. swiper.updateAutoHeight();
  87. }
  88. swiper.updateSlidesClasses();
  89. if (params.effect !== 'slide') {
  90. swiper.setTranslate(translate);
  91. }
  92. if (direction !== 'reset') {
  93. swiper.transitionStart(runCallbacks, direction);
  94. swiper.transitionEnd(runCallbacks, direction);
  95. }
  96. return false;
  97. }
  98. if (params.cssMode) {
  99. const isH = swiper.isHorizontal();
  100. const t = rtl ? translate : -translate;
  101. if (speed === 0) {
  102. const isVirtual = swiper.virtual && swiper.params.virtual.enabled;
  103. if (isVirtual) {
  104. swiper.wrapperEl.style.scrollSnapType = 'none';
  105. swiper._immediateVirtual = true;
  106. }
  107. wrapperEl[isH ? 'scrollLeft' : 'scrollTop'] = t;
  108. if (isVirtual) {
  109. requestAnimationFrame(() => {
  110. swiper.wrapperEl.style.scrollSnapType = '';
  111. swiper._swiperImmediateVirtual = false;
  112. });
  113. }
  114. } else {
  115. if (!swiper.support.smoothScroll) {
  116. animateCSSModeScroll({
  117. swiper,
  118. targetPosition: t,
  119. side: isH ? 'left' : 'top'
  120. });
  121. return true;
  122. }
  123. wrapperEl.scrollTo({
  124. [isH ? 'left' : 'top']: t,
  125. behavior: 'smooth'
  126. });
  127. }
  128. return true;
  129. }
  130. swiper.setTransition(speed);
  131. swiper.setTranslate(translate);
  132. swiper.updateActiveIndex(slideIndex);
  133. swiper.updateSlidesClasses();
  134. swiper.emit('beforeTransitionStart', speed, internal);
  135. swiper.transitionStart(runCallbacks, direction);
  136. if (speed === 0) {
  137. swiper.transitionEnd(runCallbacks, direction);
  138. } else if (!swiper.animating) {
  139. swiper.animating = true;
  140. if (!swiper.onSlideToWrapperTransitionEnd) {
  141. swiper.onSlideToWrapperTransitionEnd = function transitionEnd(e) {
  142. if (!swiper || swiper.destroyed) return;
  143. if (e.target !== this) return;
  144. swiper.$wrapperEl[0].removeEventListener('transitionend', swiper.onSlideToWrapperTransitionEnd);
  145. swiper.$wrapperEl[0].removeEventListener('webkitTransitionEnd', swiper.onSlideToWrapperTransitionEnd);
  146. swiper.onSlideToWrapperTransitionEnd = null;
  147. delete swiper.onSlideToWrapperTransitionEnd;
  148. swiper.transitionEnd(runCallbacks, direction);
  149. };
  150. }
  151. swiper.$wrapperEl[0].addEventListener('transitionend', swiper.onSlideToWrapperTransitionEnd);
  152. swiper.$wrapperEl[0].addEventListener('webkitTransitionEnd', swiper.onSlideToWrapperTransitionEnd);
  153. }
  154. return true;
  155. }