Files
Webklar.com/node_modules/lenis/dist/lenis-snap.min.js
Basilosaurusrex f027651f9b main repo
2025-11-24 18:09:40 +01:00

1 line
5.7 KiB
JavaScript

function e(e,t){let i;return function(...s){let n=this;clearTimeout(i),i=setTimeout(()=>{i=void 0,e.apply(n,s)},t)}}function t(e){"sticky"===getComputedStyle(e).position&&(e.style.setProperty("position","static"),e.dataset.sticky="true"),e.offsetParent&&t(e.offsetParent)}function i(e){"true"===e?.dataset?.sticky&&(e.style.removeProperty("position"),delete e.dataset.sticky),e.offsetParent&&i(e.offsetParent)}function s(e,t=0){const i=t+e.offsetTop;return e.offsetParent?s(e.offsetParent,i):i}function n(e,t=0){const i=t+e.offsetLeft;return e.offsetParent?n(e.offsetParent,i):i}function o(e,t=0){const i=t+e.scrollTop;return e.offsetParent?o(e.offsetParent,i):i+window.scrollY}function r(e,t=0){const i=t+e.scrollLeft;return e.offsetParent?r(e.offsetParent,i):i+window.scrollX}var h=class{element;options;align;rect={};wrapperResizeObserver;resizeObserver;debouncedWrapperResize;constructor(t,{align:i=["start"],ignoreSticky:s=!0,ignoreTransform:n=!1}={}){this.element=t,this.options={align:i,ignoreSticky:s,ignoreTransform:n},this.align=[i].flat(),this.debouncedWrapperResize=e(this.onWrapperResize,500),this.wrapperResizeObserver=new ResizeObserver(this.debouncedWrapperResize),this.wrapperResizeObserver.observe(document.body),this.onWrapperResize(),this.resizeObserver=new ResizeObserver(this.onResize),this.resizeObserver.observe(this.element),this.setRect({width:this.element.offsetWidth,height:this.element.offsetHeight})}destroy(){this.wrapperResizeObserver.disconnect(),this.resizeObserver.disconnect()}setRect({top:e,left:t,width:i,height:s,element:n}={}){e=e??this.rect.top,t=t??this.rect.left,i=i??this.rect.width,s=s??this.rect.height,n=n??this.rect.element,e===this.rect.top&&t===this.rect.left&&i===this.rect.width&&s===this.rect.height&&n===this.rect.element||(this.rect.top=e,this.rect.y=e,this.rect.width=i,this.rect.height=s,this.rect.left=t,this.rect.x=t,this.rect.bottom=e+s,this.rect.right=t+i)}onWrapperResize=()=>{let e,h;if(this.options.ignoreSticky&&t(this.element),this.options.ignoreTransform)e=s(this.element),h=n(this.element);else{const t=this.element.getBoundingClientRect();e=t.top+o(this.element),h=t.left+r(this.element)}this.options.ignoreSticky&&i(this.element),this.setRect({top:e,left:h})};onResize=([e])=>{if(!e?.borderBoxSize[0])return;const t=e.borderBoxSize[0].inlineSize,i=e.borderBoxSize[0].blockSize;this.setRect({width:t,height:i})}},a=0;function p(){return a++}globalThis.Snap=class{constructor(t,{type:i="proximity",lerp:s,easing:n,duration:o,distanceThreshold:r="50%",debounce:h=500,onSnapStart:a,onSnapComplete:p}={}){this.lenis=t,this.options={type:i,lerp:s,easing:n,duration:o,distanceThreshold:r,debounce:h,onSnapStart:a,onSnapComplete:p},this.onWindowResize(),window.addEventListener("resize",this.onWindowResize,!1),this.onSnapDebounced=e(this.onSnap,this.options.debounce),this.lenis.on("virtual-scroll",this.onSnapDebounced)}options;elements=new Map;snaps=new Map;viewport={width:window.innerWidth,height:window.innerHeight};isStopped=!1;onSnapDebounced;currentSnapIndex;destroy(){this.lenis.off("virtual-scroll",this.onSnapDebounced),window.removeEventListener("resize",this.onWindowResize,!1),this.elements.forEach(e=>e.destroy())}start(){this.isStopped=!1}stop(){this.isStopped=!0}add(e){const t=p();return this.snaps.set(t,{value:e}),()=>this.snaps.delete(t)}addElement(e,t={}){const i=p();return this.elements.set(i,new h(e,t)),()=>this.elements.delete(i)}addElements(e,t={}){const i=e.map(e=>this.addElement(e,t));return()=>{i.forEach(e=>{e()})}}onWindowResize=()=>{this.viewport.width=window.innerWidth,this.viewport.height=window.innerHeight};computeSnaps=()=>{const{isHorizontal:e}=this.lenis;let t=[...this.snaps.values()];return this.elements.forEach(({rect:i,align:s})=>{let n;s.forEach(s=>{"start"===s?n=i.top:"center"===s?n=e?i.left+i.width/2-this.viewport.width/2:i.top+i.height/2-this.viewport.height/2:"end"===s&&(n=e?i.left+i.width-this.viewport.width:i.top+i.height-this.viewport.height),"number"==typeof n&&t.push({value:Math.ceil(n)})})}),t=t.sort((e,t)=>Math.abs(e.value)-Math.abs(t.value)),t};previous(){this.goTo((this.currentSnapIndex??0)-1)}next(){this.goTo((this.currentSnapIndex??0)+1)}goTo(e){const t=this.computeSnaps();if(0===t.length)return;this.currentSnapIndex=Math.max(0,Math.min(e,t.length-1));const i=t[this.currentSnapIndex];void 0!==i&&this.lenis.scrollTo(i.value,{duration:this.options.duration,easing:this.options.easing,lerp:this.options.lerp,lock:"lock"===this.options.type,userData:{initiator:"snap"},onStart:()=>{this.options.onSnapStart?.({index:this.currentSnapIndex,...i})},onComplete:()=>{this.options.onSnapComplete?.({index:this.currentSnapIndex,...i})}})}get distanceThreshold(){let e=1/0;if("mandatory"===this.options.type)return 1/0;const{isHorizontal:t}=this.lenis,i=t?"width":"height";return e="string"==typeof this.options.distanceThreshold&&this.options.distanceThreshold.endsWith("%")?Number(this.options.distanceThreshold.replace("%",""))/100*this.viewport[i]:"number"==typeof this.options.distanceThreshold?this.options.distanceThreshold:this.viewport[i],e}onSnap=e=>{if(this.isStopped)return;if("touchmove"===e.event.type)return;if("lock"===this.options.type&&"snap"===this.lenis.userData?.initiator)return;let{scroll:t,isHorizontal:i}=this.lenis;const s=i?e.deltaX:e.deltaY;t=Math.ceil(this.lenis.scroll+s);const n=this.computeSnaps();if(0===n.length)return;let o;const r=n.findLastIndex(({value:e})=>e<t),h=n.findIndex(({value:e})=>e>t);if("lock"===this.options.type)s>0?o=h:s<0&&(o=r);else{const e=n[r],i=e?Math.abs(t-e.value):1/0,s=n[h];o=i<(s?Math.abs(t-s.value):1/0)?r:h}if(void 0===o)return;if(-1===o)return;o=Math.max(0,Math.min(o,n.length-1));const a=n[o];Math.abs(t-a.value)<=this.distanceThreshold&&this.goTo(o)};resize(){this.elements.forEach(e=>e.onWrapperResize())}};//# sourceMappingURL=lenis-snap.min.js.map