scroll-padlock
Version:
Locks elements scroll handling scrollbar gaps and iOS Safari with CSS variables
3 lines (2 loc) • 11.4 kB
JavaScript
function ownKeys(e,t){var l=Object.keys(e);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);t&&(i=i.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),l.push.apply(l,i)}return l}function _objectSpread(e){for(var t=1;t<arguments.length;t++){var l=null!=arguments[t]?arguments[t]:{};t%2?ownKeys(Object(l),!0).forEach((function(t){_defineProperty(e,t,l[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(l)):ownKeys(Object(l)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(l,t))}))}return e}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var l=0;l<t.length;l++){var i=t[l];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}function _createClass(e,t,l){return t&&_defineProperties(e.prototype,t),l&&_defineProperties(e,l),e}function _classPrivateFieldGet(e,t){return _classApplyDescriptorGet(e,_classExtractFieldDescriptor(e,t,"get"))}function _classApplyDescriptorGet(e,t){return t.get?t.get.call(e):t.value}function _classPrivateFieldSet(e,t,l){return _classApplyDescriptorSet(e,_classExtractFieldDescriptor(e,t,"set"),l),l}function _classExtractFieldDescriptor(e,t,l){if(!t.has(e))throw new TypeError("attempted to "+l+" private field on non-instance");return t.get(e)}function _classApplyDescriptorSet(e,t,l){if(t.set)t.set.call(e,l);else{if(!t.writable)throw new TypeError("attempted to set read only private field");t.value=l}}function _defineProperty(e,t,l){return t in e?Object.defineProperty(e,t,{value:l,enumerable:!0,configurable:!0,writable:!0}):e[t]=l,e}var ScrollPadlock=function(){"use strict";var e=window,t=document,l=t.documentElement,i=t.body,r=t.head,a="-",s="scroll",n="resize",c="Width",o="Height",d="Left",v="add",u="remove",h="scroll-padlock",_="data-scroll-padlock",P=function(e){return e.toLowerCase()},p=P("Top"),f=P(d),F=P(c),w=P(o),y="outerWidth",G="outerHeight",b="innerWidth",k="innerHeight",g="scrollWidth",W="scrollHeight",M="scrollbarWidth",m="scrollbarHeight",S="--scroll-padlock-scroll-"+p,O="--scroll-padlock-scroll-"+f,j="--scroll-padlock-scrollbar-"+F,E="--scroll-padlock-scrollbar-"+w,H="--scroll-padlock-outer-"+F,D="--scroll-padlock-outer-"+w,C="--scroll-padlock-inner-"+F,T="--scroll-padlock-inner-"+w,A="--scroll-padlock-scroll-"+F,L="--scroll-padlock-scroll-"+w,x=function(e,t,l,i){var r=!(arguments.length>4&&void 0!==arguments[4])||arguments[4];return null==t?void 0:t["".concat(e,"EventListener")](l,i,r)},I=e.clearImmediate||e.clearTimeout,R=e.setImmediate||e.setTimeout,K=function(e){var t;return function(){for(var l=arguments.length,i=new Array(l),r=0;r<l;r++)i[r]=arguments[r];I(t),t=R((function(){return e.apply(null,i)}))}},z=function(e,t){var l,i,r,a,s,n,c,o,d,v,u,h=(_defineProperty(l={},F,null==t?void 0:t.innerWidth),_defineProperty(l,w,null==t?void 0:t.innerHeight),l);h[F]||h[w]||(h=null!==(u=null==e?void 0:e.getBoundingClientRect())&&void 0!==u?u:{});var _=null!==(i=null===(r=h)||void 0===r?void 0:r[F])&&void 0!==i?i:0,P=null!==(a=null===(s=h)||void 0===s?void 0:s[w])&&void 0!==a?a:0,p=null!==(n=null==e?void 0:e.clientWidth)&&void 0!==n?n:0,f=null!==(c=null==e?void 0:e.clientHeight)&&void 0!==c?c:0;return _defineProperty(v={},y,_),_defineProperty(v,G,P),_defineProperty(v,b,Math.min(_,p)),_defineProperty(v,k,Math.min(P,f)),_defineProperty(v,g,null!==(o=null==e?void 0:e.scrollWidth)&&void 0!==o?o:0),_defineProperty(v,W,null!==(d=null==e?void 0:e.scrollHeight)&&void 0!==d?d:0),_defineProperty(v,M,Math.max(0,_-p)),_defineProperty(v,m,Math.max(0,P-f)),v},X=function(e){var t,l,i,r,a,s,n;return _defineProperty(n={},p,null!==(t=null!==(l=null!==(i=null==e?void 0:e.scrollY)&&void 0!==i?i:null==e?void 0:e.pageYOffset)&&void 0!==l?l:null==e?void 0:e.scrollTop)&&void 0!==t?t:0),_defineProperty(n,f,null!==(r=null!==(a=null!==(s=null==e?void 0:e.scrollX)&&void 0!==s?s:null==e?void 0:e.pageXOffset)&&void 0!==a?a:null==e?void 0:e.scrollLeft)&&void 0!==r?r:0),n},Y=function(e){var t;return null!==(t=null==e?void 0:e.parentElement)&&void 0!==t?t:null},B=function(e,t,l,i,r){var s,n,c;null!=l&&l.contains(t)||!t||l.appendChild(t);var o=null!==(s=null==e?void 0:e.getAttribute(_))&&void 0!==s?s:function(e){for(var t=-1;e;)t++,e=Y(e);return t}(e)+a+function(e){for(var t,l,i=null!==(t=null===(l=Y(e))||void 0===l?void 0:l.children)&&void 0!==t?t:[],r=0,a=0,s=i.length;a<s;a++){if(i[a]===e)return r;r++}return r}(e);null==e||e.setAttribute(_,o);var d=(null!=t?t:{}).sheet;null!=d&&null!==(n=d.cssRules)&&void 0!==n&&n[0]&&d.deleteRule(0);for(var v=(_defineProperty(c={},S,r[p]),_defineProperty(c,O,r[f]),_defineProperty(c,H,i.outerWidth),_defineProperty(c,D,i.outerHeight),_defineProperty(c,C,i.innerWidth),_defineProperty(c,T,i.innerHeight),_defineProperty(c,A,i.scrollWidth),_defineProperty(c,L,i.scrollHeight),_defineProperty(c,j,i.scrollbarWidth),_defineProperty(c,E,i.scrollbarHeight),c),u=Object.keys(v),h="",P=0,F=u.length;P<F;P++){var w=u[P];h+="".concat(w,": ").concat(v[w],"px;")}return null==d||d.insertRule("[".concat(_,'="').concat(o,'"] { ').concat(h," }"),0),t},q=function(e,t){return null==t||t.remove(),null==e||e.removeAttribute(_),t},J=function(e,t,l,i,r,a){var s;return(s={},_defineProperty(s,v,B),_defineProperty(s,u,q),s)[e](t,l,i,r,a)},N=new WeakMap,Q=l,U="".concat(h,"-locked"),V=new WeakMap,Z=new WeakMap,$=new WeakMap,ee=new WeakMap,te=new WeakMap,le=new WeakMap,ie=new WeakMap,re=new WeakMap,ae=new WeakMap,se=new WeakMap,ne=new WeakMap,ce=new WeakMap,oe=new WeakMap,de=new WeakMap,ve=new WeakMap,ue=new WeakMap,he=new WeakMap,_e=new WeakMap,Pe=new WeakMap,pe=new WeakMap,fe=new WeakMap,Fe=new WeakMap,we=new WeakMap,ye=new WeakMap,Ge=new WeakMap,be=new WeakMap;function ke(){var t;return _defineProperty(t={},n,e),_defineProperty(t,s,_classPrivateFieldGet(this,$)),t}return function(){function a(){var c,o,d,h=this,_=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Q,P=arguments.length>1&&void 0!==arguments[1]?arguments[1]:U;_classCallCheck(this,a),he.set(this,{get:ke,set:void 0}),V.set(this,{writable:!0,value:Q}),Z.set(this,{writable:!0,value:i}),$.set(this,{writable:!0,value:window}),ee.set(this,{writable:!0,value:void 0}),te.set(this,{writable:!0,value:U}),le.set(this,{writable:!0,value:void 0}),ie.set(this,{writable:!0,value:void 0}),re.set(this,{writable:!0,value:void 0}),ae.set(this,{writable:!0,value:void 0}),se.set(this,{writable:!0,value:!1}),ne.set(this,{writable:!0,value:(d="MutationObserver",d in e?new e.MutationObserver((function(){var e=_classPrivateFieldGet(h,le);_classPrivateFieldGet(h,Pe).call(h),_classPrivateFieldGet(h,le)!==e&&(_classPrivateFieldGet(h,le)?_classPrivateFieldSet(h,re,_objectSpread({},_classPrivateFieldGet(h,ie))):_classPrivateFieldGet(h,_e).call(h,_classPrivateFieldGet(h,re)))})):null)}),ce.set(this,{writable:!0,value:t.createElement("style")}),oe.set(this,{writable:!0,value:K((function(){_classPrivateFieldGet(h,pe).call(h,_classPrivateFieldGet(h,fe)),_classPrivateFieldGet(h,we).call(h)}))}),de.set(this,{writable:!0,value:K((function(){_classPrivateFieldGet(h,pe).call(h,_classPrivateFieldGet(h,Fe)),_classPrivateFieldGet(h,we).call(h)}))}),ve.set(this,{writable:!0,value:(c={},_defineProperty(c,n,u),_defineProperty(c,s,u),c)}),ue.set(this,{writable:!0,value:(o={},_defineProperty(o,n,_classPrivateFieldGet(this,oe)),_defineProperty(o,s,_classPrivateFieldGet(this,de)),o)}),_e.set(this,{writable:!0,value:function(e){return _classPrivateFieldGet(h,$).scrollTo(e[f],e[p])}}),Pe.set(this,{writable:!0,value:function(){var e;return _classPrivateFieldSet(h,le,!(null===(e=_classPrivateFieldGet(h,ee))||void 0===e||!e.contains(_classPrivateFieldGet(h,te))))}}),pe.set(this,{writable:!0,value:function(e){var t,l=_classPrivateFieldGet(h,le);_classPrivateFieldGet(h,Ge).call(h),null===(t=_classPrivateFieldGet(h,ee))||void 0===t||t.remove(_classPrivateFieldGet(h,te));var i,r=e();l&&(null===(i=_classPrivateFieldGet(h,ee))||void 0===i||i.add(_classPrivateFieldGet(h,te)));return _classPrivateFieldGet(h,ye).call(h),r}}),fe.set(this,{writable:!0,value:function(){return _classPrivateFieldSet(h,ae,z(_classPrivateFieldGet(h,V),_classPrivateFieldGet(h,$)))}}),Fe.set(this,{writable:!0,value:function(){return _classPrivateFieldSet(h,ie,X(_classPrivateFieldGet(h,$)))}}),we.set(this,{writable:!0,value:function(){return J(v,_classPrivateFieldGet(h,Z),_classPrivateFieldGet(h,ce),r,h.layout,h.scroll)}}),ye.set(this,{writable:!0,value:function(){var e;return _classPrivateFieldGet(h,se)?_classPrivateFieldGet(h,se):(null===(e=_classPrivateFieldGet(h,ne))||void 0===e||e.observe(_classPrivateFieldGet(h,Z),{attributes:!0,attributeFilter:["class"],childList:!1,characterData:!1}),_classPrivateFieldSet(h,se,!!_classPrivateFieldGet(h,ne)))}}),Ge.set(this,{writable:!0,value:function(){var e;return _classPrivateFieldGet(h,se)?(null===(e=_classPrivateFieldGet(h,ne))||void 0===e||e.disconnect(),_classPrivateFieldSet(h,se,!1)):_classPrivateFieldGet(h,se)}}),be.set(this,{writable:!0,value:function(e,t){var l=_classPrivateFieldGet(h,ve)[e];return!(!_classPrivateFieldGet(h,V)||!l||l===t)&&(_classPrivateFieldGet(h,ve)[e]=t,x(t,_classPrivateFieldGet(h,he)[e],e,_classPrivateFieldGet(h,ue)[e]),!0)}});var F=_===e,w=_ instanceof HTMLElement;if(!F&&!w)throw new TypeError("Invalid element provided to constructor");w&&_classPrivateFieldSet(this,Z,_);var y=F||_===l||_===i;if(y||_classPrivateFieldSet(this,V,_classPrivateFieldSet(this,Z,_classPrivateFieldSet(this,$,_))),N.has(_classPrivateFieldGet(this,V)))throw new Error("An instance has already been initialized on ".concat(y?"page scroll":"the given element"));N.set(_classPrivateFieldGet(this,V),this),_classPrivateFieldSet(this,ee,_classPrivateFieldGet(this,Z).classList);var G="string"==typeof P&&!!P.length;if(!G)throw new TypeError("Invalid CSS class name provided to constructor");_classPrivateFieldSet(this,te,P),this.update(),_classPrivateFieldGet(this,ye).call(this),_classPrivateFieldGet(this,be).call(this,n,v),_classPrivateFieldGet(this,be).call(this,s,v)}return _createClass(a,[{key:"scroll",get:function(){return _classPrivateFieldGet(this,le)?_classPrivateFieldGet(this,re):_classPrivateFieldGet(this,ie)},set:function(e){_classPrivateFieldGet(this,le)?_classPrivateFieldSet(this,re,e):_classPrivateFieldGet(this,_e).call(this,e)}},{key:"layout",get:function(){return _classPrivateFieldGet(this,ae)}},{key:"unlisten",value:function(e){_classPrivateFieldGet(this,be).call(this,e,u)}},{key:"listen",value:function(e){_classPrivateFieldGet(this,be).call(this,e,v)}},{key:"destroy",value:function(){_classPrivateFieldGet(this,Ge).call(this),J(u,_classPrivateFieldGet(this,V),_classPrivateFieldGet(this,ce)),_classPrivateFieldGet(this,be).call(this,n,u),_classPrivateFieldGet(this,be).call(this,s,u),N.delete(_classPrivateFieldGet(this,V)),_classPrivateFieldSet(this,V,_classPrivateFieldSet(this,Z,_classPrivateFieldSet(this,$,_classPrivateFieldSet(this,ne,_classPrivateFieldSet(this,ce,_classPrivateFieldSet(this,ee,null))))))}},{key:"update",value:function(){var e=this;_classPrivateFieldGet(this,Pe).call(this),_classPrivateFieldGet(this,pe).call(this,(function(){_classPrivateFieldGet(e,fe).call(e),_classPrivateFieldGet(e,Fe).call(e)})),_classPrivateFieldGet(this,we).call(this)}}]),a}()}();
//# sourceMappingURL=scroll-padlock.min.js.map