![](/img/trans.png)
[英]How to prevent Infinite Scroll from loading pages when the tab is not active?
[英]how to specify when there are no pages left in an infinite scroll with kaminari
嗨,無限滾動功能存在問題,當所有內容都顯示完后,它仍會繼續滾動(奇怪的行為)。 我正在尋找一種在顯示所有內容時停止無限滾動的方法...但是首先我需要知道何時沒有更多的頁面/內容了
這是我的代碼
<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;
}
});
}
});
}());
我嘗試了這個
<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();
});
}
});
}());
我添加了.error函數,但仍然無法正常工作:)
當您從$.ajax
調用中獲得結果時,請檢查是否少於預期的記錄數(例如,您希望每頁20條記錄,例如,您有17條記錄),那么您位於結果集的末尾,並且您應該取消設置$(window).scroll
事件,或者設置一個導致事件處理程序短路的標志。
怎么樣,
$.ajax({
//calls
})
.fail(function(jqXHR, textStatus, errorThrown) {
//Finish scrolling.
finish();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.