简体   繁体   English

在现有网站中实施Infinite Scroll

[英]Implementing Infinite Scroll in existing website

We are trying to implement infinite scroll in our existing website. 我们正在尝试在现有网站中实现无限滚动。 Currently we have a image 'click for more' and it makes a ajax call to a method GetArticlesFromNextSection(true) which returns data which we append in #sectionArticles. 当前,我们有一个图像“单击更多”,它对方法GetArticlesFromNextSection(true)进行ajax调用,该方法返回我们附加在#sectionArticles中的数据。 And this works perfect. 这很完美。

Now we are trying to make it automatic as when user reaches at the end it should make a call to GetArticlesFromNextSection(true) method to load next chunk. 现在,我们正在尝试使其自动运行,因为当用户到达末尾时,应该调用GetArticlesFromNextSection(true)方法来加载下一个块。 Here is what we are using: 这是我们正在使用的:

<script type="text/javascript">
    $(window).scroll(function () {
        if ($(window).scrollTop() == $(document).height() - $(window).height()) {
            $('#lnkShowMore1').trigger('click'); //click image or
            //GetArticlesFromNextSection(true);
        }
    });
</script>

The problem with above code is, it makes continuous call to method GetArticlesFromNextSection(true) until no data left to load from database. 上面代码的问题是,它会连续调用方法GetArticlesFromNextSection(true),直到没有数据要从数据库加载为止。 It should make a single call to method GetArticlesFromNextSection(true) and stop and when user tries to scroll again, it should next chunk. 它应该对方法GetArticlesFromNextSection(true)进行一次调用,然后停止,并且当用户尝试再次滚动时,它应该下一个块。

How to make this possible? 如何做到这一点?

EDIT 编辑

I used flag but it loads just one time and never again. 我使用了flag,但是它仅加载一次,并且永远不会加载。 This is not infinite loop, it should load another chunk again when user reaches end. 这不是无限循环,当用户到达终点时,它应该再次加载另一个块。 This is what I used: 这是我使用的:

<script type="text/javascript">
    var is_loading = false;
    $(window).scroll(function() {
        if($(window).scrollTop() + $(window).height() >= $(document).height() -300) {
            if (is_loading) {
                return;
            }
            is_loading = true;
            //$('div#more').show();
            $('#lnkShowMore1').trigger('click'); //click image or
            //GetArticlesFromNextSection(true);
            //$('div#more').hide();
        }
    });
</script>

Take a look at the answers for How to rate-limit ajax requests? 看一下如何对ajax请求进行速率限制的答案

You can either include and use Underscore's _.throttle() in your script, or take a look at how they implement it and adapt into your code instead of including the whole library for a single purpose. 您可以在脚本中包含并使用Underscore的_.throttle(),也可以看看它们如何实现它并适应您的代码,而不是仅出于单个目的而包括整个库。

EDIT: 编辑:

Using Underscore, your code might look like this: 使用下划线,您的代码可能如下所示:

<script type="text/javascript">

$(window).scroll(_.throttle(function () {
    if ($(window).scrollTop() == $(document).height() - $(window).height()) {
        $('#lnkShowMore1').trigger('click'); 
    }
}, 1000));

... which should rate-limit the requests to once every second. ...应该将请求速率限制为每秒一次。

You can set a flag that check if the content is loaded. 您可以设置一个标志来检查内容是否已加载。

<script type="text/javascript">
$(window).scroll(function () {
    if (!isLoading && $(window).scrollTop() == $(document).height() - $(window).height()){
    isLoading = true;    
    $('#lnkShowMore1').trigger('click'); //click image or
        //GetArticlesFromNextSection(true);
    }
});
</script>

And inside Ajax callback sets 在Ajax回调集内

isLoading = false;

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

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