-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
74 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,91 @@ | ||
/** | ||
* yall.js version 1.1.0 | ||
* yall.js version 1.1.1 | ||
* Yet Another Lazy loader | ||
* This library is intended to be very small. As such, some of may not be very readable. | ||
* I don't normally code like this, but I wanted to see just how small I could get it! | ||
**/ | ||
|
||
(function(window, document){ | ||
/** | ||
* Everything below is defined in such a way to reduce | ||
* uglifier output. If a method or string is only used once, it | ||
* will be invoked normally. Otherwise, method shorthands are used. | ||
**/ | ||
|
||
var | ||
// Placeholders used for common method names. | ||
qsa = "querySelectorAll", | ||
fe = "forEach", | ||
pn = "parentNode", | ||
pr = "prototype", | ||
gbcr = "getBoundingClientRect", | ||
io = "IntersectionObserver", | ||
// Placeholders used for "data-src" and "data-srcset" attribute references. | ||
dss = "data-srcset", | ||
// Placeholders used for event handler strings. | ||
y = ["scroll", "touchmove"], | ||
z = ["orientationchange", "resize"], | ||
documentEvents = ["scroll", "touchmove"], | ||
windowEvents = ["orientationchange", "resize"], | ||
// Tracks if yall is currently processing. Used for throttling. Only matters if IntersectionObserver is unsupported. | ||
active = 0, | ||
// Replaces target attribute value with source attribute, if applicable | ||
replaceAttr = function(node, sattr, tattr){ | ||
var v = node.getAttribute(sattr); | ||
if(v){ | ||
node[tattr] = v; | ||
node.removeAttribute(sattr); | ||
} | ||
}, | ||
// The handler to load the image | ||
l = function(img){ | ||
if(img[pn].tagName == "PICTURE"){ | ||
Array[pr].slice.call(img[pn][qsa]("source"))[fe](function(source){ | ||
ra(source, dss, "srcset"); | ||
loadImage = function(img){ | ||
if(img.parentNode.tagName == "PICTURE"){ | ||
Array.prototype.slice.call(img.parentNode.querySelectorAll("source")).forEach(function(source){ | ||
replaceAttr(source, "data-srcset", "srcset"); | ||
}); | ||
} | ||
|
||
ra(img, "data-src", "src"); | ||
ra(img, dss, "srcset"); | ||
replaceAttr(img, "data-src", "src"); | ||
replaceAttr(img, "data-srcset", "srcset"); | ||
img.classList.remove("lazy"); | ||
els.splice(els.indexOf(img), 1); | ||
elements.splice(elements.indexOf(img), 1); | ||
}, | ||
// Tracks if yall is currently processing. Used for throttling. Only matters if IntersectionObserver is unsupported. | ||
a = 0, | ||
// A multiple event binding handler. | ||
b = function(obj, handlers, fn, add){ | ||
handlers[fe](function(handler){ | ||
add ? obj.addEventListener(handler, fn) : obj.removeEventListener(handler, fn); | ||
multiBind = function(obj, handlers, fn, remove){ | ||
handlers.forEach(function(handler){ | ||
remove ? obj.removeEventListener(handler, fn) : obj.addEventListener(handler, fn); | ||
}); | ||
}, | ||
// Replaces target attribute value with source attribute, if applicable | ||
ra = function(node, sattr, tattr){ | ||
var v = node.getAttribute(sattr); | ||
if(v){ | ||
node[tattr] = v; | ||
node.removeAttribute(sattr); | ||
} | ||
}, | ||
// The guts of the lazy loader (now only used when IntersectionObserver is not supported) | ||
ll = function(){ | ||
if(!els.length) (b(document, y, ll), b(window, z, ll)); | ||
yall = function(){ | ||
if(!elements.length){ | ||
multiBind(document, documentEvents, yall, 1); | ||
multiBind(window, windowEvents, yall, 1); | ||
} | ||
|
||
if(!active){ | ||
active = 1; | ||
|
||
if(!a){ | ||
a = 1; | ||
setTimeout(function(){ | ||
els[fe](function(img){ | ||
if((img[gbcr]().top <= window.innerHeight && img[gbcr]().bottom >= 0) && getComputedStyle(img).display != "none") l(img); | ||
elements.forEach(function(img){ | ||
if((img.getBoundingClientRect().top <= window.innerHeight && img.getBoundingClientRect().bottom >= 0) && getComputedStyle(img).display != "none"){ | ||
loadImage(img); | ||
} | ||
}); | ||
|
||
a = 0; | ||
active = 0; | ||
}, 200); | ||
} | ||
}; | ||
|
||
// Everything's kicked off on DOMContentLoaded | ||
b(document, ["DOMContentLoaded"], function(){ | ||
els = Array[pr].slice.call(document[qsa]("img.lazy")); | ||
multiBind(document, ["DOMContentLoaded"], function(){ | ||
elements = Array.prototype.slice.call(document.querySelectorAll("img.lazy")); | ||
|
||
if(elements.length){ | ||
// This compatibility check has been taken from https://github.com/WICG/IntersectionObserver/blob/gh-pages/polyfill/intersection-observer.js | ||
if("IntersectionObserver" in window && "IntersectionObserverEntry" in window && "intersectionRatio" in IntersectionObserverEntry.prototype){ | ||
ob = new IntersectionObserver(function(entries, observer){ | ||
entries.forEach(function(entry){ | ||
if(entry.isIntersecting){ | ||
loadImage(entry.target); | ||
if(!elements.length) observer.disconnect(); | ||
} | ||
}); | ||
}); | ||
|
||
if(els.length){ | ||
if(io in window && io+"Entry" in window && "intersectionRatio" in window[io+"Entry"][pr]){ | ||
els[fe](function(img){ | ||
new window[io](function(entries, observer){ | ||
entries[fe](function(entry){ | ||
if(entry.isIntersecting){ | ||
l(img); | ||
observer.disconnect(); | ||
} | ||
}); | ||
}).observe(img); | ||
elements.forEach(function(img){ | ||
ob.observe(img); | ||
}); | ||
|
||
return; | ||
} | ||
|
||
ll(); | ||
b(document, y, ll, 1); | ||
b(window, z, ll, 1); | ||
// If IntersectionObserver isn't available, we'll do things the old way. | ||
yall(); | ||
multiBind(document, documentEvents, yall); | ||
multiBind(window, windowEvents, yall); | ||
} | ||
}, 1); | ||
}); | ||
})(window, document); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters