Skip to content

Commit f1fc170

Browse files
committed
Went back to variable refs.
1 parent 3aab5f3 commit f1fc170

File tree

5 files changed

+56
-34
lines changed

5 files changed

+56
-34
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# yall.js (Yet Another Lazy Loader)
22
### (Y'all need to lazy load images)
33

4-
yall.js is a very small image lazy loader for reasonably modern browsers (back to IE10) that weighs in at 1.24 KB uglified (621 bytes with gzip/507 bytes with Brotli). It depends on `classList`, `querySelectorAll`, supports the `<picture>` element and the `srcset` attribute. yall.js also uses `IntersectionObserver` if available, but will fall back to more traditional means if it's not. If you want to try out yall.js, grab the copy in the `dist` folder. Or you can clone the repo and check out the `test` folder. If you want to tinker, work with the copy in the `src` folder and build using `npm run build` (requires `npx`).
4+
yall.js is a very small image lazy loader for reasonably modern browsers (back to IE10) that weighs in at 1.09 KB uglified (657 bytes with gzip/554 bytes with Brotli). It depends on `classList`, `querySelectorAll`, and supports the `<picture>` element and `srcset`. yall.js will also use `IntersectionObserver` if available, but will fall back to more traditional means if it's not. If you want to try out yall.js, grab the copy in the `dist` folder. Or you can clone the repo and check out the `test` folder. If you want to tinker, work with the copy in the `src` folder and build using `npm run build` (requires `npx`).
55

66
## Usage Pattern
77

dist/yall-1.1.1.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/yall.min.js

-1
This file was deleted.

