-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.scrollmore.js
63 lines (58 loc) · 1.8 KB
/
jquery.scrollmore.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
( function() {
window.ScrollMore = window.ScrollMore || {
// Basic variables
opts : {
nextUrl : "",
distanceToBottom : 100,
},
// Use internally, to prevent next URL is loaded multiple times
_blockUrls : {},
// Check if user scroll near bottom
// Taken from http://stackoverflow.com/questions/3898130/how-to-check-if-a-user-has-scrolled-to-the-bottom
isNearbottom : function() {
if ($(window).scrollTop() + $(window).height() > $(document).height() - this.opts.distanceToBottom) {
return true;
}
return false;
},
// Do AJAX GET for the nextUrl, with callback before, success and error
// Simply jQuery Ajax here
_get : function(before, success, error) {
var $this = this;
$.ajax({
type : "GET",
url : $this.opts.nextUrl,
beforeSend : function(xhr) {
before(xhr);
}
}).done(function(data) {
success(data);
}).fail(function(jqXHR, textStatus) {
error(jqXHR, textStatus);
});
},
// Initialize the scroll with callback functions
// Adopt pattern from http://ejohn.org/blog/learning-from-twitter/ to prevent slow performance with scroll event
init : function(before, success, error) {
var didScroll = false;
$(window).scroll(function() {
didScroll = true;
});
var $this = this;
setInterval(function() {
if (didScroll) {
didScroll = false;
if ($this.isNearbottom()) {
if ($this.opts.nextUrl !== "" && $this.opts.nextUrl !== undefined) {
// If the next url is already loaded, it will not be loaded when user continues scrolling
if (($this.opts.nextUrl in $this._blockUrls) == false) {
$this._get(before, success, error);
$this._blockUrls[$this.opts.nextUrl] = true;
}
}
}
}
}, 250);
}
};
}());