-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresponsive-background-images.min.js
1 lines (1 loc) · 1.81 KB
/
responsive-background-images.min.js
1
!function(e,t){"function"==typeof define&&define.amd?define([],t(e)):"object"==typeof exports?module.exports=t(e):e.ResponsiveBackgroundImages=t(e)}(this,function(e){"use strict";var t={},r={selector:"background-image-srcset",interval:250},n=[],i=function(e,t,r){if("[object Object]"===Object.prototype.toString.call(e))for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&t.call(r,e[n],n,e);else for(var i=0,o=e.length;o>i;i++)t.call(r,e[i],i,e)},o=function(e,t){var r={};return i(e,function(t,n){r[n]=e[n]}),i(t,function(e,n){r[n]=t[n]}),r},a=function(e,t,r){var n,i,o,a,c,l=function(){var u=(new Date).getTime(),f=u-a;t>f&&f>=0?n=setTimeout(l,t-f):(n=null,r||(c=e.apply(o,i),n||(o=i=null)))};return function(){o=this,i=arguments,a=(new Date).getTime();var u=r&&!n;return n||(n=setTimeout(l,t)),u&&(c=e.apply(o,i),o=i=null),c}};return t.run=function(e){r=o(r,e||{});for(var i,a,c,l=document.querySelectorAll("[data-"+r.selector+"]"),u=0,f=l.length;f>u;u++){var s=[];if(i=l[u],a=i.getAttribute("data-"+r.selector),c=i.offsetWidth*window.devicePixelRatio||1,s=t.parseSrcSet(a),s.length>0)for(var d=0,g=s.length;g>d;d++)if(s[d].width>=c||d===g-1){var v=null;s[d].force||(v=t.findAlreadyLoadedBiggerImage(s,c));var h=v?v:s[d].src;i.style.backgroundImage="url('"+h+"')",-1===n.indexOf(h)&&n.push(h);break}}},t.parseSrcSet=function(e){var t=[];if(null!==e&&e.length>0){for(var r=e.split(","),n=0,i=r.length;i>n;n++){var o=r[n].match(/(\S+)\s+(\d+)w(\s+force)?/);t.push({src:o[1],width:o[2],force:!!o[3]})}t.sort(function(e,t){return e.width-t.width})}return t},t.findAlreadyLoadedBiggerImage=function(e,t){for(var r=e.slice(0).reverse(),i=0,o=r.length;o>i;i++)if(-1!==n.indexOf(r[i].src)&&(r[i].width>=t||0===i))return r[i].src;return null},t.addResizeEvent=function(){window.addEventListener("resize",a(t.run,r.interval))},t.run(),t.addResizeEvent(),t});