【发布时间】:2014-06-27 22:49:03
【问题描述】:
我不确定我在这里缺少什么。我已经在页面上实现了无限滚动,但结果被重复了不止一次。我为我的开发环境创建了一个项目列表,这些项目是按顺序制作的。它们在无限卷轴中被重复,例如。项目编号 49、48、47、46、45、44、46、45、44、46、45、44、43,......
index.js.erb
$('#my-posts').append('<%= j render @news %>');
<% if @news.next_page %>
$('.pagination').replaceWith('<%= j will_paginate @news %>');
<% else %>
$(window).off('scroll');
$('.pagination').remove();
<% end %>
index.html.erb
<section class='page-content-container'>
<h2 class='static-page-subheading'>Stay Up to Date</h2>
<%= render 'news' %>
<div id="infinite-scrolling">
<%= will_paginate @news%>
</div>
</section>
_news.html.erb
<div id='my-posts'>
<% @news.each do |news| %>
<div class='news-item-container'>
<h2><%= news.title %></h2>
<h3><%= news.created_at %></h3>
<%= truncate(news.body, :length => 500, escape: false) %>
<%= link_to 'Read More', news %>
<div class="addthis_sharing_toolbox"></div>
</div>
<% end %>
</div>
pagination.js.coffee (此处忽略缩进)
jQuery ->
if $('#infinite-scrolling').size() > 0
$(window).on 'scroll', ->
more_news_url = $('.pagination .next_page a').attr('href')
if more_news_url && $(window).scrollTop() > $(document).height() - $(window).height() - 60
$('.pagination').html('<img src="/assets/ajax-loader.gif" alt="Loading..." title="Loading..." />')
$.getScript more_news_url
return
return
【问题讨论】:
-
不,它只是堆栈溢出。稍等,我会编辑
-
我可以看看你的控制器吗?
标签: jquery ruby-on-rails coffeescript infinite-scroll