UNPKG

3.26 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,u){return i<e&&l>t||i>e&&l<t?0:i<=e&&u<=n||l>=t&&u>=n?i-e-o:l>t&&u<n||i<e&&u>n?l-t+r:0}function r(t,r){var i=window,l=r.scrollMode,u=r.block,c=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"===u||"nearest"===u?H:"end"===u?M:H+y/2,k="center"===c?T+W/2:"end"===c?E:T,x=[],I=0;I<h.length;I++){var O=h[I],j=O.getBoundingClientRect(),B=j.height,C=j.width,D=j.top,L=j.right,R=j.bottom,X=j.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(O),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 O?O.offsetWidth-O.clientWidth-S-z:0,K="offsetHeight"in O?O.offsetHeight-O.clientHeight-q-A:0;if(s===O)F="start"===u?V:"end"===u?V-g:"nearest"===u?o(w,w+g,g,q,A,w+V,w+V+y,y):V-g/2,G="start"===c?k:"center"===c?k-m/2:"end"===c?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"===u?V-D-q:"end"===u?V-R+A+K:"nearest"===u?o(D,R,B,q,A+K,V,V+y,y):V-(D+B/2)+K/2,G="start"===c?k-X-S:"center"===c?k-(X+C/2)+J/2:"end"===c?k-L+z+J:o(X,L,C,S,z+J,k,k+W,W);var N=O.scrollLeft,P=O.scrollTop;V+=P-(F=Math.max(0,Math.min(P+F,O.scrollHeight-B+K))),k+=N-(G=Math.max(0,Math.min(N+G,O.scrollWidth-C+J)))}x.push({el:O,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.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)}}}));