简体   繁体   English

懒惰加载滚动图像和“进入视图”

[英]lazy loading images on scroll and “come into view”

I am using Lazy as a lazy image loading plugin. 我使用Lazy作为一个懒惰的图像加载插件。 I have a div where I load divs like this: 我有一个div,我加载这样的div:

<div class="nano-content" id="lScroll">

    /*... MORE LIKE THIS ... */
    <div class="card">
        <div class="city-selected city-medium clickChampion pointer"
     data-champ-id="1">
        <article>
            <div class="info">
                <div class="city">
                    CHAMPNAME
                </div>
            </div>
        </article>
            <figure class="cFigure lazy" data-src="images/champions/CHAMPNAME_0.png"></figure>
        </div>
    </div>
    /*... MORE LIKE THIS ... */

</div>

So I initiate the plugin and it works for the first ones visible and when I scroll: 所以我启动了插件,它适用于第一个可见的,当我滚动时:

var $lazy = $('#lScroll .lazy');
if ($lazy.length) {
    $lazy.Lazy({
        appendScroll: $('#lScroll')
    });
}

But now I have a function that "filters" the divs by their attributes when I enter sth in my search input and it fails to load the image when the according div is shown: 但是现在我有一个函数,当我在搜索输入中输入sth时,按其属性“过滤”div,并且当显示相应的div时无法加载图像:

$(document).on("keyup", "#searchVod", function () {
    var $search = $(this);
    var $sVal = $search.val().toLowerCase();
    if ($sVal !== "") {
        $(".vodCard").hide();
        $('[data-champ*="' + $sVal + '"]').show();
        $('[data-role*="' + $sVal + '"]').show();
    } else {
        $(".vodCard").show();
    }
});

I tried bind: "event" /w and w/out delay: 0 (loading the plugin in the search function) but when I searched it would load ALL images immediately in the background. 我试过bind: "event" / w和w / out delay: 0 (在搜索功能中加载插件)但是当我搜索它时会立即在后台加载所有图像。

Any hint highly appreciated 任何提示高度赞赏

UPDATE: I just noticed in Chrome DevTab after entering one letter in my searchbox it loads ALL the images and eventually the one I am searching for (if its the last it takes some time (30MB sth) 更新:我在Chrome DevTab中注意到在我的搜索框中输入一个字母后,它会加载所有图像并最终加载我正在搜索的图像(如果它是最后一个需要一些时间(30MB ......)

There is an excellent library called Lozad.js which can help you to make it easier to load your images like lazy load do but in easier way. 有一个很棒的库叫做Lozad.js ,它可以帮助你更轻松地加载像延迟加载的图像,但更容易。

You can download it here from Github. 你可以在这里从Github下载它。

Demo page here . 演示页面在这里

Explanation: 说明:
This library will load your images one by one on scrolling to each image anchor by class name. 此库将按类名滚动到每个图像锚点,逐个加载图像。

Example

HTML: HTML:
At the header -> 在标题 - >

<script src="https://cdn.jsdelivr.net/npm/lozad"></script>  

Image element should looks like this: 图像元素应如下所示:

<img class="lozad" data-src="image.png">

Javascript 使用Javascript

// Initialize library
lozad('.lozad', {
    load: function(el) {
        el.src = el.dataset.src;
        el.onload = function() {
            el.classList.add('fade')
        }
    }
}).observe()

Hope it will help you. 希望它会对你有所帮助。
Best, 最好,
Ido. 我做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM