-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinfinite-scroll.js
41 lines (34 loc) · 1.1 KB
/
infinite-scroll.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
$(document).ready(function() {
$("body").append('<div id="loader">loading...</div>');
function isLoaderOnScreen() {
return $(window).scrollTop() + window.innerHeight + 500 > $("#loader").offset().top
}
function loadNext() {
var nextPageHolderId = "page-" + Date.now(),
moreButton = $("a:contains('More'):last"),
nextPageURL = moreButton.attr('href');
// validate moreButton
if(moreButton.text() != "More") {
return false;
}
moreButton.parents('table').parents('table').children('tbody').children('tr:last').remove();
moreButton.closest('tr').remove();
$("#loader").before('<div id="' + nextPageHolderId + '" style="margin-top:-20px"></div>');
$("#" + nextPageHolderId).load(nextPageURL, function() {
$("#" + nextPageHolderId).find('tr:first').remove();
});
if(isLoaderOnScreen()) {
loadNext();
}
}
function load() {
if(isLoaderOnScreen()) {
loadNext();
}
}
$(document).scroll(function() {
clearTimeout(window.scrollTimeout);
window.scrollTimeout = setTimeout(load, 500);
});
load();
});