UNPKG

lazysizes

Version:

High performance (jankfree) lazy loader for images (including responsive images), iframes and scripts (widgets).

3 lines (2 loc) 2.67 kB
/*! lazysizes - v5.3.2 */ !function(e,t){var i=function(){t(e.lazySizes),e.removeEventListener("lazyunveilread",i,!0)};t=t.bind(null,e,e.document),"object"==typeof module&&module.exports?t(require("lazysizes")):"function"==typeof define&&define.amd?define(["lazysizes"],t):e.lazySizes?i():e.addEventListener("lazyunveilread",i,!0)}(window,function(o,r,e){"use strict";var s,t,i,n,d,c,a,u,f,l,m,h,p;function g(){this.ratioElems=r.getElementsByClassName("lazyaspectratio"),this._setupEvents(),this.processImages()}o.addEventListener&&(s=Array.prototype.forEach,d=/^picture$/i,a="img["+(c="data-aspectratio")+"]",u=function(e){return o.matchMedia?(u=function(e){return!e||(matchMedia(e)||{}).matches})(e):o.Modernizr&&Modernizr.mq?!e||Modernizr.mq(e):!e},f=e.aC,l=e.rC,m=e.cfg,g.prototype={_setupEvents:function(){function t(e){e.naturalWidth<36?n.addAspectRatio(e,!0):n.removeAspectRatio(e,!0)}function e(){n.processImages()}var i,n=this;function a(){s.call(n.ratioElems,t)}r.addEventListener("load",function(e){e.target.getAttribute&&e.target.getAttribute(c)&&t(e.target)},!0),addEventListener("resize",function(){clearTimeout(i),i=setTimeout(a,99)}),r.addEventListener("DOMContentLoaded",e),addEventListener("load",e)},processImages:function(e){for(var t=("length"in(e=e||r)&&!e.nodeName?e:e.querySelectorAll(a)),i=0;i<t.length;i++)36<t[i].naturalWidth?this.removeAspectRatio(t[i]):this.addAspectRatio(t[i])},getSelectedRatio:function(e){var t,i,n,a,o,r=e.parentNode;if(r&&d.test(r.nodeName||""))for(t=0,i=(n=r.getElementsByTagName("source")).length;t<i;t++)if(a=n[t].getAttribute("data-media")||n[t].getAttribute("media"),m.customMedia[a]&&(a=m.customMedia[a]),u(a)){o=n[t].getAttribute(c);break}return o||e.getAttribute(c)||""},parseRatio:(h=/^\s*([+\d\.]+)(\s*[\/x]\s*([+\d\.]+))?\s*$/,p={},function(e){var t;return!p[e]&&(t=e.match(h))&&(t[3]?p[e]=t[1]/t[3]:p[e]=+t[1]),p[e]}),addAspectRatio:function(e,t){var i,n=e.offsetWidth,a=e.offsetHeight;t||f(e,"lazyaspectratio"),n<36&&a<=0?(n||a&&o.console)&&console.log("Define width or height of image, so we can calculate the other dimension"):(i=this.getSelectedRatio(e),(i=this.parseRatio(i))&&(n?e.style.height=n/i+"px":e.style.width=a*i+"px"))},removeAspectRatio:function(e){l(e,"lazyaspectratio"),e.style.height="",e.style.width="",e.removeAttribute(c)}},(i=function(){(n=o.jQuery||o.Zepto||o.shoestring||o.$)&&n.fn&&!n.fn.imageRatio&&n.fn.filter&&n.fn.add&&n.fn.find?n.fn.imageRatio=function(){return t.processImages(this.find(a).add(this.filter(a))),this}:n=!1})(),setTimeout(i),t=new g,o.imageRatio=t,"object"==typeof module&&module.exports?module.exports=t:"function"==typeof define&&define.amd&&define(t))});