1

我一直在努力尝试将 SwiperJS 实施到我的 Statamic 3 项目中。

我有一个工作轮播/滑块,当不使用 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,
    },
});

当前结果: 结果

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

亲切的问候,罗伯特

4

1 回答 1

3

您需要导入延迟加载。

改变

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

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

和改变

Swiper.use([Navigation, Pagination]);

Swiper.use([Navigation, Pagination, Lazy]);
于 2021-03-10T13:53:38.543 回答