【问题标题】:infinite scroll one page at a time一次无限滚动一页
【发布时间】:2012-06-25 00:51:50
【问题描述】:

我有一个脚本可以在用户滚动到底部时加载新产品。该脚本有效,但我有两个问题。那是 a) 脚本加载 page1 两次 b) 脚本一次加载所有页面,而不是一次加载一个

有人知道解决办法吗?我知识有限,非常感谢任何帮助。

我的脚本

<script type="text/javascript">
$(document).ready(function(){

  // var pageSize = {{ collection.limit }};
  var currentPage = 1;
  var collectionPages = {{ collection.pages }};
  var category = '{{ collection.internal.url }}';

  // Appends the new product to the UI
  var appendProduct = function(product, id) {

  $('<div class="product"></div>')
    .html(product)
    .appendTo($(".productsGrid"));

  var i = 1;
  $('.product').each(function() {
    if(i++ % 3 == 0)
      $(this).addClass('last');
   });
  };

  // Load the next products page
  var loadProducts = function() {

    var url = "http://shop.com/"+category+"/page"+currentPage+".ajax";

    $.getJSON(url,function(data) {

      $.each(data.products, function(index, product) {
        appendProduct('<a href="'+product.url+'" title="'+product.fulltitle+'">' +
                    '<img src="'+product.image+'" width="180" height="150" alt="'+product.fulltitle+'" title="'+product.fulltitle+'"'+'</a>' +
                    '<div class="info"><h3><a href="'+product.url+'" title="'+product.fulltitle+'">'+product.fulltitle+''+'</a></h3>' +
                    '<div class="price">'+product.price.price_money+''+'</div>' +
                    '<div class="gridAddToCart"><a class="button grey" href="'+product.url+'" title="'+product.fulltitle+'" rel="nofollow">'+'<span>{{ 'Details' | t }}</span></a>' +
                    '<div style="margin-top:2px;"></div>' +
                    '<a class="opener button blue" href="http://meules1.webshopapp.com/cart/add/'+product.vid+'" title="'+product.fulltitle+'" rel="nofollow"><span>{{ 'Add to cart' | t }}</span></a>'
                   + '<div class="clear"></div>' +
                    '</div><div class="clear"></div></div>'      
        );
      });

    // We're done loading the products, so hide the overlay and update the UI
    $("#overlay").fadeOut();
    });
  };

  // First time, directly load the products
  loadProducts();

  // Append a scroll event handler to the container
  $(window).scroll(function() {
    // activate new load when scrollbar reaches 150 pixels or less from the bottom
    if($(window).height() + $(window).scrollTop() >= $(document).height() - 350) {

      if(currentPage <= collectionPages) {
        $("#overlay").fadeIn();
        loadProducts();
        currentPage++;
      }

    }
  });

});
</script>

【问题讨论】:

    标签: jquery ajax json infinite-scroll


    【解决方案1】:

    问题 A

    您在运行loadProducts 函数后增加页码:

    if(currentPage <= collectionPages) {
      $("#overlay").fadeIn();
      loadProducts();
      currentPage++
    }
    

    你应该试试这个:

    if(currentPage <= collectionPages) {
      $("#overlay").fadeIn();
      currentPage++; //Moved this ABOVE loadProducts()
      loadProducts();
    }
    

    问题 B

    当用户到达底部时,您在window.scroll 处执行的代码可能会触发多次,这意味着所有页面都会立即加载。解决此问题的一种方法可能是仅在您的叠加层不可见时才运行代码:

    // Append a scroll event handler to the container
    $(window).scroll(function() {
      // activate new load when scrollbar reaches 150 pixels or less from the bottom
      if(($(window).height() + $(window).scrollTop() >= $(document).height() - 350) && $("#overlay").is(":hidden")) {
    

    或者,您可以设置一个全局变量,如var loading_page = false。开始加载页面时将此设置为 true,在新页面完全加载后将其切换回 false,并且仅当 loading_page 变量为 false 时才允许执行滚动代码。

    【讨论】:

      【解决方案2】:

      发生这种情况是因为脚本触发了两次,即您的 ajax 调用必须进行多次。检查 HttpFox,您将能够观察到这一点。当我实现虚拟滚动时,我面临同样的问题。

      使用某种标志来监控这一点。检查以下 URL/问题。这包含我避免此问题的虚拟滚动脚本。 https://stackoverflow.com/questions/19049088/page-clicks-not-working-during-virtual-scroll

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-27
        • 2020-12-05
        • 1970-01-01
        • 1970-01-01
        • 2016-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多