Skip to content

Commit

Permalink
fix(core): correctly calc slide's DOM indexes, actual for Element
Browse files Browse the repository at this point in the history
  • Loading branch information
nolimits4web committed Feb 27, 2023
1 parent adb40f4 commit 18b1f2b
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 20 deletions.
8 changes: 8 additions & 0 deletions src/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
createElement,
elementChildren,
elementStyle,
elementIndex,
} from '../shared/utils.js';
import { getSupport } from '../shared/get-support.js';
import { getDevice } from '../shared/get-device.js';
Expand Down Expand Up @@ -221,6 +222,13 @@ class Swiper {
return swiper;
}

getSlideIndex(slideEl) {
const { slidesEl, params } = this;
const slides = elementChildren(slidesEl, `.${params.slideClass}, swiper-slide`);
const firstSlideIndex = elementIndex(slides[0]);
return elementIndex(slideEl) - firstSlideIndex;
}

recalcSlides() {
const swiper = this;
const { slidesEl, params } = swiper;
Expand Down
4 changes: 1 addition & 3 deletions src/core/loop/loopFix.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { elementIndex } from '../../shared/utils.js';

export default function loopFix({
slideRealIndex,
slideTo = true,
Expand Down Expand Up @@ -50,7 +48,7 @@ export default function loopFix({
let activeIndex = swiper.activeIndex;

if (typeof activeSlideIndex === 'undefined') {
activeSlideIndex = elementIndex(
activeSlideIndex = swiper.getSlideIndex(
swiper.slides.filter((el) => el.classList.contains('swiper-slide-active'))[0],
);
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/core/slide/slideToClickedSlide.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { elementChildren, elementIndex, nextTick } from '../../shared/utils.js';
import { elementChildren, nextTick } from '../../shared/utils.js';

export default function slideToClickedSlide() {
const swiper = this;
Expand All @@ -18,7 +18,7 @@ export default function slideToClickedSlide() {
slideToIndex > swiper.slides.length - swiper.loopedSlides + slidesPerView / 2
) {
swiper.loopFix();
slideToIndex = elementIndex(
slideToIndex = swiper.getSlideIndex(
elementChildren(slidesEl, `${slideSelector}[data-swiper-slide-index="${realIndex}"]`)[0],
);

Expand All @@ -30,7 +30,7 @@ export default function slideToClickedSlide() {
}
} else if (slideToIndex > swiper.slides.length - slidesPerView) {
swiper.loopFix();
slideToIndex = elementIndex(
slideToIndex = swiper.getSlideIndex(
elementChildren(slidesEl, `${slideSelector}[data-swiper-slide-index="${realIndex}"]`)[0],
);

Expand Down
4 changes: 1 addition & 3 deletions src/core/slide/slideToLoop.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { elementIndex } from '../../shared/utils.js';

export default function slideToLoop(
index = 0,
speed = this.params.speed,
Expand All @@ -19,7 +17,7 @@ export default function slideToLoop(
// eslint-disable-next-line
newIndex = newIndex + swiper.virtual.slidesBefore;
} else {
newIndex = elementIndex(
newIndex = swiper.getSlideIndex(
swiper.slides.filter(
(slideEl) => slideEl.getAttribute('data-swiper-slide-index') * 1 === newIndex,
)[0],
Expand Down
6 changes: 2 additions & 4 deletions src/core/update/updateProgress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { elementIndex } from '../../shared/utils.js';

export default function updateProgress(translate) {
const swiper = this;
if (typeof translate === 'undefined') {
Expand Down Expand Up @@ -27,10 +25,10 @@ export default function updateProgress(translate) {
}

if (params.loop) {
const firstSlideIndex = elementIndex(
const firstSlideIndex = swiper.getSlideIndex(
swiper.slides.filter((el) => el.getAttribute('data-swiper-slide-index') === '0')[0],
);
const lastSlideIndex = elementIndex(
const lastSlideIndex = swiper.getSlideIndex(
swiper.slides.filter(
(el) => el.getAttribute('data-swiper-slide-index') * 1 === swiper.slides.length - 1,
)[0],
Expand Down
6 changes: 3 additions & 3 deletions src/modules/hash-navigation/hash-navigation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getWindow, getDocument } from 'ssr-window';
import { elementChildren, elementIndex } from '../../shared/utils.js';
import { elementChildren } from '../../shared/utils.js';

export default function HashNavigation({ swiper, extendParams, emit, on }) {
let initialized = false;
Expand All @@ -17,7 +17,7 @@ export default function HashNavigation({ swiper, extendParams, emit, on }) {
const newHash = document.location.hash.replace('#', '');
const activeSlideHash = swiper.slides[swiper.activeIndex].getAttribute('data-hash');
if (newHash !== activeSlideHash) {
const newIndex = elementIndex(
const newIndex = swiper.getSlideIndex(
elementChildren(
swiper.slidesEl,
`.${swiper.params.slideClass}[data-hash="${newHash}"], swiper-slide[data-hash="${newHash}"]`,
Expand Down Expand Up @@ -61,7 +61,7 @@ export default function HashNavigation({ swiper, extendParams, emit, on }) {
const slide = swiper.slides[i];
const slideHash = slide.getAttribute('data-hash') || slide.getAttribute('data-history');
if (slideHash === hash) {
const index = elementIndex(slide);
const index = swiper.getSlideIndex(slide);
swiper.slideTo(index, speed, swiper.params.runCallbacksOnInit, true);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/modules/history/history.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getWindow } from 'ssr-window';
import { elementIndex } from '../../shared/utils.js';

export default function History({ swiper, extendParams, on }) {
extendParams({
Expand Down Expand Up @@ -80,7 +79,7 @@ export default function History({ swiper, extendParams, on }) {
const slide = swiper.slides[i];
const slideHistory = slugify(slide.getAttribute('data-history'));
if (slideHistory === value) {
const index = elementIndex(slide);
const index = swiper.getSlideIndex(slide);
swiper.slideTo(index, speed, runCallbacks);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/shared/effect-virtual-transition-end.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { elementIndex, elementTransitionEnd } from './utils.js';
import { elementTransitionEnd } from './utils.js';

export default function effectVirtualTransitionEnd({
swiper,
Expand Down Expand Up @@ -27,7 +27,7 @@ export default function effectVirtualTransitionEnd({
const el = transformEl.classList.contains('swiper-slide-transform')
? getSlide(transformEl)
: transformEl;
return elementIndex(el) === activeIndex;
return swiper.getSlideIndex(el) === activeIndex;
});
}
transitionEndTarget.forEach((el) => {
Expand Down

0 comments on commit 18b1f2b

Please sign in to comment.