-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.scrollLoading.js
84 lines (74 loc) · 1.82 KB
/
jquery.scrollLoading.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
/*!
* jquery.scrollLoading.js
*/
(function($) {
$.fn.scrollLoading = function(options) {
var defaults = {
attr: "data-url",
container: $(window),
callback: $.noop,
preloadHeight:0
};
var params = $.extend({}, defaults, options || {});
params.cache = [];
$(this).each(function() {
var node = this.nodeName.toLowerCase(), url = $(this).attr(params["attr"]);
//重组
var data = {
obj: $(this),
tag: node,
url: url
};
params.cache.push(data);
});
var callback = function(call) {
if ($.isFunction(params.callback)) {
params.callback.call(call.get(0));
}
};
//动态显示数据
var loading = function() {
var preloadHeight = params.preloadHeight ;
var contHeight = params.container.height();
if ($(window).get(0) === window) {
contop = $(window).scrollTop();
} else {
contop = params.container.offset().top;
}
var flag = true ;
$.each(params.cache, function(i, data) {
var o = data.obj, tag = data.tag, url = data.url, post, posb;
if (o) {
post = o.offset().top - contop, post + o.height();
if (o.is(':visible') && (post >= 0 && post-preloadHeight < contHeight) || (posb > 0 && posb <= contHeight)) {
if (url) {
//在浏览器窗口内
if (tag === "img") {
//图片,改变src
callback(o.attr("src", url));
} else {
o.load(url, {}, function() {
callback(o);
});
}
} else {
// 无地址,直接触发回调
callback(o);
}
data.obj = null;
}else{
flag=false;
}
}
});
if(flag){
params.container.unbind("scroll", loading);
}
};
//事件触发
//加载完毕即执行
loading();
//滚动执行
params.container.bind("scroll", loading);
};
})(jQuery);