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