Skip to content

Commit

Permalink
feat(toggle-preloading): add param to disable preloading
Browse files Browse the repository at this point in the history
  • Loading branch information
ipesic committed Jul 24, 2024
1 parent 0239e7f commit 7fea02d
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/components-shared/params-list.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const paramsList = [
'slidePrevClass',
'slideBlankClass',
'wrapperClass',
'lazyPreload',
'lazyPreloaderClass',
'lazyPreloadPrevNext',
'runCallbacksOnInit',
Expand Down
40 changes: 23 additions & 17 deletions src/core/core.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,13 @@ class Swiper {
swiper.setBreakpoint();
}

[...swiper.el.querySelectorAll('[loading="lazy"]')].forEach((imageEl) => {
if (imageEl.complete) {
processLazyPreloader(swiper, imageEl);
}
});
if (swiper.params.lazyPreload) {
[...swiper.el.querySelectorAll('[loading="lazy"]')].forEach((imageEl) => {
if (imageEl.complete) {
processLazyPreloader(swiper, imageEl);
}
});
}

swiper.updateSize();
swiper.updateSlides();
Expand Down Expand Up @@ -604,19 +606,23 @@ class Swiper {

// Attach events
swiper.attachEvents();
const lazyElements = [...swiper.el.querySelectorAll('[loading="lazy"]')];
if (swiper.isElement) {
lazyElements.push(...swiper.hostEl.querySelectorAll('[loading="lazy"]'));
}
lazyElements.forEach((imageEl) => {
if (imageEl.complete) {
processLazyPreloader(swiper, imageEl);
} else {
imageEl.addEventListener('load', (e) => {
processLazyPreloader(swiper, e.target);
});

if (swiper.params.lazyPreload) {
const lazyElements = [...swiper.el.querySelectorAll('[loading="lazy"]')];
if (swiper.isElement) {
lazyElements.push(...swiper.hostEl.querySelectorAll('[loading="lazy"]'));
}
});
lazyElements.forEach((imageEl) => {
if (imageEl.complete) {
processLazyPreloader(swiper, imageEl);
} else {
imageEl.addEventListener('load', (e) => {
processLazyPreloader(swiper, e.target);
});
}
});
}

preload(swiper);

// Init Flag
Expand Down
3 changes: 3 additions & 0 deletions src/core/defaults.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ export default {
slideNextClass: 'swiper-slide-next',
slidePrevClass: 'swiper-slide-prev',
wrapperClass: 'swiper-wrapper',

// Lazy Preload
lazyPreload: true,
lazyPreloaderClass: 'swiper-lazy-preloader',
lazyPreloadPrevNext: 0,

Expand Down
4 changes: 2 additions & 2 deletions src/shared/process-lazy-preloader.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const processLazyPreloader = (swiper, imageEl) => {
if (!swiper || swiper.destroyed || !swiper.params) return;
if (!swiper || swiper.destroyed || !swiper.params || !swiper.params.lazyPreload) return;
const slideSelector = () => (swiper.isElement ? `swiper-slide` : `.${swiper.params.slideClass}`);
const slideEl = imageEl.closest(slideSelector());
if (slideEl) {
Expand Down Expand Up @@ -28,7 +28,7 @@ const unlazy = (swiper, index) => {
};

export const preload = (swiper) => {
if (!swiper || swiper.destroyed || !swiper.params) return;
if (!swiper || swiper.destroyed || !swiper.params || !swiper.params.lazyPreload) return;
let amount = swiper.params.lazyPreloadPrevNext;
const len = swiper.slides.length;
if (!len || !amount || amount < 0) return;
Expand Down
4 changes: 4 additions & 0 deletions src/swiper-vue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ declare const Swiper: DefineComponent<
type: StringConstructor;
default: undefined;
};
lazyPreload: {
type: BooleanConstructor;
default: undefined;
};
lazyPreloaderClass: {
type: StringConstructor;
default: undefined;
Expand Down
7 changes: 7 additions & 0 deletions src/types/swiper-options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,13 @@ export interface SwiperOptions {
*/
wrapperClass?: string;

/**
* Automatically enables lazy loading on images and adds a preloader element. Set to 'false' to disable
*
* @default true
*/
lazyPreload?: boolean;

/**
* CSS class name of lazy preloader
*
Expand Down
1 change: 1 addition & 0 deletions src/vue/swiper.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ const Swiper = {
slideNextClass: { type: String, default: undefined },
slidePrevClass: { type: String, default: undefined },
wrapperClass: { type: String, default: undefined },
lazyPreload: { type: Boolean, default: undefined },
lazyPreloaderClass: { type: String, default: undefined },
lazyPreloadPrevNext: { type: Number, default: undefined },
runCallbacksOnInit: { type: Boolean, default: undefined },
Expand Down

0 comments on commit 7fea02d

Please sign in to comment.