-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmezr.js
1 lines (1 loc) · 7.7 KB
/
mezr.js
1
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).mezr={})}(this,(function(t){"use strict";const e=new WeakMap;function n(t,n){if(n)return window.getComputedStyle(t,n);let o=e.get(t)?.deref();return o||(o=window.getComputedStyle(t,null),e.set(t,new WeakRef(o))),o}const o=!!("undefined"!=typeof window&&void 0!==window.document&&navigator.vendor&&navigator.vendor.indexOf("Apple")>-1&&navigator.userAgent&&-1==navigator.userAgent.indexOf("CriOS")&&-1==navigator.userAgent.indexOf("FxiOS")),r={content:"content",padding:"padding",scrollbar:"scrollbar",border:"border",margin:"margin"},i={[r.content]:!1,[r.padding]:!1,[r.scrollbar]:!0,[r.border]:!0,[r.margin]:!0},l=new Set(["auto","scroll"]),a=(()=>{try{return window.navigator.userAgentData.brands.some((({brand:t})=>"Chromium"===t))}catch(t){return!1}})();function c(t){switch(n(t).display){case"none":return null;case"inline":case"contents":return!1;default:return!0}}function s(t){const e=n(t);if(!o){const{filter:t}=e;if(t&&"none"!==t)return!0;const{backdropFilter:n}=e;if(n&&"none"!==n)return!0;const{willChange:o}=e;if(o&&(o.indexOf("filter")>-1||o.indexOf("backdrop-filter")>-1))return!0}const r=c(t);if(!r)return r;const{transform:i}=e;if(i&&"none"!==i)return!0;const{perspective:l}=e;if(l&&"none"!==l)return!0;const{contentVisibility:a}=e;if(a&&"auto"===a)return!0;const{contain:s}=e;if(s&&("strict"===s||"content"===s||s.indexOf("paint")>-1||s.indexOf("layout")>-1))return!0;const{willChange:u}=e;return!(!u||!(u.indexOf("transform")>-1||u.indexOf("perspective")>-1||u.indexOf("contain")>-1))||!!(o&&u&&u.indexOf("filter")>-1)}function u(t){return"static"!==n(t).position||s(t)}function d(t){return t instanceof HTMLHtmlElement}function f(t,e={}){if(d(t))return t.ownerDocument.defaultView;const o=e.position||n(t).position,{skipDisplayNone:r,container:i}=e;switch(o){case"static":case"relative":case"sticky":case"-webkit-sticky":{let e=i||t.parentElement;for(;e;){const t=c(e);if(t)return e;if(null===t&&!r)return null;e=e.parentElement}return t.ownerDocument.documentElement}case"absolute":case"fixed":{const e="fixed"===o;let n=i||t.parentElement;for(;n;){const t=e?s(n):u(n);if(!0===t)return n;if(null===t&&!r)return null;n=n.parentElement}return t.ownerDocument.defaultView}default:return null}}function p(t,e,n,o){return Math.sqrt(Math.pow(n-t,2)+Math.pow(o-e,2))}function h(t){return t instanceof Window}function g(t){return t instanceof Document}const m=new Map;let w=null,b=null,x=null;function y(t,e,o){if(o<=0)return 0;if(a){const o=n(t,"::-webkit-scrollbar"),r="x"===e?o.height:o.width,i=parseFloat(r);if(!Number.isNaN(i)&&!Number.isInteger(i))return function(t,e){const n=t.split(".");let o=m.get(n[1]);void 0===o&&(w||(w=document.createElement("style")),w.innerHTML=`\n #mezr-scrollbar-test::-webkit-scrollbar {\n width: ${t} !important;\n }\n `,b&&x||(b=document.createElement("div"),x=document.createElement("div"),b.appendChild(x),b.id="mezr-scrollbar-test",b.style.cssText="\n all: unset !important;\n position: fixed !important;\n top: -200px !important;\n left: 0px !important;\n width: 100px !important;\n height: 100px !important;\n overflow: scroll !important;\n pointer-events: none !important;\n visibility: hidden !important;\n ",x.style.cssText="\n all: unset !important;\n position: absolute !important;\n inset: 0 !important;\n "),document.body.appendChild(w),document.body.appendChild(b),o=b.getBoundingClientRect().width-x.getBoundingClientRect().width-e,m.set(n[1],o),document.body.removeChild(b),document.body.removeChild(w));return e+o}(r,i)}return o}function v(t,e=r.border){return h(t)?function(t,e=!1){if(e)return t.innerWidth;const{innerWidth:n,document:o}=t,{documentElement:r}=o,{clientWidth:i}=r;return n-y(r,"y",n-i)}(t,i[e]):g(t)?function({documentElement:t}){return Math.max(t.scrollWidth,t.clientWidth,t.getBoundingClientRect().width)}(t):function(t,e=r.border){let{width:o}=t.getBoundingClientRect();if(e===r.border)return o;const i=n(t);return e===r.margin?(o+=Math.max(0,parseFloat(i.marginLeft)||0),o+=Math.max(0,parseFloat(i.marginRight)||0),o):(o-=parseFloat(i.borderLeftWidth)||0,o-=parseFloat(i.borderRightWidth)||0,e===r.scrollbar||(!d(t)&&l.has(i.overflowY)&&(o-=y(t,"y",Math.round(o)-t.clientWidth)),e===r.padding||(o-=parseFloat(i.paddingLeft)||0,o-=parseFloat(i.paddingRight)||0)),o)}(t,e)}function F(t,e=r.border){return h(t)?function(t,e=!1){if(e)return t.innerHeight;const{innerHeight:n,document:o}=t,{documentElement:r}=o,{clientHeight:i}=r;return n-y(r,"x",n-i)}(t,i[e]):g(t)?function({documentElement:t}){return Math.max(t.scrollHeight,t.clientHeight,t.getBoundingClientRect().height)}(t):function(t,e=r.border){let{height:o}=t.getBoundingClientRect();if(e===r.border)return o;const i=n(t);return e===r.margin?(o+=Math.max(0,parseFloat(i.marginTop)||0),o+=Math.max(0,parseFloat(i.marginBottom)||0),o):(o-=parseFloat(i.borderTopWidth)||0,o-=parseFloat(i.borderBottomWidth)||0,e===r.scrollbar||(!d(t)&&l.has(i.overflowX)&&(o-=y(t,"x",Math.round(o)-t.clientHeight)),e===r.padding||(o-=parseFloat(i.paddingTop)||0,o-=parseFloat(i.paddingBottom)||0)),o)}(t,e)}function M(t){return t?.constructor===Object}function C(t,e=r.border){const o={left:0,top:0};if(g(t))return o;if(h(t))return o.left+=t.scrollX||0,o.top+=t.scrollY||0,o;const i=t.ownerDocument.defaultView;i&&(o.left+=i.scrollX||0,o.top+=i.scrollY||0);const l=t.getBoundingClientRect();if(o.left+=l.left,o.top+=l.top,e===r.border)return o;const a=n(t);return e===r.margin?(o.left-=Math.max(0,parseFloat(a.marginLeft)||0),o.top-=Math.max(0,parseFloat(a.marginTop)||0),o):(o.left+=parseFloat(a.borderLeftWidth)||0,o.top+=parseFloat(a.borderTopWidth)||0,e===r.scrollbar||e===r.padding||(o.left+=parseFloat(a.paddingLeft)||0,o.top+=parseFloat(a.paddingTop)||0),o)}function O(t,e){const n=M(t)?{left:t.left,top:t.top}:Array.isArray(t)?C(...t):C(t);if(e&&!g(e)){const t=M(e)?e:Array.isArray(e)?C(e[0],e[1]):C(e);n.left-=t.left,n.top-=t.top}return n}function W(t,e){let n=0,o=0;M(t)?(n=t.width,o=t.height):Array.isArray(t)?(n=v(...t),o=F(...t)):(n=v(t),o=F(t));const r=O(t,e);return{width:n,height:o,...r,right:r.left+n,bottom:r.top+o}}function k(t){return M(t)?t:W(t)}t.getContainingBlock=f,t.getDistance=function(t,e){return function(t,e){if(function(t,e){return!(t.left+t.width<=e.left||e.left+e.width<=t.left||t.top+t.height<=e.top||e.top+e.height<=t.top)}(t,e))return null;const n=t.left+t.width,o=t.top+t.height,r=e.left+e.width,i=e.top+e.height;return n<=e.left?o<=e.top?p(n,o,e.left,e.top):t.top>=i?p(n,t.top,e.left,i):e.left-n:t.left>=r?o<=e.top?p(t.left,o,r,e.top):t.top>=i?p(t.left,t.top,r,i):t.left-r:o<=e.top?e.top-o:t.top-i}(k(t),k(e))},t.getHeight=F,t.getIntersection=function(t,...e){const n={...k(t),right:0,bottom:0};for(const t of e){const e=k(t),o=Math.max(n.left,e.left),r=Math.min(n.left+n.width,e.left+e.width);if(r<=o)return null;const i=Math.max(n.top,e.top),l=Math.min(n.top+n.height,e.height+e.top);if(l<=i)return null;n.left=o,n.top=i,n.width=r-o,n.height=l-i}return n.right=n.left+n.width,n.bottom=n.top+n.height,n},t.getOffset=O,t.getOffsetContainer=function(t,e={}){const o=n(t),{display:r}=o;if("none"===r||"contents"===r)return null;const i=e.position||n(t).position,{skipDisplayNone:l,container:a}=e;switch(i){case"relative":return t;case"fixed":return f(t,{container:a,position:i,skipDisplayNone:l});case"absolute":{const e=f(t,{container:a,position:i,skipDisplayNone:l});return h(e)?t.ownerDocument:e}default:return null}},t.getOverflow=function(t,e){const n=k(t),o=k(e);return{left:o.left-n.left,right:n.left+n.width-(o.left+o.width),top:o.top-n.top,bottom:n.top+n.height-(o.top+o.height)}},t.getRect=W,t.getWidth=v}));