Javascript 如何使用kaminari指定无限卷轴中何时没有页面
您好,我有一个无限滚动功能的问题,当所有的内容已经显示,它仍然继续滚动(奇怪的行为)。我正在寻找一种方法来停止无限滚动时,所有的内容显示…但首先我需要知道什么时候没有更多的页面/内容了 这是我的密码Javascript 如何使用kaminari指定无限卷轴中何时没有页面,javascript,jquery,ruby-on-rails-3.2,infinite-scroll,Javascript,Jquery,Ruby On Rails 3.2,Infinite Scroll,您好,我有一个无限滚动功能的问题,当所有的内容已经显示,它仍然继续滚动(奇怪的行为)。我正在寻找一种方法来停止无限滚动时,所有的内容显示…但首先我需要知道什么时候没有更多的页面/内容了 这是我的密码 <script type="text/javascript"> jQuery(document).ready(function ($) { (function () { var page = 1, loading = false, finish
<script type="text/javascript">
jQuery(document).ready(function ($) {
(function () {
var page = 1,
loading = false,
finish = false;
function nearBottomOfPage() {
return $(window).scrollTop() > $(document).height() - $(window).height() - 200;
}
function finish() {
finish = true;
}
$(window).scroll(function () {
if (loading) {
return;
}
if (nearBottomOfPage() && !finish) {
loading = true;
$('#loader').show();
page++;
$.ajax({
url: '/office?page=' + page,
type: 'get',
dataType: 'script',
success: function () {
$('#loader').hide();
loading = false;
}
});
}
});
}());
jQuery(文档).ready(函数($){
(功能(){
变量页=1,
加载=错误,
完成=错误;
函数nearBottomOfPage(){
返回$(窗口).scrollTop()>$(文档).height()-$(窗口).height()-200;
}
函数finish(){
完成=真;
}
$(窗口)。滚动(函数(){
如果(装载){
返回;
}
如果(接近页面底部()&&!完成){
加载=真;
$(“#加载程序”).show();
page++;
$.ajax({
url:'/office?第='+页,
键入:“get”,
数据类型:“脚本”,
成功:函数(){
$(“#加载程序”).hide();
加载=假;
}
});
}
});
}());
我试过这个
<script type="text/javascript">
jQuery(document).ready(function ($) {
(function () {
var page = 1,
loading = false,
finish = false;
function nearBottomOfPage() {
return $(window).scrollTop() > $(document).height() - $(window).height() - 200;
}
function finish() {
finish = true;
}
$(window).scroll(function () {
if (loading) {
return;
}
if (nearBottomOfPage() && !finish) {
loading = true;
$('#loader').show();
page++;
$.ajax({
url: '/office?page=' + page,
type: 'get',
dataType: 'script',
success: function () {
$('#loader').hide();
loading = false;
}
});
$.error(function(jqXHR, textStatus, errorThrown) {
//Finish scrolling.
finish();
});
}
});
}());
jQuery(文档).ready(函数($){
(功能(){
变量页=1,
加载=错误,
完成=错误;
函数nearBottomOfPage(){
返回$(窗口).scrollTop()>$(文档).height()-$(窗口).height()-200;
}
函数finish(){
完成=真;
}
$(窗口)。滚动(函数(){
如果(装载){
返回;
}
如果(接近页面底部()&&!完成){
加载=真;
$(“#加载程序”).show();
page++;
$.ajax({
url:'/office?第='+页,
键入:“get”,
数据类型:“脚本”,
成功:函数(){
$(“#加载程序”).hide();
加载=假;
}
});
$.error(函数(jqXHR、textStatus、errorshown){
//完成滚动。
完成();
});
}
});
}());
我添加了.error函数,但它仍然不起作用:)当您从
$调用中得到一个结果时。ajax
检查您得到的记录是否少于预期数量(例如,您预期每页20条,您得到17条),然后您就到了结果集的末尾,您应该取消设置$(窗口)。滚动
event,或者您应该设置一个导致事件处理程序短路的标志。
$.ajax({
//calls
})
.fail(function(jqXHR, textStatus, errorThrown) {
//Finish scrolling.
finish();
});
如何使用jquery获取从每个get方法获取的记录数:(您正在返回一种类型的“脚本”,因此在生成该脚本服务器端时,您应该知道您有多少条记录。如果记录少于预期值,则在返回的脚本中包含代码以解除绑定处理程序。由于某些原因,不会触发fail/error方法…有任何建议吗