【问题标题】:load more on scroll page doesn't load page 2在滚动页面上加载更多不加载第 2 页
【发布时间】:2012-06-24 22:35:47
【问题描述】:

我仍然坚持这个问题,很抱歉这个重叠的问题。 我正在尝试创建一个从第一页加载产品的产品页面。当用户从第二页向下滚动产品时,需要加载。我搜索了这个问答网站和许多教程,但我无法让它工作。

shop.com/category/page1.ajax 中的产品已正确加载,但是当我向下滚动时,我不知道为什么 shop.com/category/page2.ajax 等中的产品未加载。

不能选择无限滚动之类的插件。

非常感谢任何帮助。

JSON/Ajax 字符串(因长度而缩小):

{
"page": 1,
"pages": 7,
"count": 42,
"url": "http:\/\/meules1.webshopapp.com\/woonkamer-en-keuken\/",
"products": [
    {
        "id": 1750137,
        "vid": 2765003,
        "image": "http:\/\/cdn.webshopapp.com\/i\/z1faly\/50x50x2\/newc.jpg",
        "image_id": 2156769,
        "brand": false,
        "code": "40-95407",
        "ean": "40-95407",
        "sku": "",
        "score": false,
        "price": {
            "price": 65,
            "price_money": "\u20ac65,00",
            "price_money_with_currency": "\u20ac65,00 EUR",
            "price_money_without_currency": "65,00",
            "price_excl": 54.6218,
            "price_excl_money": "\u20ac54,62",
            "price_excl_money_with_currency": "\u20ac54,62 EUR",
            "price_excl_money_without_currency": "54,62",
            "price_incl": 65,
            "price_incl_money": "\u20ac65,00",
            "price_incl_money_with_currency": "\u20ac65,00 EUR",
            "price_incl_money_without_currency": "65,00",
            "price_old": 109,
            "price_old_money": "\u20ac109,00",
            "price_old_money_with_currency": "\u20ac109,00 EUR",
            "price_old_money_without_currency": "109,00",
            "price_old_excl": 91.5966,
            "price_old_excl_money": "\u20ac91,60",
            "price_old_excl_money_with_currency": "\u20ac91,60 EUR",
            "price_old_excl_money_without_currency": "91,60",
            "price_old_incl": 109,
            "price_old_incl_money": "\u20ac109,00",
            "price_old_incl_money_with_currency": "\u20ac109,00 EUR",
            "price_old_incl_money_without_currency": "109,00"
        },
        "available": true,
        "url": "http:\/\/meules1.webshopapp.com\/newc.html",
        "title": "Newc",
        "fulltitle": "Newc",
        "variant": "",
        "description": "In de serie Newcastle bieden wij nu ook dit TV meubel \/ salontafel aan. Deze",
        "data_01": ""
    },
    {
        "id": 1750135,
        "vid": 2765001,
        "image": "http:\/\/cdn.webshopapp.com\/i\/z1falp\/50x50x2\/utrecht.jpg",
        "image_id": 2156760,
        "brand": false,
        "code": "nov-89",
        "ean": "nov-89",
        "sku": "",
        "score": false,
        "price": {
            "price": 47.5,
            "price_money": "\u20ac47,50",
            "price_money_with_currency": "\u20ac47,50 EUR",
            "price_money_without_currency": "47,50",
            "price_excl": 39.916,
            "price_excl_money": "\u20ac39,92",
            "price_excl_money_with_currency": "\u20ac39,92 EUR",
            "price_excl_money_without_currency": "39,92",
            "price_incl": 47.5,
            "price_incl_money": "\u20ac47,50",
            "price_incl_money_with_currency": "\u20ac47,50 EUR",
            "price_incl_money_without_currency": "47,50",
            "price_old": 65,
            "price_old_money": "\u20ac65,00",
            "price_old_money_with_currency": "\u20ac65,00 EUR",
            "price_old_money_without_currency": "65,00",
            "price_old_excl": 54.6218,
            "price_old_excl_money": "\u20ac54,62",
            "price_old_excl_money_with_currency": "\u20ac54,62 }

jquery:

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

// var pageSize = {{ collection.limit }};
var currentPage = {{ collection.page }};
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"));
};

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

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

$.getJSON(url,function(data) {

  console.log(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://shop.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
 $(".productsGrid").scroll(function() {
// We check if we're at the bottom of the scrollcontainer
if ($(this)[0].scrollHeight - $(this).scrollTop() == $(this).outerHeight()) {
  // If we're at the bottom, show the overlay and retrieve the next page

    currentPage++;

  $("#overlay").fadeIn();
  loadProducts();
}
});

});
</script>

【问题讨论】:

    标签: jquery ajax json load infinite-scroll


    【解决方案1】:

    也许scroll 函数没有针对该选定元素productsGrid 触发。你会用警报检查吗? productsGrid 有滚动条还是只是整个页面的一部分?您可以对整个文档尝试更通用的方法:

    getJSON

     // We're done loading the products, so hide the overlay and update the UI
          $("#overlay").fadeOut();
          $(window).scroll(function() { update(); });  // <-- ADD THIS LINE
        });
      };
    
    // First time, directly load the products
    loadProducts();
    
    var update = function() {  // <-- NAME THIS AS FUNCTION
      // activate loadProducts when scrollbar reaches 150 pixels or less from the
      // bottom, instead of having scrolled exactly all the way to the end.
      if($(window).height() + $(window).scrollTop() >= $(document).height() - 150) {
    
        if(currentPage < collectionPages) {
          // If we're at the bottom, show the overlay and retrieve the next page    
          currentPage++;
          $("#overlay").fadeIn();
          $(window).unbind('scroll');  // <-- ADD THIS LINE
          loadProducts();
        }
      }
    };
    
    $(window).scroll(function() { update(); } ); // <-- ADD THIS LINE
    

    【讨论】:

    • 谢谢我完全错过了那个......呃!产品现在一直在加载。您知道在到达最后一个产品时停止的方法吗?
    • 您的 JSON 显示属性“pages”,您可以在 if 语句中使用它来防止加载吗?我不太确定您的应用程序是如何工作的。
    • 我的意思是我不能 100% 确定如何决定何时停止加载新页面。有来自 JSON 的“pages”变量和变量collectionPages。我不知道他们是否有关系。如果它们相同,您可以写类似if(currentPage &lt;= collectionPages) { $("#overlay").fadeIn(); loadProducts(); currentPage++; }
    • 对不起那个错误,我没有真正正确地测试代码。请参阅上面的更新答案。我做了更多的研究,一个解决方案说使用unbind。我认为,代码看起来有点难看,但似乎可以工作。我认为正在发生的事情是当添加新内容时,页面会自动滚动并触发新的 AJAX 调用,而无需用户移动页面。所以我在AJAX调用之前写了unbind,添加完所有元素后重新绑定滚动。
    • 哦,第 1 页加载了两次,因为当我将代码复制到 cmets 时,我是手动完成的,并且我移动了命令的顺序。 currentPage++ 必须在 loadProducts 之前。 :(
    猜你喜欢
    • 1970-01-01
    • 2013-08-16
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    相关资源
    最近更新 更多