1

ユーザーが一番下までスクロールしたときに新製品をロードするスクリプトがあります。スクリプトは機能しますが、2つの問題があります。つまり、a)スクリプトはpage1を2回ロードしますb)スクリプトは一度に1ページではなく、すべてのページを一度にロードします

誰かが解決策を知っていますか?私は知識が限られているので、どんな助けでも大歓迎です。

私のスクリプト

<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>
4

2 に答える 2

2

問題A

関数を実行した、ページ番号を増やしています。loadProducts

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

これを試してみてください:

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

問題B

実行するコードは、ユーザーが一番下に到達したときに複数回実行されてwindow.scrollいる可能性があります。つまり、すべてのページがすぐに読み込まれます。これを回避する1つの方法は、オーバーレイが表示されていない場合にのみコードを実行することです。

// 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の場合にのみスクロールコードの実行を許可します。

于 2012-06-25T02:23:20.963 に答える
0

これは、スクリプトが2回起動しているために発生しています。つまり、ajax呼び出しは複数回実行する必要があります。HttpFoxを使用すると、これを監視できます。仮想スクロールを実装したときに同じ問題に直面します。

これを監視するには、ある種のフラグを使用します。以下のURL/問題を確認してください。これには、この問題を回避した仮想スクロールのスクリプトが含まれています。 https://stackoverflow.com/questions/19049088/page-clicks-not-working-during-virtual-scroll

于 2013-11-14T09:40:05.240 に答える