jquery - 如何延迟加载博客文章以缩小页面大小?

标签 jquery lazy-loading

我有一个博客文章列表,数量已达到 25+,但它们都在一个页面中,所以我需要尝试构建一个惰性加载器。

我尝试过各种插件,但都不起作用

http://jsfiddle.net/tara_irvine/S9GGz/6/

JavaScript:

(function($) {

    $.belowthefold = function(element, settings) {
        var fold = $(window).height() + $(window).scrollTop();
        return fold <= $(element).offset().top - settings.threshold;
    };

    $.abovethetop = function(element, settings) {
        var top = $(window).scrollTop();
        return top >= $(element).offset().top + $(element).height() - settings.threshold;
    };

    $.rightofscreen = function(element, settings) {
        var fold = $(window).width() + $(window).scrollLeft();
        return fold <= $(element).offset().left - settings.threshold;
    };

    $.leftofscreen = function(element, settings) {
        var left = $(window).scrollLeft();
        return left >= $(element).offset().left + $(element).width() - settings.threshold;
    };

    $.inviewport = function(element, settings) {
        return !$.rightofscreen(element, settings) && !$.leftofscreen(element, settings) && !$.belowthefold(element, settings) && !$.abovethetop(element, settings);
    };

    $.extend($.expr[':'], {
        "below-the-fold": function(a, i, m) {
            return $.belowthefold(a, {threshold : 0});
        },
        "above-the-top": function(a, i, m) {
            return $.abovethetop(a, {threshold : 0});
        },
        "left-of-screen": function(a, i, m) {
            return $.leftofscreen(a, {threshold : 0});
        },
        "right-of-screen": function(a, i, m) {
            return $.rightofscreen(a, {threshold : 0});
        },
        "in-viewport": function(a, i, m) {
            return $.inviewport(a, {threshold : 0});
        }
    });


})(jQuery);

$(function() {
    var previous = "";
    $(window).bind("scroll", function(event) {

        $(".blog-post:in-viewport").each(function() {
          $(this).addClass("visible");
        });


    });
});

http://jsfiddle.net/tara_irvine/S9GGz/9/

http://jsfiddle.net/tara_irvine/S9GGz/13/

有没有办法检查 div 的顶部值,如果它在 View 中,则添加一个类,以便 div 变得可见(页面加载时 div 被隐藏)?

我看过这些帖子,但在尝试了各种解决方案后,没有一个对我有用。

我哪里出错了?

最佳答案

jQuery Waypoints是一个很好的插件,可以对进入 View 的元素使用react;他们甚至有a lazy-loading example .

关于jquery - 如何延迟加载博客文章以缩小页面大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11931264/

相关文章:

javascript - 按我的 json 对象分组并获取每个组的计数

javascript - 延迟加载不会在 Angular 中创建 chunk.js

jquery - 在延迟加载期间使用 JQuery 滚动事件处理程序时如何防止多次 AJAX 调用?

dynamic - Primefaces TabView 不维护 selectOneMenu 值

javascript - jQuery 在表循环中获取输入值

javascript - JQuery - 使 .slideToggle 特定于每个列表

javascript - setTimeout 和 setInterval 不起作用

javascript - 如何使jQuery每个循环重复无限次

jquery - 按需加载CKeditor和Jquery

java - Hibernate:org.hibernate.LazyInitializationException:无法初始化代理 - 无 session