Infinite Scroll Without External Content

前端 未结 1 920
萌比男神i
萌比男神i 2021-01-27 16:26

I am creating a blog in which I want more posts to load when the user scrolls to the bottom of the page. However, I do not want to use a jQuery plugin that pulls the content fro

相关标签:
1条回答
  • 2021-01-27 17:11

    Try this (pattern)

    $(function () {
        $("body").css("height", $(document).height() + 1)
            .find("article").each(function (i, el) {
            $(el).not(":nth-last-of-type(n+4)").hide()
                .filter(":nth-of-type(4)").on("click.y", function () {
                $("body").animate({
                    scrollTop: "0px"
                }, 1000)
            });
        });
        $(document).on("scroll", {
            "scrolled": false
        }, function (e) {
            if (!e.data.scrolled) {
                var el = $("article:nth-of-type(n+4)");
                $(el).show(1000);
                e.data.scrolled = true;
            };
            if (e.data.scrolled) {
                $(el).on("click", function (e) {
                    $(e.target).hide(1000);
                });
            };
            if ($("article:nth-of-type(n+4)").css("display") === "none") {
                e.data.scrolled = false;
            };
            return false
        });
    });
    

    jsfiddle http://jsfiddle.net/guest271314/vTKpP/

    0 讨论(0)
提交回复
热议问题