forked from luis-almeida/unveil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.unveil-E.js
154 lines (128 loc) · 4.52 KB
/
jquery.unveil-E.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Unveil-E "Enhanced"
*
* A very lightweight jQuery plugin to lazy load images
* Based on http://luis-almeida.github.com/unveil
*
* This is an enhanced version from Luis Almeida's Unveil
* featuring multiple screen densities,
* some optimisations for better performance,
* and low garbage memory saving.
*
* Licensed under the MIT license.
* Copyright 2016 Francisco Sánchez Encinas
* https://github.com/fsencinas
*/
(function($) {
var $w = $(window),
images = [],
$batch = $(),
initialised = false,
ticking = false,
// common density buckets
// (Keys are used as suffix for data-src-[density] and checking device pixel ratio)
densities = {tvdpi: 1.33, hdpi: 1.5, xhdpi: 2, retina: 2, xxhdpi: 3, xxxhdpi: 4};
// RAF fallback initial timing stuff
var now = Date.now || function(){return new Date().getTime();},
startTime = now(),
lastTime = startTime;
// requestAnimationFrame
var raf = window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame || function(fn) {
var currTime = now(),
delay = Math.max(0, 16 - (currTime - lastTime));
return window.setTimeout(function() {
lastTime = now();
fn(lastTime - startTime);
}, delay);
};
// html5 visibility API
var docHiddenProp = $.map(["hidden", "mozHidden", "webkitHidden"], function(p){
return typeof document[p] !== "undefined" ? p : null;
})[0] || "",
visibilityChange = docHiddenProp.replace(/[H|h]idden/, '') + 'visibilitychange';
// screen pixel density
var getDensity = function(){
var ret = [],
dpr = parseFloat((window.devicePixelRatio || 0).toFixed(2));
$.each(densities, function(d, r){
if (r === dpr) ret.push(d);
});
return ret;
};
var inview = function() {
var wt = $w.scrollTop() | 0,
wb = wt + $w.height() | 0;
return $batch.filter(function() {
var $img = $(this);
if ($img.is(":hidden")) return;
var th = $img.data("threshold"),
et = $img.offset().top | 0,
eb = (et + $img.height()) | 0;
return eb >= wt - th && et <= wb + th;
});
};
var unveil = function() {
if (!$batch.length) {
$w.off(".unveil");
initialised = false;
return;
}
var $unveiled = inview().trigger("unveil");
$batch = $batch.not($unveiled);
};
var lookup = function(){
unveil();
ticking = false;
};
var throttle = function(e){
if (!ticking) {
raf(lookup);
}
ticking = true;
};
// purge pool images not longer alive in DOM
var updatePool = function(){
return images = $.grep(images, function(img){
if ($.contains(document.documentElement, img)) return img;
});
};
// plugin definition
$.fn.unveil = function(threshold, callback) {
var screenDensity = getDensity();
this.data("threshold", (threshold || 0) | 0);
// pre update pool
images.length && updatePool();
// get only new images not already unveiled
var $images = this.filter(function(){
if ($.inArray(this, images) === -1) {
return this;
}
}).one("unveil", function(){
var $this = $(this);
var source = $.map(screenDensity, function(d) {
return $this.attr("data-src-" + d) || null;
})[0] || $this.attr("data-src");
if (source) {
$this.attr("src", source);
if (typeof callback === "function") {
callback.call(this);
}
}
});
$.merge(images, $images.get());
$batch = $batch.add($images);
if (!initialised) {
$w.on("lookup.unveil scroll.unveil resize.unveil", throttle);
// provide a mechanism to resume on tab change
// due rAf rate may be reduced or eventually paused when running in background tabs
docHiddenProp && $w.on(visibilityChange + ".unveil", function(){
if (!document[docHiddenProp]) unveil();
});
initialised = true;
}
unveil();
return $images;
};
}(window.jQuery || window.Zepto));