vhAstro-Theme/public/assets/js/smoothscroll.min.js
2025-03-12 15:56:51 +08:00

1 line
7.9 KiB
JavaScript

!function(){var e,t,o,n,r={frameRate:150,animationTime:400,stepSize:100,pulseAlgorithm:!0,pulseScale:4,pulseNormalize:1,accelerationDelta:50,accelerationMax:3,keyboardSupport:!0,arrowScroll:50,fixedBackground:!0,excluded:""},a=r,i=!1,l=!1,c={x:0,y:0},s=!1,u=document.documentElement,d=[];if("userAgentData"in navigator)var f=/^Mac/i.test(window.navigator.userAgentData.platform);else f=/^Mac/i.test(window.navigator.platform);var w={left:37,up:38,right:39,down:40,spacebar:32,pageup:33,pagedown:34,end:35,home:36},m={37:1,38:1,39:1,40:1};function h(){if(!s&&document.body){s=!0;var n=document.body,r=document.documentElement,c=window.innerHeight,d=n.scrollHeight;if(u=document.compatMode.indexOf("CSS")>=0?r:n,e=n,a.keyboardSupport&&Y("keydown",D),top!=self)l=!0;else if(U&&d>c&&(n.offsetHeight<=c||r.offsetHeight<=c)){var f,w=document.createElement("div");w.style.cssText="position:absolute; z-index:-10000; top:0; left:0; right:0; height:"+u.scrollHeight+"px",document.body.appendChild(w),o=function(){f||(f=setTimeout(function(){i||(w.style.height="0",w.style.height=u.scrollHeight+"px",f=null)},500))},setTimeout(o,10),Y("resize",o);if((t=new q(o)).observe(n,{attributes:!0,childList:!0,characterData:!1}),u.offsetHeight<=c){var m=document.createElement("div");m.style.clear="both",n.appendChild(m)}}a.fixedBackground||i||(n.style.backgroundAttachment="scroll",r.style.backgroundAttachment="scroll")}}var v=[],g=!1,p=Date.now();function b(e,t,o){var n,r;if(n=(n=t)>0?1:-1,r=(r=o)>0?1:-1,(c.x!==n||c.y!==r)&&(c.x=n,c.y=r,v=[],p=0),1!=a.accelerationMax){var i=Date.now()-p;if(i<a.accelerationDelta){var l=(1+50/i)/2;l>1&&(l=Math.min(l,a.accelerationMax),t*=l,o*=l)}p=Date.now()}if(v.push({x:t,y:o,lastX:t<0?.99:-.99,lastY:o<0?.99:-.99,start:Date.now()}),!g){var s=V(),u=e===s||e===document.body;null==e.$scrollBehavior&&function(e){var t=A(e);if(null==T[t]){var o=getComputedStyle(e,"")["scroll-behavior"];T[t]="smooth"==o}return T[t]}(e)&&(e.$scrollBehavior=e.style.scrollBehavior,e.style.scrollBehavior="auto");var d=function(n){for(var r=Date.now(),i=0,l=0,c=0;c<v.length;c++){var s=v[c],f=r-s.start,w=f>=a.animationTime,m=w?1:f/a.animationTime;a.pulseAlgorithm&&(m=I(m));var h=s.x*m-s.lastX>>0,p=s.y*m-s.lastY>>0;i+=h,l+=p,s.lastX+=h,s.lastY+=p,w&&(v.splice(c,1),c--)}u?window.scrollBy(i,l):(i&&(e.scrollLeft+=i),l&&(e.scrollTop+=l)),t||o||(v=[]),v.length?R(d,e,1e3/a.frameRate+1):(g=!1,null!=e.$scrollBehavior&&(e.style.scrollBehavior=e.$scrollBehavior,e.$scrollBehavior=null))};R(d,e,0),g=!0}}function y(t){s||h();var o=t.target;if(t.defaultPrevented||t.ctrlKey)return!0;if(N(e,"embed")||N(o,"embed")&&/\.pdf/i.test(o.src)||N(e,"object")||o.shadowRoot)return!0;var r=-t.wheelDeltaX||t.deltaX||0,i=-t.wheelDeltaY||t.deltaY||0;f&&(t.wheelDeltaX&&P(t.wheelDeltaX,120)&&(r=t.wheelDeltaX/Math.abs(t.wheelDeltaX)*-120),t.wheelDeltaY&&P(t.wheelDeltaY,120)&&(i=t.wheelDeltaY/Math.abs(t.wheelDeltaY)*-120)),r||i||(i=-t.wheelDelta||0),1===t.deltaMode&&(r*=40,i*=40);var c=z(o);return c?!!function(e){if(!e)return;d.length||(d=[e,e,e]);e=Math.abs(e),d.push(e),d.shift(),clearTimeout(n),n=setTimeout(function(){try{localStorage.SS_deltaBuffer=d.join(",")}catch(e){}},1e3);var t=e>120&&$(e),o=!$(120)&&!$(100)&&!t;return e<50||o}(i)||(Math.abs(r)>1.2&&(r*=a.stepSize/120),Math.abs(i)>1.2&&(i*=a.stepSize/120),b(c,r,i),t.preventDefault(),void B()):!l||!W||(Object.defineProperty(t,"target",{value:window.frameElement}),parent.wheel(t))}function D(t){var o=t.target,n=t.ctrlKey||t.altKey||t.metaKey||t.shiftKey&&t.keyCode!==w.spacebar;document.body.contains(e)||(e=document.activeElement);var r=/^(button|submit|radio|checkbox|file|color|image)$/i;if(t.defaultPrevented||/^(textarea|select|embed|object)$/i.test(o.nodeName)||N(o,"input")&&!r.test(o.type)||N(e,"video")||function(e){var t=e.target,o=!1;if(-1!=document.URL.indexOf("www.youtube.com/watch"))do{if(o=t.classList&&t.classList.contains("html5-video-controls"))break}while(t=t.parentNode);return o}(t)||o.isContentEditable||n)return!0;if((N(o,"button")||N(o,"input")&&r.test(o.type))&&t.keyCode===w.spacebar)return!0;if(N(o,"input")&&"radio"==o.type&&m[t.keyCode])return!0;var i=0,c=0,s=z(e);if(!s)return!l||!W||parent.keydown(t);var u=s.clientHeight;switch(s==document.body&&(u=window.innerHeight),t.keyCode){case w.up:c=-a.arrowScroll;break;case w.down:c=a.arrowScroll;break;case w.spacebar:c=-(t.shiftKey?1:-1)*u*.9;break;case w.pageup:c=.9*-u;break;case w.pagedown:c=.9*u;break;case w.home:s==document.body&&document.scrollingElement&&(s=document.scrollingElement),c=-s.scrollTop;break;case w.end:var d=s.scrollHeight-s.scrollTop-u;c=d>0?d+10:0;break;case w.left:i=-a.arrowScroll;break;case w.right:i=a.arrowScroll;break;default:return!0}b(s,i,c),t.preventDefault(),B()}function S(t){e=t.target}var x,k,A=(x=0,function(e){return e.uniqueID||(e.uniqueID=x++)}),M={},E={},T={};function B(){clearTimeout(k),k=setInterval(function(){M=E=T={}},1e3)}function C(e,t,o){for(var n=o?M:E,r=e.length;r--;)n[A(e[r])]=t;return t}function H(e,t){return(t?M:E)[A(e)]}function z(e){var t=[],o=document.body,n=u.scrollHeight;do{var r=H(e,!1);if(r)return C(t,r);if(t.push(e),n===e.scrollHeight){var a=O(u)&&O(o)||X(u);if(l&&L(u)||!l&&a)return C(t,V())}else if(L(e)&&X(e))return C(t,e)}while(e=e.parentElement)}function L(e){return e.clientHeight+10<e.scrollHeight}function O(e){return"hidden"!==getComputedStyle(e,"").getPropertyValue("overflow-y")}function X(e){var t=getComputedStyle(e,"").getPropertyValue("overflow-y");return"scroll"===t||"auto"===t}function Y(e,t,o){window.addEventListener(e,t,o||!1)}function K(e,t,o){window.removeEventListener(e,t,o||!1)}function N(e,t){return e&&(e.nodeName||"").toLowerCase()===t.toLowerCase()}if(window.localStorage&&localStorage.SS_deltaBuffer)try{d=localStorage.SS_deltaBuffer.split(",")}catch(e){}function P(e,t){return Math.floor(e/t)==e/t}function $(e){return P(d[0],e)&&P(d[1],e)&&P(d[2],e)}var j,R=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(e,t,o){window.setTimeout(e,o||1e3/60)},q=window.MutationObserver||window.WebKitMutationObserver||window.MozMutationObserver,V=(j=document.scrollingElement,function(){if(!j){var e=document.createElement("div");e.style.cssText="height:10000px;width:1px;",document.body.appendChild(e);var t=document.body.scrollTop;document.documentElement.scrollTop,window.scrollBy(0,3),j=document.body.scrollTop!=t?document.body:document.documentElement,window.scrollBy(0,-3),document.body.removeChild(e)}return j});function F(e){var t,o;return(e*=a.pulseScale)<1?t=e-(1-Math.exp(-e)):(e-=1,t=(o=Math.exp(-1))+(1-Math.exp(-e))*(1-o)),t*a.pulseNormalize}function I(e){return e>=1?1:e<=0?0:(1==a.pulseNormalize&&(a.pulseNormalize/=F(1)),F(e))}if("userAgentData"in navigator){var _=!1,U=!1;for(let e=0;e<window.navigator.userAgentData.brands.length;e++)if("Safari"===window.navigator.userAgentData.brands[e].brand){_=!0;if(window.navigator.userAgentData.brands[e].version<=9)U=!0}}else U=(_=/safari/i.test(window.navigator.userAgent))&&(/Version\/8/i.test(window.navigator.userAgent)||/Version\/9/i.test(window.navigator.userAgent));if("userAgentData"in navigator){var W=!1;for(let e=0;e<window.navigator.userAgentData.brands.length;e++)if("Chromium"===window.navigator.userAgentData.brands[e].brand)W=!0}else W=/chrome/i.test(window.navigator.userAgent);if("userAgentData"in navigator)var G=window.navigator.userAgentData.mobile;else G=/mobile/i.test(window.navigator.userAgent);var J=(W||_)&&!G,Q=!1;try{window.addEventListener("test",null,Object.defineProperty({},"passive",{get:function(){Q=!0}}))}catch(e){}var Z=!!Q&&{passive:!1},ee="onwheel"in document.createElement("div")?"wheel":"mousewheel";function te(e){for(var t in e)r.hasOwnProperty(t)&&(a[t]=e[t])}ee&&J&&(Y(ee,y,Z),Y("mousedown",S),Y("load",h)),te.destroy=function(){t&&t.disconnect(),K(ee,y),K("mousedown",S),K("keydown",D),K("resize",o),K("load",h)},window.SmoothScrollOptions&&te(window.SmoothScrollOptions),"function"==typeof define&&define.amd?define(function(){return te}):"object"==typeof exports?module.exports=te:window.SmoothScroll=te}();