Skip to content

Commit

Permalink
chore: add getSlideIndexByData method
Browse files Browse the repository at this point in the history
  • Loading branch information
nolimits4web committed Mar 16, 2023
1 parent e24bd8c commit bcb275f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
8 changes: 8 additions & 0 deletions src/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,14 @@ class Swiper {
return elementIndex(slideEl) - firstSlideIndex;
}

getSlideIndexByData(index) {
return this.getSlideIndex(
this.slides.filter(
(slideEl) => slideEl.getAttribute('data-swiper-slide-index') * 1 === index,
)[0],
);
}

recalcSlides() {
const swiper = this;
const { slidesEl, params } = swiper;
Expand Down
6 changes: 1 addition & 5 deletions src/core/slide/slideToLoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ export default function slideToLoop(
// eslint-disable-next-line
newIndex = newIndex + swiper.virtual.slidesBefore;
} else {
newIndex = swiper.getSlideIndex(
swiper.slides.filter(
(slideEl) => slideEl.getAttribute('data-swiper-slide-index') * 1 === newIndex,
)[0],
);
newIndex = swiper.getSlideIndexByData(newIndex);
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/core/update/updateAutoHeight.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ export default function updateAutoHeight(speed) {

const getSlideByIndex = (index) => {
if (isVirtual) {
return swiper.slides.filter(
(el) => parseInt(el.getAttribute('data-swiper-slide-index'), 10) === index,
)[0];
return swiper.getSlideIndexByData(index);
}
return swiper.slides[index];
};
Expand Down
10 changes: 2 additions & 8 deletions src/core/update/updateProgress.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,8 @@ export default function updateProgress(translate) {
}

if (params.loop) {
const firstSlideIndex = swiper.getSlideIndex(
swiper.slides.filter((el) => el.getAttribute('data-swiper-slide-index') === '0')[0],
);
const lastSlideIndex = swiper.getSlideIndex(
swiper.slides.filter(
(el) => el.getAttribute('data-swiper-slide-index') * 1 === swiper.slides.length - 1,
)[0],
);
const firstSlideIndex = swiper.getSlideIndexByData(0);
const lastSlideIndex = swiper.getSlideIndexByData(swiper.slides.length - 1);
const firstSlideTranslate = swiper.slidesGrid[firstSlideIndex];
const lastSlideTranslate = swiper.slidesGrid[lastSlideIndex];
const translateMax = swiper.slidesGrid[swiper.slidesGrid.length - 1];
Expand Down

0 comments on commit bcb275f

Please sign in to comment.