forked from luis-almeida/unveil
-
Notifications
You must be signed in to change notification settings - Fork 1
/
expose.js
70 lines (66 loc) · 2.17 KB
/
expose.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
/**
* React expose
* A very lightweight React mixin to lazy load images
* http://dok.github.com/expose
*
* Licensed under the MIT license.
* Copyright 2015 Sean Dokko
* https://github.com/dok
*/
(function() {
var root = this;
var expose = {
_reveal: function() {
var called = false;
return function(cb) {
if(!called) {
called = true;
var img = React.findDOMNode(this.refs.image);
if (img) {
var retina = window.devicePixelRatio > 1;
var attrib = retina ? "data-src-retina" : "data-src";
var source = img.getAttribute(attrib);
source = source || img.getAttribute("data-src");
if (source) {
img.setAttribute("src", source);
if(typeof cb === 'function') {
cb.call(this);
}
}
}
}
};
},
expose: function(th, cb) {
th = th || 0;
var self = this;
var img = React.findDOMNode(this.refs.image);
if (img) {
var rev = self._reveal();
window.addEventListener('scroll', function() {
var wt = document.body.scrollTop,
wb = wt + window.innerHeight,
et = img.offsetTop;
eb = et + img.clientHeight;
var shouldLoad = eb >= wt - th && et <= wb + th;
if(shouldLoad) {
rev.call(self, cb);
}
});
}
}
};
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = expose;
}
exports.expose = expose;
} else {
root.expose = expose;
}
if (typeof define === 'function' && define.amd) {
define('expose', [], function() {
return expose;
});
}
})();