【发布时间】:2014-05-09 12:38:31
【问题描述】:
我编写了一个分页评论部分, 即使你从未使用过 symfony2,它也很容易理解
它只是循环通过 100 个 cmets,然后使用“下一步”按钮显示下一个评论页面。
{% for comment in pagination %}
<div class="comment-container">
<div class="bubble">
<div class="cmt-avatar"></div>
{{ comment.text }}
</div>
</div>
{% endfor %}
到目前为止,此方法运行良好。 但这让设计看起来很糟糕,特别是页面右侧有一个巨大的滚动条。 我需要一种方法让它看起来像评论容器在用户滚动时正在加载。它显然只是一个静态页面,它只需要看起来像无限滚动。
到目前为止我所做的尝试:
我在这个页面上找到了这个,但它不起作用,没有错误,但什么也没做。
{% for comment in pagination %}
<div class="scrollable-data'">
<div class="comment-container">
<div class="bubble">
<div class="cmt-avatar"></div>
{{ comment.text }}
</div>
</div>
</div>
{% endfor %}
...脚本
var $doc=$(document);
var $win=$(window);
// hide everything that is out of bound
$('.scrollable-data').filter(function(index){
return ($(this).scrollTop() > $doc.height());
}).hide();
var DATA_INCREMENT=5;
$(window).scroll(function(){
// test if at the bottom
if ($doc.height()-$win.height()-$(this).scrollTop() == 0) {
// show the <DATA_INCREMENT> (5) next hidden data tags
$('.scrollable-data:hidden:lt('+DATA_INCREMENT+')').show();
}
});
【问题讨论】:
-
为了清楚起见,在您的脚本中,在滚动功能中,您使用
$(window)、$win和$(this),这三个含义相同。为了更好地阅读,您应该只使用一个。 -
您可能必须将
<div class=".scrollable-data'">替换为<div class="scrollable-data">(没有点也没有顶点) -
哎呀,打错字了,原来项目上其实叫 class="scrollable-data"
-
我从来没有这样做过,但我不确定你的情况
if ($doc.height()-$win.height()-$(this).scrollTop() == 0)
标签: javascript jquery html symfony infinite-scroll