@@ -9,7 +9,8 @@ export const processLazyPreloader = (swiper, imageEl) => {
9
9
} ;
10
10
11
11
const unlazy = ( swiper , index ) => {
12
- const imageEl = swiper . slides [ index ] . querySelector ( 'loading="lazy"' ) ;
12
+ if ( ! swiper . slides [ index ] ) return ;
13
+ const imageEl = swiper . slides [ index ] . querySelector ( '[loading="lazy"]' ) ;
13
14
if ( imageEl ) imageEl . removeAttribute ( 'loading' ) ;
14
15
} ;
15
16
@@ -19,15 +20,24 @@ export const preload = (swiper) => {
19
20
const len = swiper . slides . length ;
20
21
if ( ! len || ! amount || amount < 0 ) return ;
21
22
amount = Math . min ( amount , len ) ;
22
- const active = swiper . activeSlide ;
23
+ const slidesPerView =
24
+ swiper . params . slidesPerView === 'auto'
25
+ ? swiper . slidesPerViewDynamic ( )
26
+ : Math . ceil ( swiper . params . slidesPerView ) ;
27
+ const activeIndex = swiper . activeIndex ;
28
+ const slideIndexLastInView = activeIndex + slidesPerView - 1 ;
23
29
if ( swiper . params . rewind ) {
24
- for ( let i = active - amount ; i <= active + amount ; i += 1 ) {
25
- const reali = ( ( i % len ) + len ) % len ;
26
- if ( reali !== active ) unlazy ( swiper , reali ) ;
30
+ for ( let i = activeIndex - amount ; i <= slideIndexLastInView + amount ; i += 1 ) {
31
+ const realIndex = ( ( i % len ) + len ) % len ;
32
+ if ( realIndex !== activeIndex && realIndex > slideIndexLastInView ) unlazy ( swiper , realIndex ) ;
27
33
}
28
34
} else {
29
- for ( let i = Math . max ( active - amount , 0 ) ; i <= Math . min ( active + amount , len - 1 ) ; i += 1 ) {
30
- if ( i !== active ) unlazy ( swiper , i ) ;
35
+ for (
36
+ let i = Math . max ( slideIndexLastInView - amount , 0 ) ;
37
+ i <= Math . min ( slideIndexLastInView + amount , len - 1 ) ;
38
+ i += 1
39
+ ) {
40
+ if ( i !== activeIndex && i > slideIndexLastInView ) unlazy ( swiper , i ) ;
31
41
}
32
42
}
33
43
} ;
0 commit comments