forked from anties/jquery.scrollLoading
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.scrollLoading.src.js
87 lines (73 loc) · 2.68 KB
/
jquery.scrollLoading.src.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
;(function($){
var $scrollLoading;
$.fn.scrollLoading = function(settings){
return this.each(function(){
$scrollLoading.init($(this), settings);
});
};
$scrollLoading = $.scrollLoading = {
more: true,
init: function(outer, settings) {
// objects
this.outer = outer;
this.appendTo = settings.appendTo;
this.judgeBy = settings.appendTo || this.appendTo;
this.ratio = settings.ratio || 0.9;
// ajax settings
this.ajaxData = settings.ajaxData || {};
// overwrite datType
if ('undefined' === typeof this.ajaxData.dataType || !$.inArray(this.ajaxData.dataType, ('html', 'json'))) {
this.ajaxData.dataType = 'html';
}
// overwrite success callback function
this.tmp_success = this.ajaxData.success;
this.ajaxData.success = function(ret) {
sl = $scrollLoading;
sl.resultHandler(ret);
// 若需要有更新 data 的動作要寫在 success 裡
if ('function' === typeof sl.tmp_success) {
sl.tmp_success(ret);
}
// 檢查版面高度夠不夠讓捲軸出現
if (sl.more) {
sl.checkGetMore();
}
};
this.outer.scroll(function(){
var sl = $scrollLoading;
var scrollBottom = sl.outer.scrollTop() + $(window).height();
if ((true === sl.more) && (scrollBottom / sl.judgeBy.height() >= sl.ratio)) {
sl.more = false;
sl.sendRequest();
}
});
// 檢查版面高度夠不夠讓捲軸出現
this.checkGetMore();
},
sendRequest: function() {
$.ajax(this.ajaxData);
},
checkGetMore: function() {
if (this.judgeBy.height() < this.outer.height() * 2) {
this.more = false;
this.sendRequest();
}
},
resultHandler: function(ret) {
var content = '';
if ('html' === this.ajaxData.dataType) {
content = ret;
} else if ('json' === this.ajaxData.dataType) {
if (true === ret.error) {
content = '';
} else if ('undefined' !== typeof ret.content) {
content = ret.content;
}
}
if ('' !== content) {
this.appendTo.append(content);
this.more = true;
}
}
};
})(jQuery);