-
Notifications
You must be signed in to change notification settings - Fork 59
/
zenscroll5-min.js
1 lines (1 loc) · 3.15 KB
/
zenscroll5-min.js
1
const t=function(t){return t&&"smooth"===window.getComputedStyle(t)["scroll-behavior"]};export const makeScroller=function(e,o,n){let i;o=o||999,n||0===n||(n=9);const s=t=>i=t,r=()=>{clearTimeout(i),s(0)},c=t=>Math.min(Math.max(0,e.getTopOf(t)-n),e.getHeight()-window.innerHeight-n),l=function(n,i){return r(),new Promise((c=>{if(0===i||i&&i<0||t(e.body))e.toY(n),c();else{const t=e.getY(),l=Math.max(0,n)-t,a=(new Date).getTime();i=i||Math.min(Math.abs(l),o),function o(){s(setTimeout((()=>{const n=Math.min(1,((new Date).getTime()-a)/i),s=Math.max(0,Math.floor(t+l*(n<.5?2*n*n:n*(4-2*n)-1)));e.toY(s),n<1&&e.getHeight()+s<e.body.scrollHeight?o():(setTimeout(r,99),c())}),9))}()}}))},a=function(t,e){return l(c(t),e)};return{setup:function(t,e){return(0===t||t)&&(o=t),(0===e||e)&&(n=e),{defaultDuration:o,edgeOffset:n}},to:a,toY:l,intoView:function(t,o){const i=t.getBoundingClientRect().height,s=e.getTopOf(t)+i,r=e.getHeight(),g=e.getY(),m=g+r;return c(t)<g||i+n>r?a(t,o):s+n>m?l(s-r+n,o):new Promise((t=>t()))},center:function(t,o,n){return l(Math.max(0,e.getTopOf(t)-e.getHeight()/2+(n||t.getBoundingClientRect().height/2)),o)},stop:r,moving:()=>!!i,getY:e.getY,getTopOf:e.getTopOf}};const e=()=>window.scrollY||document.documentElement.scrollTop,o=makeScroller({body:document.scrollingElement||document.body,toY:t=>window.scrollTo(0,t),getY:e,getHeight:()=>document.documentElement.clientHeight||window.innerHeight,getTopOf:t=>t.getBoundingClientRect().top+e()-document.documentElement.offsetTop});export const createScroller=function(t,e,o){return makeScroller({body:t,toY:e=>t.scrollTop=e,getY:()=>t.scrollTop,getHeight:()=>Math.min(t.clientHeight,document.documentElement.clientHeight||window.innerHeight),getTopOf:t=>t.offsetTop},e,o)};if(!window.noZensmooth&&!t(document.scrollingElement)){const t="scrollRestoration"in history;let e;t&&(history.scrollRestoration="auto"),window.addEventListener("load",(()=>{t&&(setTimeout((()=>history.scrollRestoration="manual"),9),window.addEventListener("popstate",(t=>{t.state&&"zenscrollY"in t.state&&o.toY(t.state.zenscrollY,location.pathname===e?null:0)}))),location.hash&&setTimeout((()=>{const t=o.setup().edgeOffset;if(t){const e=location.href.split("#")[1];let n=document.getElementById(e);if(!n){const t=document.getElementsByName(e);for(const e of t)if("A"===e.tagName){n=e;break}}if(n){const e=Math.max(0,o.getTopOf(n)-t),i=o.getY()-e;0<=i&&i<9&&window.scrollTo(0,e)}}}),9)}));const n=new RegExp(/(^|\\s)noZensmooth(\\s|$)/i);window.addEventListener("click",(i=>{if(window.noZensmooth||0!==i.button||i.shiftKey||i.metaKey||i.ctrlKey||i.altKey)return;let s=i.target;for(;s&&"A"!==s.tagName;)s=s.parentNode;if(!s)return;if(t){const t=history.state||{};t.zenscrollY=o.getY();try{history.replaceState(t,"")}catch(c){}}const r=s.getAttribute("href")||"";if(0===r.indexOf("#")&&!n.test(s.className)){let t=0;const n=document.getElementById(r.substring(1));if("#"!==r){if(!n)return;t=o.getTopOf(n)}i.preventDefault();let s=()=>location.assign(r);const l=o.setup().edgeOffset;l&&(t=Math.max(0,t-l),e=location.pathname,s=()=>{const t=history.state||{};t.zenscrollY=o.getY();try{history.pushState(t,"",r)}catch(c){}}),o.toY(t,null).then(s)}}),{capture:!0})}export default o;