如何应用延迟加载?

时间:2018-12-23 11:40:38

标签: javascript html lazy-loading

我正在尝试为使用此 theme 创建的页面应用延迟加载功能。此 guide 的基本示例在控制台中没有错误,但是延迟加载不起作用。如何修改我的代码?这是我的div的示例,脚本部分与html文件分开。

<div class="nk-portfolio-list nk-isotope nk-isotope-4-cols nk-isotope-gap-xs nk-popup-gallery">


            <div class="nk-isotope-item lazy" data-filter="Abbigliamento">
                <div class="nk-portfolio-item nk-portfolio-item-info-style-2 nk-portfolio-item-lg">
                    <a href="assets/imagesPro/Abbigliamento/1.jpg" class="nk-portfolio-item-link nk-gallery-item" data-size="2747x3275"></a>
                    <div class="nk-portfolio-item-image"><div class="lazy" style="background-image: url('assets/imagesPro/Abbigliamento/l/l1.jpg')"></div></div>
                    <div class="nk-portfolio-item-info nk-portfolio-item-info-center text-center">
                        <div>
                            <div class="nk-portfolio-item-icon"><span class="pe-7s-look"></span></div>
                        </div>
                    </div>
                </div>
            </div>
....
</div>



$(function() {
    $('.lazy').lazy({
    });
});

1 个答案:

答案 0 :(得分:0)

根据jquery lazy的文档以在其他元素上加载背景,您应该执行以下操作:

<!-- load background images of other element types -->
<div class="lazy" data-src="path/to/image.jpg"></div>

因此,您应该更改代码以包含data-src属性:

<div class="lazy" data-src="assets/imagesPro/Abbigliamento/l/l1.jpg"></div>