SwiperJS延迟加载-使用data-src时,图像不显示

时间:2020-10-20 11:59:25

标签: swiperjs statamic

我一直在努力为自己的Statamic 3项目实现SwiperJS。

我有一个工作的传送带/滑块,当不使用data-src和延迟加载时,它可以很好地工作。但是,一旦我尝试按照其网站上的指南实施延迟加载。我得到的是带有无限加载程序的白色图像/背景或没有加载程序的白色图像/背景。

这是我的代码:

HTML(图像来自鹿角foreach):

<div class="flex flex-col w-1/3 p-2">
    <div class="h-full w-full swiper-container">
        <div class="h-48 swiper-wrapper">
            {{ foreach:photos }}
                <div class="swiper-slide">
                    <img data-src="{{ value:url }}" class="w-full h-full object-cover object-center swiper-lazy">
                    <div class="swiper-lazy-preloader"></div>
                  </div>
            {{ /foreach:photos}}
        </div>
        <div class="swiper-pagination"></div>
        <div class="swiper-button-prev"></div>
        <div class="swiper-button-next"></div>
    </div>
</div>

我的JS:

// core version + navigation, pagination modules:
import Swiper, { Navigation, Pagination } from 'swiper';
import 'swiper/swiper-bundle.css';

// configure Swiper to use modules
Swiper.use([Navigation, Pagination]);


var mySwiper = new Swiper('.swiper-container', {
    slidesPerView: 1,
    spaceBetween: 0,
    slidesPerGroup: 1,
    navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
    },
    pagination: {
        el: '.swiper-pagination'
    },
    mousewheel: false,
    keyboard: true,
    loop: true,
    parallax: true,
    grabCursor: true,

    // Disable preloading of all images
    preloadImages: false,
    // Enable lazy loading
    lazy: {
        loadPrevNext: true,
    },
});

当前结果: Result

有人知道为什么我的图像没有被渲染吗?

亲切的问候, 罗伯特

1 个答案:

答案 0 :(得分:0)

您需要导入延迟加载。

改变

import Swiper, { Navigation, Pagination } from 'swiper';

import Swiper, { Navigation, Pagination, Lazy } from 'swiper';

和改变

Swiper.use([Navigation, Pagination]);

Swiper.use([Navigation, Pagination, Lazy]);