scroll-into-view-if-needed.min.js 3.17 KB
!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 null!=e&&"object"==typeof 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.parentNode)===s){h.push(p);break}p===document.body&&n(p)&&!n(document.documentElement)||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 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),N=parseInt(Y.borderTopWidth,10),q=parseInt(Y.borderRightWidth,10),z=parseInt(Y.borderBottomWidth,10),A=0,F=0,G="offsetWidth"in O?O.offsetWidth-O.clientWidth-S-q:0,J="offsetHeight"in O?O.offsetHeight-O.clientHeight-N-z:0;if(s===O)A="start"===c?V:"end"===c?V-g:"nearest"===c?o(w,w+g,g,N,z,w+V,w+V+y,y):V-g/2,F="start"===u?k:"center"===u?k-m/2:"end"===u?k-m:o(v,v+m,m,S,q,v+k,v+k+W,W),A=Math.max(0,A+w),F=Math.max(0,F+v);else{A="start"===c?V-D-N:"end"===c?V-R+z+J:"nearest"===c?o(D,R,B,N,z+J,V,V+y,y):V-(D+B/2)+J/2,F="start"===u?k-X-S:"center"===u?k-(X+C/2)+G/2:"end"===u?k-L+q+G:o(X,L,C,S,q+G,k,k+W,W);var K=O.scrollLeft,P=O.scrollTop;V+=P-(A=Math.max(0,Math.min(P+A,O.scrollHeight-B+J))),k+=K-(F=Math.max(0,Math.min(K+F,O.scrollWidth-C+G)))}x.push({el:O,top:A,left:F})}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)}}}));