package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
"name": "yall",
33
"version": "1.1.1",
44
"description": "Yet Another Lazy Loader",
5+
"main": "./src/yall.js",
56
"scripts": {
6-
"build": "npx uglifyjs --verbose --compress warnings --mangle -o ./dist/yall.js ./src/yall.js"
7+
"clean": "rm -rf ./dist&&mkdir -p ./dist",
8+
"build": "npm run clean&&npx uglifyjs --verbose --toplevel --compress toplevel --mangle toplevel -o ./dist/yall-$(node -p -e \"require('./package.json').version\").min.js $(node -p -e \"require('./package.json').main\")"
79
},
810
"repository": {
911
"type": "git",
10-
"url": "git+https://github.com/malchata/yall.git"
12+
"url": "git+https://github.com/malchata/yall.js.git"
1113
},
1214
"keywords": [
1315
"lazy load",
@@ -17,9 +19,9 @@
1719
"author": "Jeremy Wagner <jeremy.l.wagner@gmail.com> (https://jeremywagner.me/)",
1820
"license": "MIT",
1921
"bugs": {
20-
"url": "https://github.com/malchata/yall/issues"
22+
"url": "https://github.com/malchata/yall.js/issues"
2123
},
22-
"homepage": "https://github.com/malchata/yall#readme",
24+
"homepage": "https://github.com/malchata/yall.js#readme",
2325
"devDependencies": {
2426
"uglify-js": "^3.0.27"
2527
},

src/yall.js

+48-27
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,69 @@
55

66
(function(window, document){
77
var
8+
// The primary goal here is to keep this script as small as possible
9+
// while maintaining functionality. The uglifier goes a long way,
10+
// but we can take things a bit farther by saving strings for
11+
// frequently used method and strings.
12+
fe = "forEach",
13+
qsa = "querySelectorAll",
14+
pn = "parentNode",
15+
gbcr = "getBoundingClientRect",
16+
pr = "prototype",
17+
io = "IntersectionObserver",
18+
ioe = io + "Entry",
19+
ln = "length",
20+
dss = "data-srcset",
821
// Placeholders used for event handler strings.
922
documentEvents = ["scroll", "touchmove"],
1023
windowEvents = ["orientationchange", "resize"],
1124
// Tracks if yall is currently processing. Used for throttling. Only matters if IntersectionObserver is unsupported.
1225
active = 0,
26+
// Placeholder for elements
27+
elements,
1328
// Replaces target attribute value with source attribute, if applicable
14-
replaceAttr = function(node, sattr, tattr){
15-
var v = node.getAttribute(sattr);
29+
replaceAttr = function(node, sourceAttr, targetAttr){
30+
var v = node.getAttribute(sourceAttr);
31+
1632
if(v){
17-
node[tattr] = v;
18-
node.removeAttribute(sattr);
33+
node[targetAttr] = v;
34+
node.removeAttribute(sourceAttr);
1935
}
2036
},
2137
// The handler to load the image
2238
loadImage = function(img){
23-
if(img.parentNode.tagName == "PICTURE"){
24-
Array.prototype.slice.call(img.parentNode.querySelectorAll("source")).forEach(function(source){
25-
replaceAttr(source, "data-srcset", "srcset");
39+
if(img[pn].tagName == "PICTURE"){
40+
Array[pr].slice.call(img[pn][qsa]("source"))[fe](function(source){
41+
replaceAttr(source, dss, "srcset");
2642
});
2743
}
2844

2945
replaceAttr(img, "data-src", "src");
30-
replaceAttr(img, "data-srcset", "srcset");
46+
replaceAttr(img, dss, "srcset");
3147
img.classList.remove("lazy");
3248
elements.splice(elements.indexOf(img), 1);
3349
},
3450
// A multiple event binding handler.
3551
multiBind = function(obj, handlers, fn, remove){
36-
handlers.forEach(function(handler){
52+
handlers[fe](function(handler){
3753
remove ? obj.removeEventListener(handler, fn) : obj.addEventListener(handler, fn);
3854
});
3955
},
4056
// The guts of the lazy loader (now only used when IntersectionObserver is not supported)
4157
yall = function(){
42-
if(!elements.length){
58+
if(!elements[ln]){
59+
// There are no more elements to lazy load, so we'll unbind everything.
4360
multiBind(document, documentEvents, yall, 1);
4461
multiBind(window, windowEvents, yall, 1);
4562
}
4663

64+
// Check if the lazy loader is active
4765
if(!active){
4866
active = 1;
4967

5068
setTimeout(function(){
51-
elements.forEach(function(img){
52-
if((img.getBoundingClientRect().top <= window.innerHeight && img.getBoundingClientRect().bottom >= 0) && getComputedStyle(img).display != "none"){
69+
elements[fe](function(img){
70+
if((img[gbcr]().top <= window.innerHeight && img[gbcr]().bottom >= 0) && getComputedStyle(img).display != "none"){
5371
loadImage(img);
5472
}
5573
});
@@ -61,31 +79,34 @@
6179

6280
// Everything's kicked off on DOMContentLoaded
6381
multiBind(document, ["DOMContentLoaded"], function(){
64-
elements = Array.prototype.slice.call(document.querySelectorAll("img.lazy"));
82+
elements = Array[pr].slice.call(document[qsa]("img.lazy"));
6583

66-
if(elements.length){
84+
// We're only going to do stuff if we found `img.lazy` elements
85+
if(elements[ln]){
6786
// This compatibility check has been taken from https://github.com/WICG/IntersectionObserver/blob/gh-pages/polyfill/intersection-observer.js
68-
if("IntersectionObserver" in window && "IntersectionObserverEntry" in window && "intersectionRatio" in IntersectionObserverEntry.prototype){
69-
ob = new IntersectionObserver(function(entries, observer){
70-
entries.forEach(function(entry){
87+
if(io in window && ioe in window && "intersectionRatio" in window[ioe][pr]){
88+
var imageObserver = new window[io](function(entries, observer){
89+
entries[fe](function(entry){
7190
if(entry.isIntersecting){
7291
loadImage(entry.target);
73-
if(!elements.length) observer.disconnect();
92+
93+
if(!elements[ln]){
94+
observer.disconnect();
95+
}
7496
}
7597
});
7698
});
7799

78-
elements.forEach(function(img){
79-
ob.observe(img);
100+
elements[fe](function(img){
101+
imageObserver.observe(img);
80102
});
81-
82-
return;
83103
}
84-
85-
// If IntersectionObserver isn't available, we'll do things the old way.
86-
yall();
87-
multiBind(document, documentEvents, yall);
88-
multiBind(window, windowEvents, yall);
104+
else{
105+
// If IntersectionObserver isn't available, we'll do things the old way.
106+
yall();
107+
multiBind(document, documentEvents, yall);
108+
multiBind(window, windowEvents, yall);
109+
}
89110
}
90111
});
91112
})(window, document);

0 commit comments

Comments
 (0)