UNPKG

3.28 kBJavaScriptView Raw
1!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).scrollIntoView=t()}(this,(function(){"use strict";function e(e){return"object"==typeof e&&null!=e&&1===e.nodeType}function t(e,t){return(!t||"hidden"!==e)&&"visible"!==e&&"clip"!==e}function n(e,n){if(e.clientHeight<e.scrollHeight||e.clientWidth<e.scrollWidth){var o=getComputedStyle(e,null);return t(o.overflowY,n)||t(o.overflowX,n)||function(e){var t=function(e){if(!e.ownerDocument||!e.ownerDocument.defaultView)return null;try{return e.ownerDocument.defaultView.frameElement}catch(e){return null}}(e);return!!t&&(t.clientHeight<e.scrollHeight||t.clientWidth<e.scrollWidth)}(e)}return!1}function o(e,t,n,o,r,i,l,c){return i<e&&l>t||i>e&&l<t?0:i<=e&&c<=n||l>=t&&c>=n?i-e-o:l>t&&c<n||i<e&&c>n?l-t+r:0}function r(t,r){var i=window,l=r.scrollMode,c=r.block,u=r.inline,f=r.boundary,d=r.skipOverflowHiddenElements,a="function"==typeof f?f:function(e){return e!==f};if(!e(t))throw new TypeError("Invalid target");for(var s=document.scrollingElement||document.documentElement,h=[],p=t;e(p)&&a(p);){if((p=p.parentElement)===s){h.push(p);break}null!=p&&p===document.body&&n(p)&&!n(document.documentElement)||null!=p&&n(p,d)&&h.push(p)}for(var m=i.visualViewport?i.visualViewport.width:innerWidth,g=i.visualViewport?i.visualViewport.height:innerHeight,v=window.scrollX||pageXOffset,w=window.scrollY||pageYOffset,b=t.getBoundingClientRect(),y=b.height,W=b.width,H=b.top,E=b.right,M=b.bottom,T=b.left,V="start"===c||"nearest"===c?H:"end"===c?M:H+y/2,k="center"===u?T+W/2:"end"===u?E:T,x=[],I=0;I<h.length;I++){var C=h[I],O=C.getBoundingClientRect(),j=O.height,B=O.width,D=O.top,L=O.right,R=O.bottom,X=O.left;if("if-needed"===l&&H>=0&&T>=0&&M<=g&&E<=m&&H>=D&&M<=R&&T>=X&&E<=L)return x;var Y=getComputedStyle(C),S=parseInt(Y.borderLeftWidth,10),q=parseInt(Y.borderTopWidth,10),z=parseInt(Y.borderRightWidth,10),A=parseInt(Y.borderBottomWidth,10),F=0,G=0,J="offsetWidth"in C?C.offsetWidth-C.clientWidth-S-z:0,K="offsetHeight"in C?C.offsetHeight-C.clientHeight-q-A:0;if(s===C)F="start"===c?V:"end"===c?V-g:"nearest"===c?o(w,w+g,g,q,A,w+V,w+V+y,y):V-g/2,G="start"===u?k:"center"===u?k-m/2:"end"===u?k-m:o(v,v+m,m,S,z,v+k,v+k+W,W),F=Math.max(0,F+w),G=Math.max(0,G+v);else{F="start"===c?V-D-q:"end"===c?V-R+A+K:"nearest"===c?o(D,R,j,q,A+K,V,V+y,y):V-(D+j/2)+K/2,G="start"===u?k-X-S:"center"===u?k-(X+B/2)+J/2:"end"===u?k-L+z+J:o(X,L,B,S,z+J,k,k+W,W);var N=C.scrollLeft,P=C.scrollTop;V+=P-(F=Math.max(0,Math.min(P+F,C.scrollHeight-j+K))),k+=N-(G=Math.max(0,Math.min(N+G,C.scrollWidth-B+J)))}x.push({el:C,top:F,left:G})}return x}function i(e){return e===Object(e)&&0!==Object.keys(e).length}return function(e,t){var n=e.isConnected||e.ownerDocument.documentElement.contains(e);if(i(t)&&"function"==typeof t.behavior)return t.behavior(n?r(e,t):[]);if(n){var o=function(e){return!1===e?{block:"end",inline:"nearest"}:i(e)?e:{block:"start",inline:"nearest"}}(t);return function(e,t){void 0===t&&(t="auto");var n="scrollBehavior"in document.body.style;e.forEach((function(e){var o=e.el,r=e.top,i=e.left;o.scroll&&n?o.scroll({top:r,left:i,behavior:t}):(o.scrollTop=r,o.scrollLeft=i)}))}(r(e,o),o.behavior)}}}));