简体   繁体   English

如何在Rails中实现无限滚动

[英]How to implement Infinite Scrolling in Rails

Hi I am working on a ROR project with ruby-2.2.2 and rails 4. i have implemented an infinite scroll in my webpage to a specific area(div specific) and its working fine, but whenever a horizontal scrolling is aded to that div either by increasing the width of the table or by increasing the number of "td" inside the table, it sends multiple request for the same page on a single scroll. 嗨我正在使用ruby-2.2.2和rails 4进行ROR项目。我在我的网页中实现了一个无限滚动到特定区域(特定于div)并且它工作正常,但每当水平滚动与该div相关时通过增加表的宽度或增加表中“td”的数量,它会在单个滚动上发送对同一页面的多个请求。

html page:
<div class="ibox-content" style="height: 450px;overflow-y: scroll;" id="user_mangmnet_scroll_div">
  <table class="table table-hover">
    <thead>
    <tr>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Address</th>
      <th>Zipcode</th>
      <th>Phone Number</th>
      <th>Email</th>
      <th>User Type</th>
      <th>Actions</th>
    </tr>
    </thead>
    <tbody id="user_management_container">
      <%= render 'dashboard/dashboards/admin/user_managment.html.erb' %>
    </tbody>
  </table>
</div>
<div id="user-pagination-container">
  <%= will_paginate @user_registered %>
</div>

jQuery: jQuery的:

$(document).ready(function() {
    if ($('#user-pagination-container .pagination').length) {
      $('#user_mangmnet_scroll_div').scroll(function() {
        var url = $('#user-pagination-container .pagination .next_page').attr('href');
        if(url && ($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight)) {
          return $.getScript(url);
        }

      });
      return $('#user_mangmnet_scroll_div').scroll();
    }
  });

The problem is whenever i added a horizontal scrolling to the div it sends multiple request for the same page on a single scroll. 问题是每当我向div添加水平滚动时它会在单个滚动上发送多个同一页面的请求。

Please help Thanks in advance :) 请帮助提前谢谢:)

I think you need to include this answer that I found for you here: Is there a vertical scroll event in jQuery 我想你需要在这里找到我找到的答案: jQuery中是否存在垂直滚动事件

You check if scroll was vertical and only then you call action. 您检查滚动是否是垂直的,然后才调用动作。

copy - paste 复制粘贴

var prevTop = 0;
$(document).scroll( function(evt) {
    var currentTop = $(this).scrollTop();
    if(prevTop !== currentTop) {
        prevTop = currentTop;
        console.log("I scrolled vertically.");
    }
});

and make your code something like that: 并使你的代码像这样:

$(document).ready(function() {
    var prevTop = 0;
    var currentTop = 0;
    if ($('#user-pagination-container .pagination').length) {
      $('#user_mangmnet_scroll_div').scroll(function() {

        currentTop = $(this).scrollTop();
        if(prevTop !== currentTop) {
           prevTop = currentTop
        var url = $('#user-pagination-container .pagination .next_page').attr('href');
        if(url && ($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight)) {
          return $.getScript(url);
        }

        } // end of checking if prevTop!==currentTop
      });
      return $('#user_mangmnet_scroll_div').scroll();
    }
  });

I didnt check if it works. 我没有检查它是否有效。 I dont know also why do you return 我也不知道为什么你会回来

$('#user_mangmnet_scroll_div').scroll(); in if statement...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM