Skip to content

Commit

Permalink
fix(core): add/remove slide classes only when changed
Browse files Browse the repository at this point in the history
fixes #7356
  • Loading branch information
nolimits4web committed Mar 28, 2024
1 parent 5b0fa84 commit 3312fba
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions src/core/update/updateSlidesClasses.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { elementChildren, elementNextAll, elementPrevAll } from '../../shared/utils.mjs';

const toggleSlideClasses = (slideEl, condition, className) => {
if (condition && !slideEl.classList.contains(className)) {
slideEl.classList.add(className);
} else if (!condition && slideEl.classList.contains(className)) {
slideEl.classList.remove(className);
}
};

export default function updateSlidesClasses() {
const swiper = this;

Expand All @@ -13,9 +21,6 @@ export default function updateSlidesClasses() {
`.${params.slideClass}${selector}, swiper-slide${selector}`,
)[0];
};
slides.forEach((slideEl) => {
slideEl.classList.remove(params.slideActiveClass, params.slideNextClass, params.slidePrevClass);
});

let activeSlide;
let prevSlide;
Expand All @@ -38,37 +43,28 @@ export default function updateSlidesClasses() {
activeSlide = slides[activeIndex];
}
}
if (activeSlide) {
// Active classes
activeSlide.classList.add(params.slideActiveClass);

if (gridEnabled) {
if (nextSlide) {
nextSlide.classList.add(params.slideNextClass);
}
if (prevSlide) {
prevSlide.classList.add(params.slidePrevClass);
}
} else {
if (activeSlide) {
if (!gridEnabled) {
// Next Slide
nextSlide = elementNextAll(activeSlide, `.${params.slideClass}, swiper-slide`)[0];
if (params.loop && !nextSlide) {
nextSlide = slides[0];
}
if (nextSlide) {
nextSlide.classList.add(params.slideNextClass);
}

// Prev Slide
prevSlide = elementPrevAll(activeSlide, `.${params.slideClass}, swiper-slide`)[0];
if (params.loop && !prevSlide === 0) {
prevSlide = slides[slides.length - 1];
}
if (prevSlide) {
prevSlide.classList.add(params.slidePrevClass);
}
}
}

slides.forEach((slideEl) => {
toggleSlideClasses(slideEl, slideEl === activeSlide, params.slideActiveClass);
toggleSlideClasses(slideEl, slideEl === nextSlide, params.slideNextClass);
toggleSlideClasses(slideEl, slideEl === prevSlide, params.slidePrevClass);
});

swiper.emitSlidesClasses();
}

0 comments on commit 3312fba

Please sign in to comment.