【问题标题】:JavaScript - lazy load <li> elementJavaScript - 延迟加载 <li> 元素
【发布时间】:2021-08-11 18:06:54
【问题描述】:

有什么方法可以使用基本的 js 函数在滚动时延迟加载 li 元素? 我发现了一些我没有成功适应我的例子的例子。

<div class="col-md-12">
    <ul class="timeline">

       <li class="lazyload">
           // content
         </li>

        < li class="lazyload">
           // content
         ​</li> 

       ..etc

      </ul>
   </div>

我对 JavaScript 很陌生,这是我的工作 JS 代码。 谢谢。

$(document).ready(function () {

function lazyload()
{
    var wt = $(window).scrollTop();    //* top of the window
    var wb = wt + $(window).height();  //* bottom of the window

    $(".ads").each(function () {
        var ot = $(this).offset().top;  //* top of object (i.e. advertising div)
        var ob = ot + $(this).height(); //* bottom of object

        if (!$(this).attr("loaded") && wt<=ob && wb >= ot) {
            $(this).html("lazyload");
            $(this).attr("loaded",true);
        }
    });
}

$(window).scroll(lazyload);
lazyload();

});

【问题讨论】:

    标签: javascript html jquery lazy-loading jquery-lazyload


    【解决方案1】:

    下面怎么样

    var mincount = 20;
    var maxcount = 40;
    
    $(".image-gallery-ul li").slice(20).hide();
    
    $(".image-gallery-list").scroll(function() {
    
      if($(".image-gallery-list").scrollTop() + $(".image-gallery-list").height() >= $(".image-gallery-list")[0].scrollHeight) {
        
        $(".image-gallery-ul li").slice(mincount,maxcount).fadeIn(1000);
    
        mincount = mincount+20;
        maxcount = maxcount+20;
    
      }
    });
    

    来自https://codepen.io/kvzivn/pen/MYKMVG

    【讨论】:

    • 谢谢。当我从 PHP metod 循环数据时,这个例子可以用 foreach 应用到我的 twig html 视图吗? @SecurityObscurity
    • @pohofo 是的,你可以,只需从 php 中获取数据并将它们放在 html 上,让 js 完成剩下的工作,检查 codepen
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-05
    • 1970-01-01
    • 2023-03-10
    • 2021-05-30
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    相关资源
    最近更新 更多