【问题标题】:SwiperJS lazy loading - Image not displaying when using data-src as the guide saysSwiperJS 延迟加载 - 如指南所述,使用 data-src 时图像不显示
【发布时间】:2021-02-03 06:07:34
【问题描述】:

我一直在努力尝试在我的 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,
    },
});

当前结果:

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

亲切的问候, 罗伯特

【问题讨论】:

标签: swiperjs statamic


【解决方案1】:

你需要导入延迟加载。

改变

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

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

改变

Swiper.use([Navigation, Pagination]);

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

【讨论】:

    猜你喜欢
    • 2020-06-20
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    相关资源
    最近更新 更多