【问题标题】:Automatically scrolls up after loading more items (Endless scrolling)加载更多项目后自动向上滚动(无限滚动)
【发布时间】:2019-12-23 17:14:55
【问题描述】:

我正在尝试在 RoR 中实现无限滚动,但在加载更多项目后,会自动滚动到页面顶部。我想保持相同的位置。会发生什么?

控制器:

def index
  @works = Work.paginate(page: params[:page], per_page: 15).order('created_at DESC')
  respond_to do |format|
    format.html
    format.js
  end
end

查看 index.js.erb:

$("#works").append('<%= j render("work_page_header") %>');
$("#works").append('<%= j render(@works) %>');

<% if @works.next_page %>
  $('.pagination').replaceWith('<%= j will_paginate(@works) %>');
<% else %>
  $('.pagination').remove();
<% end %>

endless_scrolling.js:

$(document).on('turbolinks:load', function() { 
  if( $('.pagination').length && $('#works-list').length)

  $(window).on('scroll', function(e){
    var url = $('.pagination .page-link[rel="next"]').attr('href');

    if(url && $(window).scrollTop() > ($(document).height() - $(window).height() - 50)) {
      $('.pagination').text('Loading works...');
      $.getScript(url);
      console.log('paginate...', url);
      $(window).scroll();
    }
  });
});

【问题讨论】:

    标签: javascript ruby-on-rails ruby


    【解决方案1】:

    这个函数代替了:$(window).scroll() 我把

      $('html, body').animate({ scrollTop: $(document).height() - 
    $(window).height() }, 1000, function() {
        $(this).animate({ scrollBottom: 0 }, 1000);
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-31
      • 1970-01-01
      • 1970-01-01
      • 2017-05-06
      • 1970-01-01
      • 1970-01-01
      • 2016-04-02
      相关资源
      最近更新 更多