-
Notifications
You must be signed in to change notification settings - Fork 10
/
lazy-progressive-enhancement.js
82 lines (71 loc) · 2.92 KB
/
lazy-progressive-enhancement.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
/* @license
lazy-progressive-enhancement.js
https://github.com/tvler/lazy-progressive-enhancement
The MIT License (MIT)
Copyright (c) 2016 Tyler Deitz
*/
/**
* Loads media, hooking an optional onload function,
* optionally loading when scrolled into the viewport.
*
* @param {String | NodeList | Element} [media]
* String - (optional) A CSS selector targeting the noscript elements to be loaded.
* NodeList - (optional) A nodelist of noscript elements to be loaded.
* Element - (optional) A singular noscript element to be loaded.
* If not defined, targets every 'noscript' element on the page.
* @param {Function} [onloadfn] - (optional) The onload function fired for each targeted element.
* @param {Boolean} [scroll] - (optional) Load image when scrolled into the viewport.
*/
function loadMedia(media, onloadfn, scroll) {
'use strict'
var intervals = []
// Fires replaceNoscript either on DOMContentLoaded or after
// @see https://gist.github.com/tvler/8fd53d11ed775ebc72419bb5d96b8696
// @author tvler
function onwheneva() {
replaceNoscript(media)
}
document.readyState !== 'loading' ? onwheneva() :
document.addEventListener('DOMContentLoaded', onwheneva)
function scrollVisibility(img, src, srcset) {
var rect = img.getBoundingClientRect(),
offset = 300
if (
(rect.bottom >= -offset && rect.top - window.innerHeight < offset) &&
(rect.right >= -offset && rect.left - window.innerWidth < offset)
) {
clearInterval(intervals[src])
img.onload = onloadfn
srcset && (img.srcset = srcset)
img.src = src
}
}
function replaceNoscript(media) {
var noscript, img, src, srcset, parent, i = 0,
// Smallest data URI image possible for a transparent image
// @see http://stackoverflow.com/questions/6018611/smallest-data-uri-image-possible-for-a-transparent-image
// @author layke
tempSrc = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'
if (media == null || typeof media === 'string') {
media = document.body.querySelectorAll(media || 'noscript')
} else if (!media.length) {
media = [media]
}
while (noscript = media[i++]) {
// Create an img element in a DOMParser so the image won't load.
img = document.importNode((new DOMParser()).parseFromString(noscript.textContent, 'text/html').body.firstChild, true)
parent = noscript.parentElement
if (scroll) {
src = img.getAttribute('src')
srcset = img.getAttribute('srcset')
img.src = tempSrc
img.removeAttribute('srcset')
parent.replaceChild(img, noscript)
intervals[src] = setInterval(scrollVisibility, 100, img, src, srcset)
} else {
img.onload = onloadfn
parent.replaceChild(img, noscript)
}
}
}
}