-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.scrollLoad.js
66 lines (62 loc) · 2.84 KB
/
jquery.scrollLoad.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
/**
* Created by Falost on 2017/3/29.
* Name:jQuery.scrollLoad.js
* 滚动响应加载页面图片
*/
(function ($) {
$.fn.scrollLoad= function (options) {
var defaults = {
src:'data-src',
time:300,
t: this //默认参数请勿修改,除非你已知他的用处
};
var ops = $.extend(defaults, options);
var _fn = {
_init : function(){
var t = ops.t;
//console.log(t)
if(t == null)return false;
var offsetPage = window.pageYOffset ? window.pageYOffset : window.document.documentElement.scrollTop,
offsetWindow = offsetPage + Number(window.innerHeight ? window.innerHeight : document.documentElement.clientHeight),
docImg = t.find("img"), //获取页面图片列表
_len = docImg.length;
if (!_len) return false;
for (var i = 0; i < _len; i++) {
var attrSrc = docImg[i].getAttribute(defaults.src),
o = docImg[i], tag = o.nodeName.toLowerCase();
if (o) {
postPage = o.getBoundingClientRect().top + window.document.documentElement.scrollTop + window.document.body.scrollTop; postWindow = postPage + Number(_fn._getStyle(o, 'height').replace('px', ''));
if ((postPage > offsetPage && postPage < offsetWindow) || (postWindow > offsetPage && postWindow < offsetWindow)) {
if (tag === "img" && attrSrc !== null) {
o.setAttribute("src", attrSrc);
}
o = null;
}
}
}
},
_camelize : function (s) {
return s.replace(/-(\w)/g, function (strMatch, p1) {
return p1.toUpperCase();
});
},
_getStyle : function (element, property) {
if (arguments.length != 2) return false;
var value = element.style[_fn._camelize(property)];
if (!value) {
if (document.defaultView && document.defaultView.getComputedStyle) {
var css = document.defaultView.getComputedStyle(element, null);
value = css ? css.getPropertyValue(property) : null;
} else if (element.currentStyle) {
value = element.currentStyle[_fn._camelize(property)];
}
}
return value == 'auto' ? '' : value;
}
}
window.onscroll = function () {
setTimeout(function () {_fn._init()}, defaults.time);
}
return _fn._init();
};
})(jQuery);