1 | export default function updateActiveIndex(newActiveIndex) {
|
2 | const swiper = this;
|
3 | const translate = swiper.rtlTranslate ? swiper.translate : -swiper.translate;
|
4 | const {
|
5 | slidesGrid,
|
6 | snapGrid,
|
7 | params,
|
8 | activeIndex: previousIndex,
|
9 | realIndex: previousRealIndex,
|
10 | snapIndex: previousSnapIndex
|
11 | } = swiper;
|
12 | let activeIndex = newActiveIndex;
|
13 | let snapIndex;
|
14 |
|
15 | if (typeof activeIndex === 'undefined') {
|
16 | for (let i = 0; i < slidesGrid.length; i += 1) {
|
17 | if (typeof slidesGrid[i + 1] !== 'undefined') {
|
18 | if (translate >= slidesGrid[i] && translate < slidesGrid[i + 1] - (slidesGrid[i + 1] - slidesGrid[i]) / 2) {
|
19 | activeIndex = i;
|
20 | } else if (translate >= slidesGrid[i] && translate < slidesGrid[i + 1]) {
|
21 | activeIndex = i + 1;
|
22 | }
|
23 | } else if (translate >= slidesGrid[i]) {
|
24 | activeIndex = i;
|
25 | }
|
26 | }
|
27 |
|
28 |
|
29 | if (params.normalizeSlideIndex) {
|
30 | if (activeIndex < 0 || typeof activeIndex === 'undefined') activeIndex = 0;
|
31 | }
|
32 | }
|
33 |
|
34 | if (snapGrid.indexOf(translate) >= 0) {
|
35 | snapIndex = snapGrid.indexOf(translate);
|
36 | } else {
|
37 | const skip = Math.min(params.slidesPerGroupSkip, activeIndex);
|
38 | snapIndex = skip + Math.floor((activeIndex - skip) / params.slidesPerGroup);
|
39 | }
|
40 |
|
41 | if (snapIndex >= snapGrid.length) snapIndex = snapGrid.length - 1;
|
42 |
|
43 | if (activeIndex === previousIndex) {
|
44 | if (snapIndex !== previousSnapIndex) {
|
45 | swiper.snapIndex = snapIndex;
|
46 | swiper.emit('snapIndexChange');
|
47 | }
|
48 |
|
49 | return;
|
50 | }
|
51 |
|
52 |
|
53 | const realIndex = parseInt(swiper.slides.eq(activeIndex).attr('data-swiper-slide-index') || activeIndex, 10);
|
54 | Object.assign(swiper, {
|
55 | snapIndex,
|
56 | realIndex,
|
57 | previousIndex,
|
58 | activeIndex
|
59 | });
|
60 | swiper.emit('activeIndexChange');
|
61 | swiper.emit('snapIndexChange');
|
62 |
|
63 | if (previousRealIndex !== realIndex) {
|
64 | swiper.emit('realIndexChange');
|
65 | }
|
66 |
|
67 | if (swiper.initialized || swiper.params.runCallbacksOnInit) {
|
68 | swiper.emit('slideChange');
|
69 | }
|
70 | } |
\ | No newline at end of file |