简体   繁体   English

如何使用Jquery / Ajax处理无限滚动的页码?

[英]How to handle page numbers for an infinite scroll with Jquery / Ajax?

This is my first time trying to implement an infinite scroll with JQuery / Ajax. 这是我第一次尝试使用JQuery / Ajax实现无限滚动。 This is where I am currently: 这是我目前所在的位置:

<script>
$(document).ready(function() {
    // see if we're at the bottom of the page to potentially load more content
    $(window).on('scroll', scrollProducts);

    function scrollProducts() {
        var end = $("#footer").offset().top;
        var viewEnd = $(window).scrollTop() + $(window).height();
        var distance = end - viewEnd;

        // when we're almost at the bottom
        if (distance < 300)  {
            // unbind to prevent excessive firing
            $(window).off('scroll', scrollProducts);
            console.log('we reached the bottom');

            $.ajax({
                type: 'GET',
                url: "foo/bar/2",
                success: function(data) {
                    console.log("success!");
                    $('#container').append(data).fadeIn();
                    // rebind after successful update
                    $(window).on('scroll', scrollProducts);
                }
            });
        }
    }
});
</script>

I'd like to understand the correct way to update the page number in the url: foo/bar/2 . 我想了解更新url中的页码的正确方法: foo/bar/2

I've read that due to the difference between synchronous and asynchronous calls you can't use a global variable but instead need a callback (although I'm failing to understand it). 我读过,由于同步和异步调用之间的差异,您不能使用全局变量,而需要回调(尽管我无法理解)。 I've also seen a solution where someone updated the values of hidden fields and then referenced those, although that seems like an ugly workaround. 我也看到了一个解决方案,其中有人更新了隐藏字段的值,然后引用了这些值,尽管这似乎是一个丑陋的解决方法。

What is the correct or recommended way to handle page numbers in this situation, so that the number increases with each request until there are no more pages? 在这种情况下,处理页码的正确或推荐方法是什么,以便该数目随每个请求而增加,直到没有更多页为止?

keep a counter and use it in you request 保留一个计数器并在您的请求中使用它

var page = 1;

$(document).ready(function() {
// see if we're at the bottom of the page to potentially load more content
$(window).on('scroll', scrollProducts);

function scrollProducts() {
    var end = $("#footer").offset().top;
    var viewEnd = $(window).scrollTop() + $(window).height();
    var distance = end - viewEnd;

    // when we're almost at the bottom
    if (distance < 300)  {
        // unbind to prevent excessive firing
        $(window).off('scroll', scrollProducts);
        console.log('we reached the bottom');

        $.ajax({
            type: 'GET',
            url: "foo/bar/" + page,
            success: function(data) {
                console.log("success!");
                $('#container').append(data).fadeIn();
                // rebind after successful update
                $(window).on('scroll', scrollProducts);
                page++;
            }
        });
    }
}
});

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

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