|
1 | 1 | /** |
2 | | - * yall.js version 1.1.2 |
| 2 | + * yall.js version 1.2.0 |
3 | 3 | * Yet Another Lazy loader |
4 | 4 | **/ |
5 | 5 |
|
6 | 6 | (function(window, document){ |
7 | 7 | var |
8 | 8 | // The primary goal here is to keep this script as small as possible |
9 | 9 | // while maintaining functionality. The uglifier goes a long way, |
10 | | - // but we can take things a bit farther by saving strings for |
| 10 | + // but we can take things a bit farther by saving references for |
11 | 11 | // frequently used method and strings. |
12 | 12 | fe = "forEach", |
13 | 13 | qsa = "querySelectorAll", |
|
16 | 16 | pr = "prototype", |
17 | 17 | io = "IntersectionObserver", |
18 | 18 | ioe = io + "Entry", |
19 | | - dss = "data-srcset", |
| 19 | + s = "src", |
| 20 | + ss = "srcset", |
| 21 | + d = "data-", |
| 22 | + ds = d + s, |
| 23 | + dss = d + ss, |
20 | 24 | // Placeholders used for event handler strings. |
21 | 25 | documentEvents = ["scroll", "touchmove"], |
22 | 26 | windowEvents = ["orientationchange", "resize"], |
|
33 | 37 | node.removeAttribute(sourceAttr); |
34 | 38 | } |
35 | 39 | }, |
36 | | - // The handler to load the image |
37 | | - loadImage = function(img){ |
38 | | - if(img[pn].tagName == "PICTURE"){ |
39 | | - Array[pr].slice.call(img[pn][qsa]("source"))[fe](function(source){ |
40 | | - replaceAttr(source, dss, "srcset"); |
| 40 | + // The handler to load the media |
| 41 | + loadMedia = function(media){ |
| 42 | + if(media.tagName == "VIDEO"){ |
| 43 | + media[qsa]("source")[fe](function(source){ |
| 44 | + replaceAttr(source, ds, s); |
41 | 45 | }); |
| 46 | + |
| 47 | + media.load(); |
| 48 | + } |
| 49 | + else{ |
| 50 | + if(media[pn].tagName == "PICTURE"){ |
| 51 | + Array[pr].slice.call(media[pn][qsa]("source"))[fe](function(source){ |
| 52 | + replaceAttr(source, dss, ss); |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + replaceAttr(media, ds, s); |
| 57 | + replaceAttr(media, dss, ss); |
42 | 58 | } |
43 | 59 |
|
44 | | - replaceAttr(img, "data-src", "src"); |
45 | | - replaceAttr(img, dss, "srcset"); |
46 | | - img.classList.remove("lazy"); |
| 60 | + media.classList.remove("lazy"); |
47 | 61 | elements = elements.filter(function(e){ |
48 | | - return e !== img; |
| 62 | + return e !== media; |
49 | 63 | }); |
50 | 64 | }, |
51 | 65 | // A multiple event binding handler. |
|
67 | 81 | active = 1; |
68 | 82 |
|
69 | 83 | setTimeout(function(){ |
70 | | - elements[fe](function(img){ |
71 | | - if((img[gbcr]().top <= window.innerHeight && img[gbcr]().bottom >= 0) && getComputedStyle(img).display != "none"){ |
72 | | - loadImage(img); |
| 84 | + elements[fe](function(media){ |
| 85 | + if((media[gbcr]().top <= window.innerHeight && media[gbcr]().bottom >= 0) && getComputedStyle(media).display != "none"){ |
| 86 | + loadMedia(media); |
73 | 87 | } |
74 | 88 | }); |
75 | 89 |
|
|
80 | 94 |
|
81 | 95 | // Everything's kicked off on DOMContentLoaded |
82 | 96 | multiBind(document, ["DOMContentLoaded"], function(){ |
83 | | - elements = Array[pr].slice.call(document[qsa]("img.lazy")); |
| 97 | + elements = Array[pr].slice.call(document[qsa](".lazy")); |
84 | 98 |
|
85 | | - // We're only going to do stuff if we found `img.lazy` elements |
| 99 | + // We're only going to do stuff if we found `.lazy` elements |
86 | 100 | if(elements.length){ |
87 | 101 | // This compatibility check has been taken from https://github.com/WICG/IntersectionObserver/blob/gh-pages/polyfill/intersection-observer.js |
88 | 102 | if(io in window && ioe in window && "intersectionRatio" in window[ioe][pr]){ |
89 | | - var imageObserver = new window[io](function(entries, observer){ |
| 103 | + var mediaObserver = new window[io](function(entries, observer){ |
90 | 104 | entries[fe](function(entry){ |
91 | 105 | if(entry.isIntersecting){ |
92 | | - loadImage(entry.target); |
93 | | - imageObserver.unobserve(entry.target); |
| 106 | + loadMedia(entry.target); |
| 107 | + mediaObserver.unobserve(entry.target); |
94 | 108 | } |
95 | 109 | }); |
96 | 110 | }); |
97 | 111 |
|
98 | | - elements[fe](function(img){ |
99 | | - imageObserver.observe(img); |
| 112 | + elements[fe](function(media){ |
| 113 | + mediaObserver.observe(media); |
100 | 114 | }); |
101 | 115 | } |
102 | 116 | else{ |
|
0 commit comments