-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
28 lines (25 loc) · 849 Bytes
/
main.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
(function(){
var proximityThreshold = 100; // Pixels
var automaticSize = true;
function lazyLoadSize(dom,rect){
if(automaticSize){
var calculatedSize = Math.ceil((rect.width / window.outerWidth) * 100);
calculatedSize = calculatedSize+"vw";
dom.setAttribute("sizes",calculatedSize);
}
dom.setAttribute("srcset",dom.getAttribute('data-srcset'));
}
window.addEventListener('load',function(){
document.querySelectorAll('img[data-srcset]').forEach(function(dom){
var listener = function(){
var rect = dom.getBoundingClientRect();
if(rect.top - window.outerHeight < proximityThreshold){
lazyLoadSize(dom,rect);
window.removeEventListener("scroll",listener);
}
};
window.addEventListener("scroll",listener);
listener();
});
});
})();