我有一个脚本可以在用户滚动到底部时加载新产品。该脚本有效,但我有两个问题。那是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>