Skip to content

Commit 7fea02d

Browse files
author
ipesic
committed
feat(toggle-preloading): add param to disable preloading
1 parent 0239e7f commit 7fea02d

File tree

7 files changed

+41
-19
lines changed

7 files changed

+41
-19
lines changed

src/components-shared/params-list.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ const paramsList = [
8989
'slidePrevClass',
9090
'slideBlankClass',
9191
'wrapperClass',
92+
'lazyPreload',
9293
'lazyPreloaderClass',
9394
'lazyPreloadPrevNext',
9495
'runCallbacksOnInit',

src/core/core.mjs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,13 @@ class Swiper {
392392
swiper.setBreakpoint();
393393
}
394394

395-
[...swiper.el.querySelectorAll('[loading="lazy"]')].forEach((imageEl) => {
396-
if (imageEl.complete) {
397-
processLazyPreloader(swiper, imageEl);
398-
}
399-
});
395+
if (swiper.params.lazyPreload) {
396+
[...swiper.el.querySelectorAll('[loading="lazy"]')].forEach((imageEl) => {
397+
if (imageEl.complete) {
398+
processLazyPreloader(swiper, imageEl);
399+
}
400+
});
401+
}
400402

401403
swiper.updateSize();
402404
swiper.updateSlides();
@@ -604,19 +606,23 @@ class Swiper {
604606

605607
// Attach events
606608
swiper.attachEvents();
607-
const lazyElements = [...swiper.el.querySelectorAll('[loading="lazy"]')];
608-
if (swiper.isElement) {
609-
lazyElements.push(...swiper.hostEl.querySelectorAll('[loading="lazy"]'));
610-
}
611-
lazyElements.forEach((imageEl) => {
612-
if (imageEl.complete) {
613-
processLazyPreloader(swiper, imageEl);
614-
} else {
615-
imageEl.addEventListener('load', (e) => {
616-
processLazyPreloader(swiper, e.target);
617-
});
609+
610+
if (swiper.params.lazyPreload) {
611+
const lazyElements = [...swiper.el.querySelectorAll('[loading="lazy"]')];
612+
if (swiper.isElement) {
613+
lazyElements.push(...swiper.hostEl.querySelectorAll('[loading="lazy"]'));
618614
}
619-
});
615+
lazyElements.forEach((imageEl) => {
616+
if (imageEl.complete) {
617+
processLazyPreloader(swiper, imageEl);
618+
} else {
619+
imageEl.addEventListener('load', (e) => {
620+
processLazyPreloader(swiper, e.target);
621+
});
622+
}
623+
});
624+
}
625+
620626
preload(swiper);
621627

622628
// Init Flag

src/core/defaults.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ export default {
131131
slideNextClass: 'swiper-slide-next',
132132
slidePrevClass: 'swiper-slide-prev',
133133
wrapperClass: 'swiper-wrapper',
134+
135+
// Lazy Preload
136+
lazyPreload: true,
134137
lazyPreloaderClass: 'swiper-lazy-preloader',
135138
lazyPreloadPrevNext: 0,
136139

src/shared/process-lazy-preloader.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export const processLazyPreloader = (swiper, imageEl) => {
2-
if (!swiper || swiper.destroyed || !swiper.params) return;
2+
if (!swiper || swiper.destroyed || !swiper.params || !swiper.params.lazyPreload) return;
33
const slideSelector = () => (swiper.isElement ? `swiper-slide` : `.${swiper.params.slideClass}`);
44
const slideEl = imageEl.closest(slideSelector());
55
if (slideEl) {
@@ -28,7 +28,7 @@ const unlazy = (swiper, index) => {
2828
};
2929

3030
export const preload = (swiper) => {
31-
if (!swiper || swiper.destroyed || !swiper.params) return;
31+
if (!swiper || swiper.destroyed || !swiper.params || !swiper.params.lazyPreload) return;
3232
let amount = swiper.params.lazyPreloadPrevNext;
3333
const len = swiper.slides.length;
3434
if (!len || !amount || amount < 0) return;

src/swiper-vue.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ declare const Swiper: DefineComponent<
327327
type: StringConstructor;
328328
default: undefined;
329329
};
330+
lazyPreload: {
331+
type: BooleanConstructor;
332+
default: undefined;
333+
};
330334
lazyPreloaderClass: {
331335
type: StringConstructor;
332336
default: undefined;

src/types/swiper-options.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,13 @@ export interface SwiperOptions {
856856
*/
857857
wrapperClass?: string;
858858

859+
/**
860+
* Automatically enables lazy loading on images and adds a preloader element. Set to 'false' to disable
861+
*
862+
* @default true
863+
*/
864+
lazyPreload?: boolean;
865+
859866
/**
860867
* CSS class name of lazy preloader
861868
*

src/vue/swiper.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ const Swiper = {
109109
slideNextClass: { type: String, default: undefined },
110110
slidePrevClass: { type: String, default: undefined },
111111
wrapperClass: { type: String, default: undefined },
112+
lazyPreload: { type: Boolean, default: undefined },
112113
lazyPreloaderClass: { type: String, default: undefined },
113114
lazyPreloadPrevNext: { type: Number, default: undefined },
114115
runCallbacksOnInit: { type: Boolean, default: undefined },

0 commit comments

Comments
 (0)