forked from aFarkas/lazysizes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathls.aspectratio.min.js
2 lines (2 loc) · 2.52 KB
/
ls.aspectratio.min.js
1
2
/*! lazysizes - v1.1.2 - Licensed MIT */
!function(a,b){"use strict";function c(){this.ratioElems=b.getElementsByClassName("lazyaspectratio"),this._setupEvents(),this.processImages()}if(a.addEventListener){var d,e,f,g=/^picture$/i,h="data-aspectratio",i="img["+h+"]",j=function(b){return a.matchMedia?(j=function(a){return!a||(matchMedia(a)||{}).matches})(b):a.Modernizr&&Modernizr.mq?!b||Modernizr.mq(b):!b},k=function(b,c){f?f(b).addClass(c):a.lazySizes?lazySizes.aC(b,c):b.classList.add(c)},l=function(b,c){f?f(b).removeClass(c):a.lazySizes?lazySizes.rC(b,c):b.classList.remove(c)};c.prototype={_setupEvents:function(){var a=this,c=function(b){b.naturalWidth<36?a.addAspectRatio(b,!0):a.removeAspectRatio(b,!0)},d=function(){a.processImages()};b.addEventListener("load",function(a){a.target.getAttribute&&a.target.getAttribute(h)&&c(a.target)},!0),addEventListener("resize",function(){var b,d=function(){var b,d;for(b=0,d=a.ratioElems.length;d>b;b++)c(a.ratioElems[b])};return function(){clearTimeout(b),b=setTimeout(d,33)}}()),b.addEventListener("DOMContentLoaded",d),addEventListener("load",d)},processImages:function(a){var c,d;a||(a=b),c="length"in a&&!a.nodeName?a:a.querySelectorAll(i);for(d=0;d<c.length;d++)c[d].naturalWidth>36?this.removeAspectRatio(c[d]):this.addAspectRatio(c[d])},getSelectedRatio:function(b){var c,d,e,f,i,k=b.parentNode;if(k&&g.test(k.nodeName||""))for(e=k.getElementsByTagName("source"),c=0,d=e.length;d>c;c++)if(f=e[c].getAttribute("data-media")||e[c].getAttribute("media"),a.lazySizesConfig&&lazySizesConfig.customMedia[f]&&(f=lazySizesConfig.customMedia[f]),j(f)){i=e[c].getAttribute(h);break}return i||b.getAttribute(h)||""},parseRatio:function(){var a=/^\s*([+\d\.]+)(\s*[\/x]\s*([+\d\.]+))?\s*$/,b={};return function(c){return!b[c]&&c.match(a)&&(RegExp.$3?b[c]=RegExp.$1/RegExp.$3:b[c]=1*RegExp.$1),b[c]}}(),addAspectRatio:function(b,c){var d,e=b.offsetWidth;return c||k(b,"lazyaspectratio"),36>e?void(e&&a.console&&console.log("Define width of image, so we can calculate the height")):(d=this.getSelectedRatio(b),d=this.parseRatio(d),void(d&&(b.style.height=e/d+"px")))},removeAspectRatio:function(a){l(a,"lazyaspectratio"),a.style.height="",a.removeAttribute(h)}},e=function(){f=a.jQuery||a.Zepto||a.shoestring||a.$,f&&f.fn&&!f.fn.imageRatio&&f.fn.filter&&f.fn.add&&f.fn.find?f.fn.imageRatio=function(){return d.processImages(this.find(i).add(this.filter(i))),this}:f=!1},e(),setTimeout(e),d=new c,a.imageRatio=d,"object"==typeof module&&module.exports?module.exports=d:"function"==typeof define&&define.amd&&define(d)}}(window,document);