Skip to content

Commit

Permalink
feat(core): prevent runnding .slideTo methods when Swiper is destroyed
Browse files Browse the repository at this point in the history
fixes #7265
  • Loading branch information
nolimits4web committed Feb 5, 2024
1 parent 6bbb73d commit 05f9c64
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 6 deletions.
9 changes: 6 additions & 3 deletions playground/core/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-pagination"></div>
<div class="swiper-scrollbar"></div>
</div>


Expand All @@ -69,9 +70,11 @@
import Swiper from 'swiper/swiper-bundle.mjs';

window.swiper = new Swiper(".swiper", {
slidesPerView: 5,
spaceBetween: 30,
loop: true,
slidesPerView: 'auto',
// spaceBetween: 30,
scrollbar: {
el: '.swiper-scrollbar',
},
pagination: {
el: ".swiper-pagination",
clickable: true
Expand Down
2 changes: 1 addition & 1 deletion src/core/slide/slideNext.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export default function slideNext(speed = this.params.speed, runCallbacks = true, internal) {
const swiper = this;
const { enabled, params, animating } = swiper;
if (!enabled) return swiper;
if (!enabled || swiper.destroyed) return swiper;
let perGroup = params.slidesPerGroup;
if (params.slidesPerView === 'auto' && params.slidesPerGroup === 1 && params.slidesPerGroupAuto) {
perGroup = Math.max(swiper.slidesPerViewDynamic('current', true), 1);
Expand Down
2 changes: 1 addition & 1 deletion src/core/slide/slidePrev.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export default function slidePrev(speed = this.params.speed, runCallbacks = true, internal) {
const swiper = this;
const { params, snapGrid, slidesGrid, rtlTranslate, enabled, animating } = swiper;
if (!enabled) return swiper;
if (!enabled || swiper.destroyed) return swiper;
const isVirtual = swiper.virtual && params.virtual.enabled;

if (params.loop) {
Expand Down
1 change: 1 addition & 0 deletions src/core/slide/slideReset.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint no-unused-vars: "off" */
export default function slideReset(speed = this.params.speed, runCallbacks = true, internal) {
const swiper = this;
if (swiper.destroyed) return;
return swiper.slideTo(swiper.activeIndex, speed, runCallbacks, internal);
}
4 changes: 3 additions & 1 deletion src/core/slide/slideTo.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default function slideTo(
}

const swiper = this;

let slideIndex = index;
if (slideIndex < 0) slideIndex = 0;

Expand All @@ -28,7 +29,8 @@ export default function slideTo(

if (
(swiper.animating && params.preventInteractionOnTransition) ||
(!enabled && !internal && !initial)
(!enabled && !internal && !initial) ||
swiper.destroyed
) {
return false;
}
Expand Down
1 change: 1 addition & 0 deletions src/core/slide/slideToClickedSlide.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { elementChildren, nextTick } from '../../shared/utils.mjs';

export default function slideToClickedSlide() {
const swiper = this;
if (swiper.destroyed) return;
const { params, slidesEl } = swiper;

const slidesPerView =
Expand Down
1 change: 1 addition & 0 deletions src/core/slide/slideToClosest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default function slideToClosest(
threshold = 0.5,
) {
const swiper = this;
if (swiper.destroyed) return;
let index = swiper.activeIndex;
const skip = Math.min(swiper.params.slidesPerGroupSkip, index);
const snapIndex = skip + Math.floor((index - skip) / swiper.params.slidesPerGroup);
Expand Down
1 change: 1 addition & 0 deletions src/core/slide/slideToLoop.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default function slideToLoop(
index = indexAsNumber;
}
const swiper = this;
if (swiper.destroyed) return;
const gridEnabled = swiper.grid && swiper.params.grid && swiper.params.grid.rows > 1;
let newIndex = index;
if (swiper.params.loop) {
Expand Down

0 comments on commit 05f9c64

Please sign in to comment.