UNPKG

@thednp/dll

Version:

Double Lazy Load for Images, Videos and Background Images.

3 lines (2 loc) 1.7 kB
var DLL=function(){"use strict";const D="load",E="loadstart",I=(e,n,s,t)=>{e.addEventListener(n,s,!1)},N=(e,n,s,t)=>{e.removeEventListener(n,s,!1)},y=(e,n)=>e.getAttribute(n),j=(e,n)=>e.removeAttribute(n),S=e=>e!=null&&typeof e=="object"||!1,f=e=>S(e)&&typeof e.nodeType=="number"&&[1,2,3,4,5,6,7,8,9,10,11].some(n=>e.nodeType===n)||!1,u=e=>f(e)&&e.nodeType===1||!1,b=e=>typeof e=="string"||!1,R=e=>S(e)&&e.constructor.name==="Window"||!1,C=e=>f(e)&&e.nodeType===9||!1,d=e=>C(e)?e:f(e)?e.ownerDocument:R(e)?e.document:globalThis.document,L=(e,...n)=>Object.assign(e,...n),p=e=>{if(!e)return;if(b(e))return d().createElement(e);const{tagName:n}=e,s=p(n);if(!s)return;const t={...e};return delete t.tagName,L(s,t)},M=e=>Object.entries(e),V=(e,n)=>{M(n).forEach(([s,t])=>{if(t&&b(s)&&s.includes("--"))e.style.setProperty(s,t);else{const o={};o[s]=t,L(e.style,o)}})},O=(e,n)=>(f(n)?n:d()).querySelectorAll(e),g=e=>u(e)&&"offsetWidth"in e||!1,T=e=>typeof e=="function"||!1,W=(e,n)=>u(e)?e:(u(n)?n:d()).querySelector(e),c="data-src",q=e=>g(e)&&e.tagName==="VIDEO",l=e=>g(e)&&e.tagName==="SOURCE",h=e=>g(e)&&e.tagName==="IMG",v=(e,n)=>{const s=l(e),t=s?E:D,o=s?p("VIDEO"):null,r=p(s?"SOURCE":"IMG"),a=s?o:r,i=y(e,c);if(!a||!i)return;const m=()=>{h(e)||l(e)?(e.src=i,l(e)&&q(e.parentElement)&&e.parentElement.load()):V(e,{backgroundImage:`url("${i}")`}),j(e,c),T(n)&&n(),N(a,t,m)};I(a,t,m),(h(r)||l(r))&&(r.src=i,o&&o.append(r))},w=e=>{const n=O(`[${c}]`,e),s=e&&y(e,c);return s&&!n?[e]:!s&&n?[...n]:s&&n?[e,...n]:!s&&!n?[...O(`[${c}]`)]:null};return(e,n)=>{const s=W(e);if(!s)return;const t=T(n)?n:null,o=w(s);o&&o.length&&o.forEach((r,a)=>{a===o.length-1&&t?v(r,t):v(r)})}}(); //# sourceMappingURL=dll.js.map