sticky-scroll-trigger 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -202,12 +202,19 @@ ScrollTrigger.create(
|
|
|
202
202
|
sticky.refresh();
|
|
203
203
|
```
|
|
204
204
|
|
|
205
|
-
| option | default | description
|
|
206
|
-
| ------------ | ----------- |
|
|
207
|
-
| `trigger` | (required) | The element to pin and show
|
|
208
|
-
| `
|
|
209
|
-
| `
|
|
210
|
-
| `
|
|
205
|
+
| option | default | description |
|
|
206
|
+
| ------------ | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
207
|
+
| `trigger` | (required) | The element to pin and show |
|
|
208
|
+
| `start` | `'top top'` | Where `trigger` lands in the viewport while pinned, in [position syntax](#position-syntax), so the element's own side counts too (`'bottom bottom'` rests it against the viewport's bottom edge) |
|
|
209
|
+
| `top` | `0` | A px distance from the viewport's top edge, as a plain number. `top: 20` is `start: 'top 20px'` |
|
|
210
|
+
| `endTrigger` | (required) | Reference element for releasing the pin |
|
|
211
|
+
| `end` | `'top top'` | Which clause of `endTrigger` releases the pin (standard GSAP [position syntax](#position-syntax)) |
|
|
212
|
+
|
|
213
|
+
Here `start` means only "where it sits while pinned", not "when pinning begins" as it does for the other methods. Pinning begins the moment `trigger` reaches that position naturally, which is CSS's decision rather than a separate number to set, so a pin's position and the scroll position it engages at are the same setting.
|
|
214
|
+
|
|
215
|
+
That's why `start` throws on an [absolute scroll position](#absolute-scroll-position) (`start: 20`, which GSAP reads as scroll position 20) instead of quietly treating it as a px distance. It's also why `top` is its own option: with `start`'s bare-number slot reserved for GSAP's meaning, a pin would otherwise have no way to say "20px below the top of the viewport" short of the string `'top 20px'`. `end` does take an absolute scroll position (`end: 3000` releases at scroll 3000), since a release point on the scroll axis is a real thing to ask for.
|
|
216
|
+
|
|
217
|
+
Every clause spelling resolves exactly as it does elsewhere, including the one-token forms that name the element's own side: `'top 20px'` sits 20px _below_ the viewport's top edge, while `'20px'` sits 20px _above_ it.
|
|
211
218
|
|
|
212
219
|
Internally, the element wrapping `trigger` renders beyond its own section's bounds, into the visual area of following elements. Make sure the ancestor section containing `trigger` isn't hidden behind a later section in DOM order (e.g. via `position: relative; z-index: ...`).
|
|
213
220
|
|
|
@@ -375,6 +382,8 @@ If the entire value is just a number (a plain JS number, or a string that's noth
|
|
|
375
382
|
|
|
376
383
|
> [`createOverlapScroll`](#createoverlapscrolloptions)'s `start` doesn't support absolute scroll positions: a cover layer's sticky position is always computed relative to its own wrapper (see the repository's `ARCHITECTURE.md`), which has no equivalent for one. It throws instead; use a position clause.
|
|
377
384
|
|
|
385
|
+
> [`createStickyPin`](#createstickypinoptions)'s `start` throws on one too, for a different reason than the cover layer above: the scroll position a pin engages at follows from where it sits, so an absolute value could only be honored by inverting it back into a viewport position the caller never chose (a trigger 2000px down the document asked to engage at scroll 500 would sit 1500px below the viewport's top edge, off-screen). Its `end` does take one.
|
|
386
|
+
|
|
378
387
|
## End syntax
|
|
379
388
|
|
|
380
389
|
`end` auto-detects between an absolute scroll position, dwell distance, GSAP's `'max'` keyword, and a position clause.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @license
|
|
3
|
-
* StickyScrollTrigger v0.
|
|
3
|
+
* StickyScrollTrigger v0.4.0
|
|
4
4
|
* Copyright (c) 2026 Kuninori Ogino
|
|
5
5
|
* Released under the MIT License
|
|
6
6
|
* https://github.com/kuninori-ogino/sticky-scroll-trigger
|
|
@@ -24,6 +24,7 @@ export declare interface CreateResolvedTriggerOptions extends PassThroughVars {
|
|
|
24
24
|
|
|
25
25
|
export declare interface CreateStickyPinOptions extends PassThroughVars {
|
|
26
26
|
trigger: string | HTMLElement;
|
|
27
|
+
start?: PositionInput;
|
|
27
28
|
top?: number | (() => number);
|
|
28
29
|
endTrigger: string | HTMLElement;
|
|
29
30
|
end?: PositionInput;
|
|
@@ -52,7 +53,7 @@ declare class StickyScrollTrigger {
|
|
|
52
53
|
refresh(): void;
|
|
53
54
|
createStickyTrigger({ trigger: triggerInput, start, end, endTrigger: endTriggerInput, onKill, ...rest }: CreateStickyTriggerOptions): ScrollTrigger.Vars;
|
|
54
55
|
createOverlapScroll({ trigger: triggerInput, cover, start, end, endTrigger: endTriggerInput, onKill, ...rest }: CreateOverlapScrollOptions): ScrollTrigger.Vars;
|
|
55
|
-
createStickyPin({ trigger: triggerInput, top, endTrigger: endTriggerInput, end, onKill, ...rest }: CreateStickyPinOptions): ScrollTrigger.Vars;
|
|
56
|
+
createStickyPin({ trigger: triggerInput, start, top, endTrigger: endTriggerInput, end, onKill, ...rest }: CreateStickyPinOptions): ScrollTrigger.Vars;
|
|
56
57
|
resolveScrollPosition(elementInput: string | HTMLElement, position: PositionInput): number;
|
|
57
58
|
static getScrollTop(elementInput: string | HTMLElement, instances: readonly StickyScrollTrigger[]): number;
|
|
58
59
|
createResolvedTrigger({ trigger: triggerInput, start, end, endTrigger: endTriggerInput, ...rest }: CreateResolvedTriggerOptions): ScrollTrigger.Vars;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @license
|
|
3
|
-
* StickyScrollTrigger v0.
|
|
3
|
+
* StickyScrollTrigger v0.4.0
|
|
4
4
|
* Copyright (c) 2026 Kuninori Ogino
|
|
5
5
|
* Released under the MIT License
|
|
6
6
|
* https://github.com/kuninori-ogino/sticky-scroll-trigger
|
|
@@ -509,6 +509,13 @@ var StickyScrollTrigger = (function() {
|
|
|
509
509
|
endTriggerHeight: end.mode === "clause" ? layer.endTrigger.offsetHeight : 0
|
|
510
510
|
};
|
|
511
511
|
};
|
|
512
|
+
const topToStartClause = (top) => {
|
|
513
|
+
const toClause = (value) => {
|
|
514
|
+
if (!Number.isFinite(value)) throw new Error(`StickyScrollTrigger: createStickyPin's top must be a finite number, got ${value}.`);
|
|
515
|
+
return `top ${value}px`;
|
|
516
|
+
};
|
|
517
|
+
return typeof top === "function" ? () => toClause(top()) : toClause(top);
|
|
518
|
+
};
|
|
512
519
|
const hasDuplicateTrigger = (list, trigger) => list.some((existing) => existing.trigger === trigger);
|
|
513
520
|
var StickyScrollTrigger = class {
|
|
514
521
|
#rootElement;
|
|
@@ -586,6 +593,13 @@ var StickyScrollTrigger = (function() {
|
|
|
586
593
|
#refreshPins(viewportHeight) {
|
|
587
594
|
if (!this.#pinLayers.length) return;
|
|
588
595
|
const restoreSceneCoverStickyState = this.#resetSceneCoverStickyState();
|
|
596
|
+
try {
|
|
597
|
+
this.#refreshPinLayers(viewportHeight);
|
|
598
|
+
} finally {
|
|
599
|
+
restoreSceneCoverStickyState();
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
#refreshPinLayers(viewportHeight) {
|
|
589
603
|
this.#pinLayers.forEach((layer) => {
|
|
590
604
|
resetStickyPosition(layer.trigger);
|
|
591
605
|
if (layer.inner) layer.inner.style.height = "";
|
|
@@ -594,14 +608,15 @@ var StickyScrollTrigger = (function() {
|
|
|
594
608
|
if (!layer.inner) return;
|
|
595
609
|
const resolvedEnd = resolveMaybeFn(layer.end);
|
|
596
610
|
if (isMaxFormat(resolvedEnd)) throw new Error(`StickyScrollTrigger: createStickyPin's end "${resolvedEnd}" uses GSAP's 'max' keyword, which isn't supported here (the pin's own spacer height contributes to the document's max scroll position, so the spacer height would depend on itself and never converge). Use a plain position clause such as 'top top' against endTrigger instead.`);
|
|
597
|
-
const
|
|
611
|
+
const resolvedStart = resolveMaybeFn(layer.start);
|
|
598
612
|
const triggerTop = documentTop(layer.trigger);
|
|
599
613
|
const triggerHeight = layer.trigger.offsetHeight;
|
|
614
|
+
if (isAbsoluteFormat(resolvedStart)) throw new Error(`StickyScrollTrigger: createStickyPin's start "${resolvedStart}" is an absolute scroll position (GSAP treats any bare-number start this way). A pin can't act on one: it engages when position:sticky engages, so start says where trigger sits in the viewport while pinned. Use the top option (top: ${resolvedStart}) for a px distance from the viewport's top edge, or a position clause such as 'top top'.`);
|
|
615
|
+
const topPx = resolveAnchorTop(resolvedStart, triggerHeight, viewportHeight);
|
|
600
616
|
const height = (isAbsoluteFormat(resolvedEnd) ? resolveAbsolute(resolvedEnd) : documentTop(layer.endTrigger) - resolveAnchorTop(resolvedEnd, layer.endTrigger.offsetHeight, viewportHeight)) - triggerTop + topPx + triggerHeight;
|
|
601
617
|
applyStickyPosition(layer.trigger, topPx);
|
|
602
618
|
layer.inner.style.height = `${Math.max(0, height)}px`;
|
|
603
619
|
});
|
|
604
|
-
restoreSceneCoverStickyState();
|
|
605
620
|
}
|
|
606
621
|
#refreshScenesAndCovers(viewportHeight) {
|
|
607
622
|
if (!this.#layers.length) return;
|
|
@@ -725,8 +740,9 @@ var StickyScrollTrigger = (function() {
|
|
|
725
740
|
this.#coverRestoreByLayer.set(layer, restoreCoverStyles);
|
|
726
741
|
return vars;
|
|
727
742
|
}
|
|
728
|
-
createStickyPin({ trigger: triggerInput, top
|
|
743
|
+
createStickyPin({ trigger: triggerInput, start, top, endTrigger: endTriggerInput, end = "top top", onKill, ...rest }) {
|
|
729
744
|
if (this.#destroyed) throw new Error("StickyScrollTrigger: cannot register a new pin after destroy() has been called.");
|
|
745
|
+
if (start !== void 0 && top !== void 0) throw new Error("StickyScrollTrigger: createStickyPin accepts either start or top, not both (top: 20 is start: 'top 20px'). Use top for a plain px distance from the viewport's top edge, start for anything else.");
|
|
730
746
|
const trigger = resolveElement(triggerInput, "createStickyPin");
|
|
731
747
|
const endTrigger = resolveElement(endTriggerInput, "createStickyPin");
|
|
732
748
|
this.#assertTriggerAvailable(this.#pinLayers, trigger, `StickyScrollTrigger: ${describeElement(trigger)} is already registered as a pin trigger.`);
|
|
@@ -735,7 +751,7 @@ var StickyScrollTrigger = (function() {
|
|
|
735
751
|
trigger,
|
|
736
752
|
outer: null,
|
|
737
753
|
inner: null,
|
|
738
|
-
top,
|
|
754
|
+
start: top === void 0 ? start ?? "top top" : topToStartClause(top),
|
|
739
755
|
endTrigger,
|
|
740
756
|
end
|
|
741
757
|
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @license
|
|
3
|
-
* StickyScrollTrigger v0.
|
|
3
|
+
* StickyScrollTrigger v0.4.0
|
|
4
4
|
* Copyright (c) 2026 Kuninori Ogino
|
|
5
5
|
* Released under the MIT License
|
|
6
6
|
* https://github.com/kuninori-ogino/sticky-scroll-trigger
|
|
7
7
|
*/
|
|
8
|
-
var StickyScrollTrigger=(function(){let e=e=>{let t=typeof e==`string`?document.querySelector(e):e;if(!t){let t=typeof e==`string`?`"${e}"`:String(e);throw Error(`StickyScrollTrigger: root ${t} not found`)}return t},t=(e,t)=>{let n=typeof e==`string`?document.querySelector(e):e;if(!n){let n=typeof e==`string`?`"${e}"`:String(e);throw Error(`${t}: element ${n} not found`)}return n},n=(e,n,r)=>n===void 0?e:t(n,r),r=e=>{let t=e.id?`#${e.id}`:``,n=e.classList.length>0?`.${Array.from(e.classList).join(`.`)}`:``;return`<${e.tagName.toLowerCase()}${t}${n}>`},i=e=>{let t=0,n=e;for(;n;){t+=n.offsetTop;let e=n.offsetParent;e&&(t+=e.clientTop),n=e}return t},a=()=>{let e=document.createElement(`div`);e.style.height=`100vh`,e.style.position=`absolute`,document.body.appendChild(e);let t=e.offsetHeight||window.innerHeight;return e.remove(),t},o=e=>{let t=document.documentElement;return Math.max(0,(t.scrollHeight||document.body.scrollHeight)-e)},s=(e,t)=>{let n=e.compareDocumentPosition(t);return n&Node.DOCUMENT_POSITION_FOLLOWING?-1:n&Node.DOCUMENT_POSITION_PRECEDING?1:0},c=e=>{e.style.position=``,e.style.top=``},l=(e,t)=>{e.style.position=`sticky`,e.style.top=`${t}px`},u=e=>{if(!e.parentNode)throw Error(`StickyScrollTrigger: ${r(e)} is not attached to the document`);let t=document.createElement(`div`),n=document.createElement(`div`),i=document.createElement(`div`);return i.setAttribute(`aria-hidden`,`true`),i.style.pointerEvents=`none`,e.parentNode.insertBefore(t,e),t.appendChild(n),n.appendChild(e),t.appendChild(i),{container:t,wrapper:n,padding:i}},d=e=>{let t=e.parentNode;if(!t)throw Error(`createOverlapScroll: trigger ${r(e)} is not attached to the document`);let n=document.createElement(`div`);for(t.insertBefore(n,t.firstChild);n.nextSibling;){let t=n.nextSibling;if(n.appendChild(t),t===e)break}return n},f=e=>{let t=e.parentNode;if(t){for(;e.firstChild;)t.insertBefore(e.firstChild,e);e.remove()}},p=e=>{if(!e.parentNode)throw Error(`createStickyPin: trigger ${r(e)} is not attached to the document`);let t=document.createElement(`div`),n=document.createElement(`div`);return t.style.height=`0`,t.style.contain=`layout`,e.parentNode.insertBefore(t,e),t.appendChild(n),n.appendChild(e),{outer:t,inner:n}},m=(e,t)=>{let n=e.parentNode;c(t),n&&n.insertBefore(t,e),e.remove()},h=new WeakMap,g=e=>{let t=[];for(let n=e;n;n=n.nextElementSibling){if(!(n instanceof HTMLElement))continue;let e=h.get(n);if(e)e.count+=1;else{let e=getComputedStyle(n),t={count:1,position:n.style.position,zIndex:n.style.zIndex};e.position===`static`&&(n.style.position=`relative`),e.zIndex===`auto`&&(n.style.zIndex=`1`),h.set(n,t)}t.push(n)}return()=>{t.forEach(e=>{let t=h.get(e);t&&(--t.count,!(t.count>0)&&(e.style.position=t.position,e.style.zIndex=t.zIndex,h.delete(e)))}),t.length=0}},_=0,v=typeof CSS<`u`&&typeof CSS.supports==`function`&&CSS.supports(`animation-timeline`,`scroll()`),y=(e,t)=>{let n=[],r=[],i=[],a=[];return t.forEach((t,o)=>{let s=`--${e}-c${o}`,c=`${e}-k${o}`;n.push(`@property ${s}{syntax:"<length>";inherits:true;initial-value:0px}`,`@keyframes ${c}{from{${s}:0px}to{${s}:${t.freezeEnd-t.freezeStart}px}}`),r.push(`${c} linear both`),i.push(`scroll(root block)`),a.push(`${t.freezeStart}px ${t.freezeEnd}px`)}),n.push(`@supports (animation-timeline: scroll()) {[data-${e}]{animation:${r.join(`,`)};animation-timeline:${i.join(`,`)};animation-range:${a.join(`,`)}}}`),n.join(``)},b=e=>{let t=`sst${_++}`,n=new WeakMap,r=new Set,i=null,a=null,o=[],c=null,l=()=>{if(!c)return;let e=window.scrollY;o.forEach((n,r)=>{let i=Math.min(Math.max(e-n.freezeStart,0),n.freezeEnd-n.freezeStart);c.style.setProperty(`--${t}-c${r}`,`${i}px`)})};v||window.addEventListener(`scroll`,l,{passive:!0});let u=e=>{let t=n.get(e);e.style.scrollMarginTop=t?t.inline:``},d=()=>{i?.remove(),i=null,a?.removeAttribute(`data-${t}`),a=null,o=[],c=null};return{sync(f,p,m){let h=f.filter(e=>e.freezeEnd>e.freezeStart);if(m===null||!h.length||!p){r.forEach(u),r=new Set,d();return}a!==p&&(a?.removeAttribute(`data-${t}`),a=p,a.setAttribute(`data-${t}`,``)),v||(o=h,c=p,l()),i||(i=document.createElement(`style`),document.head.appendChild(i)),i.textContent=y(t,h);let g=Array.from(e.querySelectorAll(m));g.forEach(e=>{let t=n.get(e);t?r.has(e)&&(e.style.scrollMarginTop=t.inline):n.set(e,{inline:e.style.scrollMarginTop})});let _=h.map((e,n)=>`var(--${t}-c${n}, 0px)`).join(` + `);g.forEach(e=>{let t=parseFloat(getComputedStyle(e).scrollMarginTop)||0,n=0;h.forEach(t=>{s(t.trigger,e)>=0||(n+=t.freezeEnd-t.freezeStart)}),e.style.scrollMarginTop=`calc(${t}px + var(--sst-scroll-margin-top-offset, 0px) + ${_} - ${n}px)`});let b=new Set(g);r.forEach(e=>{b.has(e)||u(e)}),r=b},restore(){v||window.removeEventListener(`scroll`,l),r.forEach(u),r=new Set,d()}}},x=e=>{for(let t=1;t<e.length;t+=1)if(s(e[t-1].trigger,e[t].trigger)>0)return!0;return!1},S=(e,t)=>{t.sort((e,t)=>s(e.trigger,t.trigger));for(let e of t)e.kind===`cover`&&(e.wrapper=d(e.trigger));let n=e,r=null;for(let e of t){if(e.kind!==`scene`)continue;let t=u(n);e.container=t.container,e.wrapper=t.wrapper,e.padding=t.padding,n=t.container,r=t.container}return r},C=(e,t,n)=>{n&&(n.parentNode?.insertBefore(e,n),n.remove());for(let e=t.length-1;e>=0;--e){let n=t[e];n.kind===`cover`&&n.wrapper&&f(n.wrapper)}t.forEach(e=>{e.wrapper=null,e.kind===`scene`&&(e.container=null,e.padding=null)})},w=e=>typeof e==`function`?e():e,T={top:0,center:.5,bottom:1},E=(e,t,n,r)=>{let i=n===`%`?parseFloat(t)/100*r:parseFloat(t);return e?.startsWith(`-`)?-i:i},D=/^(top|center|bottom|(?:\d+\.?\d*|\.\d+))?(%|px)?(?:([+-]=?)(\d+\.?\d*|\.\d+)(%|px)?)?$/,O=(e,t)=>{let n=D.exec(e);if(!n||!n[1]&&!n[3])throw Error(`StickyScrollTrigger: unsupported position clause "${e}"`);let[,r,i,a,o,s]=n;if(r!==void 0&&a&&!a.includes(`=`)){let t=`${r}${i??``}${a}=${o}${s??``}`;throw Error(`StickyScrollTrigger: unsupported position clause "${e}": a signed offset needs an explicit '=' when it follows a keyword or number (GSAP itself requires this too, to tell "${r}" and the offset apart). Did you mean "${t}"?`)}let c=r!==void 0&&r in T,l=i===`%`;return{fraction:c?T[r]:l?parseFloat(r)/100:0,offsetPx:(!c&&!l&&r!==void 0?parseFloat(r):0)+(a?E(a,o,s,t):0)}},k=e=>typeof e==`number`||e.trim()!==``&&!Number.isNaN(Number(e)),A=e=>typeof e==`number`?e:Number(e),j=(e,t,n)=>{let[r,i=`top`]=e.trim().split(/\s+/),a=O(r,t),o=O(i,n);return n*o.fraction+o.offsetPx-(t*a.fraction+a.offsetPx)},M=/^\+=(\d+\.?\d*|\.\d+)(%|px)?$/,N=e=>typeof e==`string`&&M.test(e.trim()),P=(e,t)=>{let n=M.exec(String(e).trim());if(!n)throw Error(`StickyScrollTrigger: unsupported end format "${e}"`);let[,r,i]=n;return E(void 0,r,i,t)},F=/^max(?:([+-]=)(\d+\.?\d*|\.\d+)(%|px)?)?$/,I=e=>typeof e==`string`&&F.test(e.trim()),L=(e,t)=>{let n=F.exec(e.trim());if(!n)throw Error(`StickyScrollTrigger: unsupported max-position format "${e}"`);let[,r,i,a]=n;return r?E(r,i,a,t):0},R=(e,t,n,r,i)=>{let a=0;return e.forEach((e,o)=>{if(o===i||e.triggerTop>=r)return;let s=t[o]===void 0?n?n.paddings[o]:null:t[o];s!=null&&(a+=s)}),a},z=(e,{viewportHeight:t,structureTop:n,documentMaxScroll:r,measureLiveEndTriggerTop:i},a)=>{let o=[],s=[],c=0;return{plans:e.map((l,u)=>{let d=l.triggerTop+c,f=l.start.mode===`absolute`?l.start.value:d-l.start.anchorOffset,p;switch(l.end.mode){case`auto`:if(l.start.mode!==`clause`)throw Error(`StickyScrollTrigger: internal error: an absolute start reached 'auto' end mode, which resolveStartSpec should have already rejected.`);p=f+Math.max(0,l.start.anchorOffset+(l.coverTop-l.triggerTop));break;case`dwell`:p=f+l.end.distancePx;break;case`absolute`:p=Math.max(f,l.end.value);break;case`max`:p=Math.max(f,r+l.end.offsetPx);break;case`clause`:{let n;if(l.endTriggerIsSelf)n=d;else{let t=l.endTriggerIndex===null?void 0:o[l.endTriggerIndex]===void 0?a?a.naturalTops[l.endTriggerIndex]:void 0:o[l.endTriggerIndex];n=t===void 0?l.end.measureLive?i(u):l.end.rawTop===null?0:l.end.rawTop+R(e,s,a,l.end.rawTop,u):t}let r=j(l.end.clause,l.endTriggerHeight,t);p=Math.max(f,n-r);break}}let m;if(l.kind===`cover`){if(l.start.mode!==`clause`)throw Error(`StickyScrollTrigger: internal error: a cover layer measurement carries an absolute start, which resolveStartSpec should have already rejected.`);m={freezeStart:f,freezeEnd:p,stickyTop:l.start.anchorOffset-(l.triggerTop-l.wrapperTop),paddingHeight:null}}else m={freezeStart:f,freezeEnd:p,stickyTop:n-f,paddingHeight:Math.max(0,p-f)};return m.paddingHeight!==null&&(c+=m.paddingHeight),o[u]=d,s[u]=m.paddingHeight,m}),naturalTops:o}},B=(e,t)=>{let n=e.some((e,t)=>e.end.mode!==`clause`||e.endTriggerIsSelf?!1:e.endTriggerIndex===null?e.end.rawTop!==null:e.endTriggerIndex>=t),r=new Map,i={...t,measureLiveEndTriggerTop:e=>{let n=r.get(e);if(n!==void 0)return n;let i=t.measureLiveEndTriggerTop(e);return r.set(e,i),i}},{plans:a,naturalTops:o}=z(e,i,null);if(n)for(let t=0;t<e.length;t+=1){let n={paddings:a.map(e=>e.paddingHeight),naturalTops:o},r=z(e,i,n),s=r.plans.every((e,t)=>e.paddingHeight===a[t].paddingHeight);if(a=r.plans,o=r.naturalTops,s)break;if(t===e.length-1)throw Error(`StickyScrollTrigger: could not resolve endTrigger positions: some Scene layers' endTrigger references form a circular structural dependency (either two or more triggers reference each other's endTrigger, or an unregistered endTrigger's dwell depends on a layer whose own end depends back on it). Point each endTrigger at a layer that doesn't depend on it.`)}return a.forEach((e,n)=>t.onPlanned(n,e)),a},V=(e,t,n,a,o,s)=>{if(a===null)return{mode:`auto`};if(N(a))return{mode:`dwell`,distancePx:Math.max(0,P(a,t))};if(k(a))return{mode:`absolute`,value:A(a)};if(I(a)){if(n.kind!==`cover`)throw Error(`StickyScrollTrigger: createStickyTrigger's trigger ${r(n.trigger)} has end "${a}", which uses GSAP's 'max' keyword. This isn't supported here (a Scene layer's own dwell padding contributes to the document's max scroll position, so the freeze window would depend on itself and never converge). Use createOverlapScroll instead (cover layers never add padding), or a dwell distance such as '+=500'.`);return{mode:`max`,offsetPx:L(a,t)}}if(n.kind!==`cover`&&s!==null&&s>o)throw Error(`StickyScrollTrigger: createStickyTrigger's trigger ${r(n.trigger)}'s endTrigger (${r(n.endTrigger)}) refers to a layer positioned later in DOM order. This isn't supported (the layer's own dwell padding always precedes and pushes down that later layer's position, so the freeze window would depend on its own dwell and never converge). Point endTrigger at a layer registered earlier in DOM order, or use a dwell distance such as '+=500' instead.`);let c=s===null,l=e.contains(n.endTrigger);if(c&&!l&&n.kind!==`cover`)throw Error(`StickyScrollTrigger: createStickyTrigger's trigger ${r(n.trigger)} has an endTrigger (${r(n.endTrigger)}) outside the shared container (the layer's own dwell would push it away, so the freeze window never converges). Point endTrigger at an element inside the container, or use a dwell distance such as '+=500'.`);return{mode:`clause`,clause:a,rawTop:c&&l?i(n.endTrigger):null,measureLive:c&&!l}},H=(e,t,n,i)=>{if(!k(t))return{mode:`clause`,anchorOffset:j(t,n,i)};if(e.kind===`cover`)throw Error(`StickyScrollTrigger: createOverlapScroll's trigger ${r(e.trigger)} has start "${t}", an absolute scroll position (GSAP treats any bare-number start this way). This isn't supported here: a cover layer's sticky position is always computed relative to its own wrapper, which needs start to be a position clause (e.g. 'bottom bottom'), not an absolute scroll position.`);return{mode:`absolute`,value:A(t)}},U=(e,t,n,r,a)=>{let o=w(n.start),s=n.end===null?null:w(n.end),c=n.trigger.offsetHeight,l=n.endTrigger===n.trigger,u=a.get(n.endTrigger)??null,d=V(e,t,n,s,r,u),f=H(n,o,c,t);return{kind:n.kind,start:f,triggerTop:i(n.trigger),wrapperTop:n.wrapper?i(n.wrapper):0,coverTop:n.kind===`cover`?i(n.cover):0,end:d,endTriggerIsSelf:l,endTriggerIndex:u,endTriggerHeight:d.mode===`clause`?n.endTrigger.offsetHeight:0}},W=(e,t)=>e.some(e=>e.trigger===t);return class{#e;#t=[];#n=[];#r=new WeakMap;#i=!1;#a=!1;#o=null;#s=!1;#c=!1;#l=null;#u=[];#d;#f;constructor(t,n={}){this.#e=e(t),this.#d=b(this.#e),this.#f=n.scrollMarginTargets===void 0?`[id]`:n.scrollMarginTargets}#p(){this.#i&&=(C(this.#e,this.#n,this.#o),this.#o=null,!1)}#m(){this.#o=S(this.#e,this.#t),this.#n=[...this.#t],this.#i=!0,this.#a=!1}#h(){this.#c||this.#s||(this.#c=!0,queueMicrotask(()=>{this.#c=!1,!this.#s&&(this.refresh(),typeof this.#l?.refresh==`function`&&this.#l.refresh())}))}#g(e){this.#l=e.constructor}#_(e,t,n){return r=>{this.#g(r),e[0]===t&&this.refresh(),n?.(r)}}#v(){this.#u.forEach(e=>{if(e.outer)return;let t=p(e.trigger);e.outer=t.outer,e.inner=t.inner})}#y(){let e=this.#t.map(e=>e.wrapper).filter(e=>e!==null).map(e=>({wrapper:e,position:e.style.position,top:e.style.top}));return e.forEach(({wrapper:e})=>c(e)),()=>{e.forEach(({wrapper:e,position:t,top:n})=>{e.style.position=t,e.style.top=n})}}#b(e){if(!this.#u.length)return;let t=this.#y();this.#u.forEach(e=>{c(e.trigger),e.inner&&(e.inner.style.height=``)}),this.#u.forEach(t=>{if(!t.inner)return;let n=w(t.end);if(I(n))throw Error(`StickyScrollTrigger: createStickyPin's end "${n}" uses GSAP's 'max' keyword, which isn't supported here (the pin's own spacer height contributes to the document's max scroll position, so the spacer height would depend on itself and never converge). Use a plain position clause such as 'top top' against endTrigger instead.`);let r=w(t.top),a=i(t.trigger),o=t.trigger.offsetHeight,s=(k(n)?A(n):i(t.endTrigger)-j(n,t.endTrigger.offsetHeight,e))-a+r+o;l(t.trigger,r),t.inner.style.height=`${Math.max(0,s)}px`}),t()}#x(e){if(!this.#t.length)return;let t=[];if(this.#t.forEach(e=>{e.wrapper&&(c(e.wrapper),t.push(e))}),!t.length)return;let n=this.#o?i(this.#o):0,r=new Map;t.forEach((e,t)=>{r.set(e.trigger,t)});let a=t.map((t,n)=>U(this.#e,e,t,n,r)),s=0,u=()=>B(a,{viewportHeight:e,structureTop:n,documentMaxScroll:s,measureLiveEndTriggerTop:e=>i(t[e].endTrigger),onPlanned:(e,n)=>{let r=t[e];r.freezeStart=n.freezeStart,r.freezeEnd=n.freezeEnd,r.wrapper&&(l(r.wrapper,n.stickyTop),r.kind===`scene`&&r.padding&&n.paddingHeight!==null&&(r.padding.style.height=`${n.paddingHeight}px`))}});u();let d=a.some(e=>e.end.mode===`clause`&&e.end.measureLive),f=a.some(e=>e.end.mode===`max`);f&&(s=o(e)),(d||f)&&u()}refresh(){if(this.#s)return;(this.#a||!this.#i||x(this.#t))&&(this.#p(),this.#m()),this.#v();let e=this.#t.length||this.#u.length?a():0;this.#x(e),this.#b(e),this.#S()}#S(){this.#d.sync(this.#t.filter(e=>e.kind===`scene`).map(({trigger:e,freezeStart:t,freezeEnd:n})=>({trigger:e,freezeStart:t,freezeEnd:n})),this.#o,this.#f)}#C(e,t,n){if(W(e,t))throw Error(n)}#w(e,t,n){if(this.#s)throw Error(`StickyScrollTrigger: cannot register a new layer after destroy() has been called.`);return this.#C(this.#t,e.trigger,`StickyScrollTrigger: ${r(e.trigger)} is already registered as a trigger for another layer. Each element can only be used once across createStickyTrigger/createOverlapScroll calls in the same controller instance (reusing it makes endTrigger resolution ambiguous).`),this.#C(this.#u,e.trigger,`StickyScrollTrigger: ${r(e.trigger)} is already registered as a pin trigger (createStickyPin). Using the same element as both a Scene/Cover trigger and a pin trigger wraps it twice and produces conflicting position:sticky behavior.`),this.#t.push(e),this.#a=!0,{...n,trigger:e.trigger,start:()=>e.freezeStart,end:()=>e.freezeEnd||.001,invalidateOnRefresh:n.invalidateOnRefresh??!0,onKill:n=>{this.#g(n);let r=this.#t.indexOf(e);r!==-1&&this.#t.splice(r,1),this.#a=!0,this.#h(),t?.(n)},onRefreshInit:this.#_(this.#t,e,n.onRefreshInit)}}createStickyTrigger({trigger:e,start:r=`0 0`,end:i=`100% 0`,endTrigger:a,onKill:o,...s}){let c=t(e,`createStickyTrigger`),l=n(c,a,`createStickyTrigger`);return this.#w({kind:`scene`,trigger:c,endTrigger:l,container:null,wrapper:null,padding:null,start:r,end:i,freezeStart:0,freezeEnd:0},o,s)}createOverlapScroll({trigger:e,cover:i,start:a=`bottom bottom`,end:o=null,endTrigger:s,onKill:c,...l}){let u=t(e,`createOverlapScroll`),d=n(u,s,`createOverlapScroll`),f=i===void 0?u.nextElementSibling:t(i,`createOverlapScroll`);if(!(f instanceof HTMLElement))throw Error(`createOverlapScroll: cover element (trigger.nextElementSibling) not found for trigger ${r(u)}`);if(f.parentNode!==u.parentNode)throw Error(`createOverlapScroll: cover ${r(f)} must be a sibling of trigger `+r(u));let p={kind:`cover`,trigger:u,endTrigger:d,cover:f,wrapper:null,start:a,end:o,freezeStart:0,freezeEnd:0},m=this.#w(p,e=>{h(),c?.(e)},l),h=g(f);return this.#r.set(p,h),m}createStickyPin({trigger:e,top:n=0,endTrigger:i,end:a=`top top`,onKill:o,...s}){if(this.#s)throw Error(`StickyScrollTrigger: cannot register a new pin after destroy() has been called.`);let c=t(e,`createStickyPin`),l=t(i,`createStickyPin`);this.#C(this.#u,c,`StickyScrollTrigger: ${r(c)} is already registered as a pin trigger.`),this.#C(this.#t,c,`StickyScrollTrigger: ${r(c)} is already registered as a trigger for another layer (createStickyTrigger/createOverlapScroll). Using the same element as both a Scene/Cover trigger and a pin trigger wraps it twice and produces conflicting position:sticky behavior.`);let u={trigger:c,outer:null,inner:null,top:n,endTrigger:l,end:a};return this.#u.push(u),{...s,trigger:c,onKill:e=>{this.#g(e);let t=this.#u.indexOf(u);t!==-1&&this.#u.splice(t,1),u.outer&&(m(u.outer,u.trigger),u.outer=null,u.inner=null),o?.(e)},onRefreshInit:this.#_(this.#u,u,s.onRefreshInit)}}resolveScrollPosition(e,n){let r=t(e,`resolveScrollPosition`),c=w(n),l=a();if(I(c))return o(l)+L(c,l);if(k(c))return A(c);let u=0;this.#t.forEach(e=>{e.kind===`scene`&&(s(e.trigger,r)>=0||(u+=e.freezeEnd-e.freezeStart))});let d=j(c,r.offsetHeight,l),f=this.#y(),p=i(r)+u-d;return f(),p}static getScrollTop(e,n){let r=t(e,`StickyScrollTrigger.getScrollTop`),a=n.find(e=>e.#e.contains(r));return a?a.resolveScrollPosition(r,`top top`):i(r)}createResolvedTrigger({trigger:e,start:r,end:i,endTrigger:a,...o}){let s=t(e,`createResolvedTrigger`),c=n(s,a,`createResolvedTrigger`);return{...o,trigger:s,start:()=>this.resolveScrollPosition(s,r),end:()=>this.resolveScrollPosition(c,i)}}destroy(){this.#s||(this.#s=!0,this.#d.restore(),this.#t.forEach(e=>{e.kind===`cover`&&this.#r.get(e)?.()}),this.#p(),this.#t.length=0,this.#n=[],this.#u.forEach(e=>{e.outer&&m(e.outer,e.trigger)}),this.#u.length=0)}}})();
|
|
8
|
+
var StickyScrollTrigger=(function(){let e=e=>{let t=typeof e==`string`?document.querySelector(e):e;if(!t){let t=typeof e==`string`?`"${e}"`:String(e);throw Error(`StickyScrollTrigger: root ${t} not found`)}return t},t=(e,t)=>{let n=typeof e==`string`?document.querySelector(e):e;if(!n){let n=typeof e==`string`?`"${e}"`:String(e);throw Error(`${t}: element ${n} not found`)}return n},n=(e,n,r)=>n===void 0?e:t(n,r),r=e=>{let t=e.id?`#${e.id}`:``,n=e.classList.length>0?`.${Array.from(e.classList).join(`.`)}`:``;return`<${e.tagName.toLowerCase()}${t}${n}>`},i=e=>{let t=0,n=e;for(;n;){t+=n.offsetTop;let e=n.offsetParent;e&&(t+=e.clientTop),n=e}return t},a=()=>{let e=document.createElement(`div`);e.style.height=`100vh`,e.style.position=`absolute`,document.body.appendChild(e);let t=e.offsetHeight||window.innerHeight;return e.remove(),t},o=e=>{let t=document.documentElement;return Math.max(0,(t.scrollHeight||document.body.scrollHeight)-e)},s=(e,t)=>{let n=e.compareDocumentPosition(t);return n&Node.DOCUMENT_POSITION_FOLLOWING?-1:n&Node.DOCUMENT_POSITION_PRECEDING?1:0},c=e=>{e.style.position=``,e.style.top=``},l=(e,t)=>{e.style.position=`sticky`,e.style.top=`${t}px`},u=e=>{if(!e.parentNode)throw Error(`StickyScrollTrigger: ${r(e)} is not attached to the document`);let t=document.createElement(`div`),n=document.createElement(`div`),i=document.createElement(`div`);return i.setAttribute(`aria-hidden`,`true`),i.style.pointerEvents=`none`,e.parentNode.insertBefore(t,e),t.appendChild(n),n.appendChild(e),t.appendChild(i),{container:t,wrapper:n,padding:i}},d=e=>{let t=e.parentNode;if(!t)throw Error(`createOverlapScroll: trigger ${r(e)} is not attached to the document`);let n=document.createElement(`div`);for(t.insertBefore(n,t.firstChild);n.nextSibling;){let t=n.nextSibling;if(n.appendChild(t),t===e)break}return n},f=e=>{let t=e.parentNode;if(t){for(;e.firstChild;)t.insertBefore(e.firstChild,e);e.remove()}},p=e=>{if(!e.parentNode)throw Error(`createStickyPin: trigger ${r(e)} is not attached to the document`);let t=document.createElement(`div`),n=document.createElement(`div`);return t.style.height=`0`,t.style.contain=`layout`,e.parentNode.insertBefore(t,e),t.appendChild(n),n.appendChild(e),{outer:t,inner:n}},m=(e,t)=>{let n=e.parentNode;c(t),n&&n.insertBefore(t,e),e.remove()},h=new WeakMap,g=e=>{let t=[];for(let n=e;n;n=n.nextElementSibling){if(!(n instanceof HTMLElement))continue;let e=h.get(n);if(e)e.count+=1;else{let e=getComputedStyle(n),t={count:1,position:n.style.position,zIndex:n.style.zIndex};e.position===`static`&&(n.style.position=`relative`),e.zIndex===`auto`&&(n.style.zIndex=`1`),h.set(n,t)}t.push(n)}return()=>{t.forEach(e=>{let t=h.get(e);t&&(--t.count,!(t.count>0)&&(e.style.position=t.position,e.style.zIndex=t.zIndex,h.delete(e)))}),t.length=0}},_=0,v=typeof CSS<`u`&&typeof CSS.supports==`function`&&CSS.supports(`animation-timeline`,`scroll()`),y=(e,t)=>{let n=[],r=[],i=[],a=[];return t.forEach((t,o)=>{let s=`--${e}-c${o}`,c=`${e}-k${o}`;n.push(`@property ${s}{syntax:"<length>";inherits:true;initial-value:0px}`,`@keyframes ${c}{from{${s}:0px}to{${s}:${t.freezeEnd-t.freezeStart}px}}`),r.push(`${c} linear both`),i.push(`scroll(root block)`),a.push(`${t.freezeStart}px ${t.freezeEnd}px`)}),n.push(`@supports (animation-timeline: scroll()) {[data-${e}]{animation:${r.join(`,`)};animation-timeline:${i.join(`,`)};animation-range:${a.join(`,`)}}}`),n.join(``)},b=e=>{let t=`sst${_++}`,n=new WeakMap,r=new Set,i=null,a=null,o=[],c=null,l=()=>{if(!c)return;let e=window.scrollY;o.forEach((n,r)=>{let i=Math.min(Math.max(e-n.freezeStart,0),n.freezeEnd-n.freezeStart);c.style.setProperty(`--${t}-c${r}`,`${i}px`)})};v||window.addEventListener(`scroll`,l,{passive:!0});let u=e=>{let t=n.get(e);e.style.scrollMarginTop=t?t.inline:``},d=()=>{i?.remove(),i=null,a?.removeAttribute(`data-${t}`),a=null,o=[],c=null};return{sync(f,p,m){let h=f.filter(e=>e.freezeEnd>e.freezeStart);if(m===null||!h.length||!p){r.forEach(u),r=new Set,d();return}a!==p&&(a?.removeAttribute(`data-${t}`),a=p,a.setAttribute(`data-${t}`,``)),v||(o=h,c=p,l()),i||(i=document.createElement(`style`),document.head.appendChild(i)),i.textContent=y(t,h);let g=Array.from(e.querySelectorAll(m));g.forEach(e=>{let t=n.get(e);t?r.has(e)&&(e.style.scrollMarginTop=t.inline):n.set(e,{inline:e.style.scrollMarginTop})});let _=h.map((e,n)=>`var(--${t}-c${n}, 0px)`).join(` + `);g.forEach(e=>{let t=parseFloat(getComputedStyle(e).scrollMarginTop)||0,n=0;h.forEach(t=>{s(t.trigger,e)>=0||(n+=t.freezeEnd-t.freezeStart)}),e.style.scrollMarginTop=`calc(${t}px + var(--sst-scroll-margin-top-offset, 0px) + ${_} - ${n}px)`});let b=new Set(g);r.forEach(e=>{b.has(e)||u(e)}),r=b},restore(){v||window.removeEventListener(`scroll`,l),r.forEach(u),r=new Set,d()}}},x=e=>{for(let t=1;t<e.length;t+=1)if(s(e[t-1].trigger,e[t].trigger)>0)return!0;return!1},S=(e,t)=>{t.sort((e,t)=>s(e.trigger,t.trigger));for(let e of t)e.kind===`cover`&&(e.wrapper=d(e.trigger));let n=e,r=null;for(let e of t){if(e.kind!==`scene`)continue;let t=u(n);e.container=t.container,e.wrapper=t.wrapper,e.padding=t.padding,n=t.container,r=t.container}return r},C=(e,t,n)=>{n&&(n.parentNode?.insertBefore(e,n),n.remove());for(let e=t.length-1;e>=0;--e){let n=t[e];n.kind===`cover`&&n.wrapper&&f(n.wrapper)}t.forEach(e=>{e.wrapper=null,e.kind===`scene`&&(e.container=null,e.padding=null)})},w=e=>typeof e==`function`?e():e,T={top:0,center:.5,bottom:1},E=(e,t,n,r)=>{let i=n===`%`?parseFloat(t)/100*r:parseFloat(t);return e?.startsWith(`-`)?-i:i},D=/^(top|center|bottom|(?:\d+\.?\d*|\.\d+))?(%|px)?(?:([+-]=?)(\d+\.?\d*|\.\d+)(%|px)?)?$/,O=(e,t)=>{let n=D.exec(e);if(!n||!n[1]&&!n[3])throw Error(`StickyScrollTrigger: unsupported position clause "${e}"`);let[,r,i,a,o,s]=n;if(r!==void 0&&a&&!a.includes(`=`)){let t=`${r}${i??``}${a}=${o}${s??``}`;throw Error(`StickyScrollTrigger: unsupported position clause "${e}": a signed offset needs an explicit '=' when it follows a keyword or number (GSAP itself requires this too, to tell "${r}" and the offset apart). Did you mean "${t}"?`)}let c=r!==void 0&&r in T,l=i===`%`;return{fraction:c?T[r]:l?parseFloat(r)/100:0,offsetPx:(!c&&!l&&r!==void 0?parseFloat(r):0)+(a?E(a,o,s,t):0)}},k=e=>typeof e==`number`||e.trim()!==``&&!Number.isNaN(Number(e)),A=e=>typeof e==`number`?e:Number(e),j=(e,t,n)=>{let[r,i=`top`]=e.trim().split(/\s+/),a=O(r,t),o=O(i,n);return n*o.fraction+o.offsetPx-(t*a.fraction+a.offsetPx)},M=/^\+=(\d+\.?\d*|\.\d+)(%|px)?$/,N=e=>typeof e==`string`&&M.test(e.trim()),P=(e,t)=>{let n=M.exec(String(e).trim());if(!n)throw Error(`StickyScrollTrigger: unsupported end format "${e}"`);let[,r,i]=n;return E(void 0,r,i,t)},F=/^max(?:([+-]=)(\d+\.?\d*|\.\d+)(%|px)?)?$/,I=e=>typeof e==`string`&&F.test(e.trim()),L=(e,t)=>{let n=F.exec(e.trim());if(!n)throw Error(`StickyScrollTrigger: unsupported max-position format "${e}"`);let[,r,i,a]=n;return r?E(r,i,a,t):0},R=(e,t,n,r,i)=>{let a=0;return e.forEach((e,o)=>{if(o===i||e.triggerTop>=r)return;let s=t[o]===void 0?n?n.paddings[o]:null:t[o];s!=null&&(a+=s)}),a},z=(e,{viewportHeight:t,structureTop:n,documentMaxScroll:r,measureLiveEndTriggerTop:i},a)=>{let o=[],s=[],c=0;return{plans:e.map((l,u)=>{let d=l.triggerTop+c,f=l.start.mode===`absolute`?l.start.value:d-l.start.anchorOffset,p;switch(l.end.mode){case`auto`:if(l.start.mode!==`clause`)throw Error(`StickyScrollTrigger: internal error: an absolute start reached 'auto' end mode, which resolveStartSpec should have already rejected.`);p=f+Math.max(0,l.start.anchorOffset+(l.coverTop-l.triggerTop));break;case`dwell`:p=f+l.end.distancePx;break;case`absolute`:p=Math.max(f,l.end.value);break;case`max`:p=Math.max(f,r+l.end.offsetPx);break;case`clause`:{let n;if(l.endTriggerIsSelf)n=d;else{let t=l.endTriggerIndex===null?void 0:o[l.endTriggerIndex]===void 0?a?a.naturalTops[l.endTriggerIndex]:void 0:o[l.endTriggerIndex];n=t===void 0?l.end.measureLive?i(u):l.end.rawTop===null?0:l.end.rawTop+R(e,s,a,l.end.rawTop,u):t}let r=j(l.end.clause,l.endTriggerHeight,t);p=Math.max(f,n-r);break}}let m;if(l.kind===`cover`){if(l.start.mode!==`clause`)throw Error(`StickyScrollTrigger: internal error: a cover layer measurement carries an absolute start, which resolveStartSpec should have already rejected.`);m={freezeStart:f,freezeEnd:p,stickyTop:l.start.anchorOffset-(l.triggerTop-l.wrapperTop),paddingHeight:null}}else m={freezeStart:f,freezeEnd:p,stickyTop:n-f,paddingHeight:Math.max(0,p-f)};return m.paddingHeight!==null&&(c+=m.paddingHeight),o[u]=d,s[u]=m.paddingHeight,m}),naturalTops:o}},B=(e,t)=>{let n=e.some((e,t)=>e.end.mode!==`clause`||e.endTriggerIsSelf?!1:e.endTriggerIndex===null?e.end.rawTop!==null:e.endTriggerIndex>=t),r=new Map,i={...t,measureLiveEndTriggerTop:e=>{let n=r.get(e);if(n!==void 0)return n;let i=t.measureLiveEndTriggerTop(e);return r.set(e,i),i}},{plans:a,naturalTops:o}=z(e,i,null);if(n)for(let t=0;t<e.length;t+=1){let n={paddings:a.map(e=>e.paddingHeight),naturalTops:o},r=z(e,i,n),s=r.plans.every((e,t)=>e.paddingHeight===a[t].paddingHeight);if(a=r.plans,o=r.naturalTops,s)break;if(t===e.length-1)throw Error(`StickyScrollTrigger: could not resolve endTrigger positions: some Scene layers' endTrigger references form a circular structural dependency (either two or more triggers reference each other's endTrigger, or an unregistered endTrigger's dwell depends on a layer whose own end depends back on it). Point each endTrigger at a layer that doesn't depend on it.`)}return a.forEach((e,n)=>t.onPlanned(n,e)),a},V=(e,t,n,a,o,s)=>{if(a===null)return{mode:`auto`};if(N(a))return{mode:`dwell`,distancePx:Math.max(0,P(a,t))};if(k(a))return{mode:`absolute`,value:A(a)};if(I(a)){if(n.kind!==`cover`)throw Error(`StickyScrollTrigger: createStickyTrigger's trigger ${r(n.trigger)} has end "${a}", which uses GSAP's 'max' keyword. This isn't supported here (a Scene layer's own dwell padding contributes to the document's max scroll position, so the freeze window would depend on itself and never converge). Use createOverlapScroll instead (cover layers never add padding), or a dwell distance such as '+=500'.`);return{mode:`max`,offsetPx:L(a,t)}}if(n.kind!==`cover`&&s!==null&&s>o)throw Error(`StickyScrollTrigger: createStickyTrigger's trigger ${r(n.trigger)}'s endTrigger (${r(n.endTrigger)}) refers to a layer positioned later in DOM order. This isn't supported (the layer's own dwell padding always precedes and pushes down that later layer's position, so the freeze window would depend on its own dwell and never converge). Point endTrigger at a layer registered earlier in DOM order, or use a dwell distance such as '+=500' instead.`);let c=s===null,l=e.contains(n.endTrigger);if(c&&!l&&n.kind!==`cover`)throw Error(`StickyScrollTrigger: createStickyTrigger's trigger ${r(n.trigger)} has an endTrigger (${r(n.endTrigger)}) outside the shared container (the layer's own dwell would push it away, so the freeze window never converges). Point endTrigger at an element inside the container, or use a dwell distance such as '+=500'.`);return{mode:`clause`,clause:a,rawTop:c&&l?i(n.endTrigger):null,measureLive:c&&!l}},H=(e,t,n,i)=>{if(!k(t))return{mode:`clause`,anchorOffset:j(t,n,i)};if(e.kind===`cover`)throw Error(`StickyScrollTrigger: createOverlapScroll's trigger ${r(e.trigger)} has start "${t}", an absolute scroll position (GSAP treats any bare-number start this way). This isn't supported here: a cover layer's sticky position is always computed relative to its own wrapper, which needs start to be a position clause (e.g. 'bottom bottom'), not an absolute scroll position.`);return{mode:`absolute`,value:A(t)}},U=(e,t,n,r,a)=>{let o=w(n.start),s=n.end===null?null:w(n.end),c=n.trigger.offsetHeight,l=n.endTrigger===n.trigger,u=a.get(n.endTrigger)??null,d=V(e,t,n,s,r,u),f=H(n,o,c,t);return{kind:n.kind,start:f,triggerTop:i(n.trigger),wrapperTop:n.wrapper?i(n.wrapper):0,coverTop:n.kind===`cover`?i(n.cover):0,end:d,endTriggerIsSelf:l,endTriggerIndex:u,endTriggerHeight:d.mode===`clause`?n.endTrigger.offsetHeight:0}},W=e=>{let t=e=>{if(!Number.isFinite(e))throw Error(`StickyScrollTrigger: createStickyPin's top must be a finite number, got ${e}.`);return`top ${e}px`};return typeof e==`function`?()=>t(e()):t(e)},G=(e,t)=>e.some(e=>e.trigger===t);return class{#e;#t=[];#n=[];#r=new WeakMap;#i=!1;#a=!1;#o=null;#s=!1;#c=!1;#l=null;#u=[];#d;#f;constructor(t,n={}){this.#e=e(t),this.#d=b(this.#e),this.#f=n.scrollMarginTargets===void 0?`[id]`:n.scrollMarginTargets}#p(){this.#i&&=(C(this.#e,this.#n,this.#o),this.#o=null,!1)}#m(){this.#o=S(this.#e,this.#t),this.#n=[...this.#t],this.#i=!0,this.#a=!1}#h(){this.#c||this.#s||(this.#c=!0,queueMicrotask(()=>{this.#c=!1,!this.#s&&(this.refresh(),typeof this.#l?.refresh==`function`&&this.#l.refresh())}))}#g(e){this.#l=e.constructor}#_(e,t,n){return r=>{this.#g(r),e[0]===t&&this.refresh(),n?.(r)}}#v(){this.#u.forEach(e=>{if(e.outer)return;let t=p(e.trigger);e.outer=t.outer,e.inner=t.inner})}#y(){let e=this.#t.map(e=>e.wrapper).filter(e=>e!==null).map(e=>({wrapper:e,position:e.style.position,top:e.style.top}));return e.forEach(({wrapper:e})=>c(e)),()=>{e.forEach(({wrapper:e,position:t,top:n})=>{e.style.position=t,e.style.top=n})}}#b(e){if(!this.#u.length)return;let t=this.#y();try{this.#x(e)}finally{t()}}#x(e){this.#u.forEach(e=>{c(e.trigger),e.inner&&(e.inner.style.height=``)}),this.#u.forEach(t=>{if(!t.inner)return;let n=w(t.end);if(I(n))throw Error(`StickyScrollTrigger: createStickyPin's end "${n}" uses GSAP's 'max' keyword, which isn't supported here (the pin's own spacer height contributes to the document's max scroll position, so the spacer height would depend on itself and never converge). Use a plain position clause such as 'top top' against endTrigger instead.`);let r=w(t.start),a=i(t.trigger),o=t.trigger.offsetHeight;if(k(r))throw Error(`StickyScrollTrigger: createStickyPin's start "${r}" is an absolute scroll position (GSAP treats any bare-number start this way). A pin can't act on one: it engages when position:sticky engages, so start says where trigger sits in the viewport while pinned. Use the top option (top: ${r}) for a px distance from the viewport's top edge, or a position clause such as 'top top'.`);let s=j(r,o,e),c=(k(n)?A(n):i(t.endTrigger)-j(n,t.endTrigger.offsetHeight,e))-a+s+o;l(t.trigger,s),t.inner.style.height=`${Math.max(0,c)}px`})}#S(e){if(!this.#t.length)return;let t=[];if(this.#t.forEach(e=>{e.wrapper&&(c(e.wrapper),t.push(e))}),!t.length)return;let n=this.#o?i(this.#o):0,r=new Map;t.forEach((e,t)=>{r.set(e.trigger,t)});let a=t.map((t,n)=>U(this.#e,e,t,n,r)),s=0,u=()=>B(a,{viewportHeight:e,structureTop:n,documentMaxScroll:s,measureLiveEndTriggerTop:e=>i(t[e].endTrigger),onPlanned:(e,n)=>{let r=t[e];r.freezeStart=n.freezeStart,r.freezeEnd=n.freezeEnd,r.wrapper&&(l(r.wrapper,n.stickyTop),r.kind===`scene`&&r.padding&&n.paddingHeight!==null&&(r.padding.style.height=`${n.paddingHeight}px`))}});u();let d=a.some(e=>e.end.mode===`clause`&&e.end.measureLive),f=a.some(e=>e.end.mode===`max`);f&&(s=o(e)),(d||f)&&u()}refresh(){if(this.#s)return;(this.#a||!this.#i||x(this.#t))&&(this.#p(),this.#m()),this.#v();let e=this.#t.length||this.#u.length?a():0;this.#S(e),this.#b(e),this.#C()}#C(){this.#d.sync(this.#t.filter(e=>e.kind===`scene`).map(({trigger:e,freezeStart:t,freezeEnd:n})=>({trigger:e,freezeStart:t,freezeEnd:n})),this.#o,this.#f)}#w(e,t,n){if(G(e,t))throw Error(n)}#T(e,t,n){if(this.#s)throw Error(`StickyScrollTrigger: cannot register a new layer after destroy() has been called.`);return this.#w(this.#t,e.trigger,`StickyScrollTrigger: ${r(e.trigger)} is already registered as a trigger for another layer. Each element can only be used once across createStickyTrigger/createOverlapScroll calls in the same controller instance (reusing it makes endTrigger resolution ambiguous).`),this.#w(this.#u,e.trigger,`StickyScrollTrigger: ${r(e.trigger)} is already registered as a pin trigger (createStickyPin). Using the same element as both a Scene/Cover trigger and a pin trigger wraps it twice and produces conflicting position:sticky behavior.`),this.#t.push(e),this.#a=!0,{...n,trigger:e.trigger,start:()=>e.freezeStart,end:()=>e.freezeEnd||.001,invalidateOnRefresh:n.invalidateOnRefresh??!0,onKill:n=>{this.#g(n);let r=this.#t.indexOf(e);r!==-1&&this.#t.splice(r,1),this.#a=!0,this.#h(),t?.(n)},onRefreshInit:this.#_(this.#t,e,n.onRefreshInit)}}createStickyTrigger({trigger:e,start:r=`0 0`,end:i=`100% 0`,endTrigger:a,onKill:o,...s}){let c=t(e,`createStickyTrigger`),l=n(c,a,`createStickyTrigger`);return this.#T({kind:`scene`,trigger:c,endTrigger:l,container:null,wrapper:null,padding:null,start:r,end:i,freezeStart:0,freezeEnd:0},o,s)}createOverlapScroll({trigger:e,cover:i,start:a=`bottom bottom`,end:o=null,endTrigger:s,onKill:c,...l}){let u=t(e,`createOverlapScroll`),d=n(u,s,`createOverlapScroll`),f=i===void 0?u.nextElementSibling:t(i,`createOverlapScroll`);if(!(f instanceof HTMLElement))throw Error(`createOverlapScroll: cover element (trigger.nextElementSibling) not found for trigger ${r(u)}`);if(f.parentNode!==u.parentNode)throw Error(`createOverlapScroll: cover ${r(f)} must be a sibling of trigger `+r(u));let p={kind:`cover`,trigger:u,endTrigger:d,cover:f,wrapper:null,start:a,end:o,freezeStart:0,freezeEnd:0},m=this.#T(p,e=>{h(),c?.(e)},l),h=g(f);return this.#r.set(p,h),m}createStickyPin({trigger:e,start:n,top:i,endTrigger:a,end:o=`top top`,onKill:s,...c}){if(this.#s)throw Error(`StickyScrollTrigger: cannot register a new pin after destroy() has been called.`);if(n!==void 0&&i!==void 0)throw Error(`StickyScrollTrigger: createStickyPin accepts either start or top, not both (top: 20 is start: 'top 20px'). Use top for a plain px distance from the viewport's top edge, start for anything else.`);let l=t(e,`createStickyPin`),u=t(a,`createStickyPin`);this.#w(this.#u,l,`StickyScrollTrigger: ${r(l)} is already registered as a pin trigger.`),this.#w(this.#t,l,`StickyScrollTrigger: ${r(l)} is already registered as a trigger for another layer (createStickyTrigger/createOverlapScroll). Using the same element as both a Scene/Cover trigger and a pin trigger wraps it twice and produces conflicting position:sticky behavior.`);let d={trigger:l,outer:null,inner:null,start:i===void 0?n??`top top`:W(i),endTrigger:u,end:o};return this.#u.push(d),{...c,trigger:l,onKill:e=>{this.#g(e);let t=this.#u.indexOf(d);t!==-1&&this.#u.splice(t,1),d.outer&&(m(d.outer,d.trigger),d.outer=null,d.inner=null),s?.(e)},onRefreshInit:this.#_(this.#u,d,c.onRefreshInit)}}resolveScrollPosition(e,n){let r=t(e,`resolveScrollPosition`),c=w(n),l=a();if(I(c))return o(l)+L(c,l);if(k(c))return A(c);let u=0;this.#t.forEach(e=>{e.kind===`scene`&&(s(e.trigger,r)>=0||(u+=e.freezeEnd-e.freezeStart))});let d=j(c,r.offsetHeight,l),f=this.#y(),p=i(r)+u-d;return f(),p}static getScrollTop(e,n){let r=t(e,`StickyScrollTrigger.getScrollTop`),a=n.find(e=>e.#e.contains(r));return a?a.resolveScrollPosition(r,`top top`):i(r)}createResolvedTrigger({trigger:e,start:r,end:i,endTrigger:a,...o}){let s=t(e,`createResolvedTrigger`),c=n(s,a,`createResolvedTrigger`);return{...o,trigger:s,start:()=>this.resolveScrollPosition(s,r),end:()=>this.resolveScrollPosition(c,i)}}destroy(){this.#s||(this.#s=!0,this.#d.restore(),this.#t.forEach(e=>{e.kind===`cover`&&this.#r.get(e)?.()}),this.#p(),this.#t.length=0,this.#n=[],this.#u.forEach(e=>{e.outer&&m(e.outer,e.trigger)}),this.#u.length=0)}}})();
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @license
|
|
3
|
-
* StickyScrollTrigger v0.
|
|
3
|
+
* StickyScrollTrigger v0.4.0
|
|
4
4
|
* Copyright (c) 2026 Kuninori Ogino
|
|
5
5
|
* Released under the MIT License
|
|
6
6
|
* https://github.com/kuninori-ogino/sticky-scroll-trigger
|
|
@@ -508,6 +508,13 @@ const measureLayer = (rootElement, viewportHeight, layer, ownIndex, indexByTrigg
|
|
|
508
508
|
endTriggerHeight: end.mode === "clause" ? layer.endTrigger.offsetHeight : 0
|
|
509
509
|
};
|
|
510
510
|
};
|
|
511
|
+
const topToStartClause = (top) => {
|
|
512
|
+
const toClause = (value) => {
|
|
513
|
+
if (!Number.isFinite(value)) throw new Error(`StickyScrollTrigger: createStickyPin's top must be a finite number, got ${value}.`);
|
|
514
|
+
return `top ${value}px`;
|
|
515
|
+
};
|
|
516
|
+
return typeof top === "function" ? () => toClause(top()) : toClause(top);
|
|
517
|
+
};
|
|
511
518
|
const hasDuplicateTrigger = (list, trigger) => list.some((existing) => existing.trigger === trigger);
|
|
512
519
|
var StickyScrollTrigger = class {
|
|
513
520
|
#rootElement;
|
|
@@ -585,6 +592,13 @@ var StickyScrollTrigger = class {
|
|
|
585
592
|
#refreshPins(viewportHeight) {
|
|
586
593
|
if (!this.#pinLayers.length) return;
|
|
587
594
|
const restoreSceneCoverStickyState = this.#resetSceneCoverStickyState();
|
|
595
|
+
try {
|
|
596
|
+
this.#refreshPinLayers(viewportHeight);
|
|
597
|
+
} finally {
|
|
598
|
+
restoreSceneCoverStickyState();
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
#refreshPinLayers(viewportHeight) {
|
|
588
602
|
this.#pinLayers.forEach((layer) => {
|
|
589
603
|
resetStickyPosition(layer.trigger);
|
|
590
604
|
if (layer.inner) layer.inner.style.height = "";
|
|
@@ -593,14 +607,15 @@ var StickyScrollTrigger = class {
|
|
|
593
607
|
if (!layer.inner) return;
|
|
594
608
|
const resolvedEnd = resolveMaybeFn(layer.end);
|
|
595
609
|
if (isMaxFormat(resolvedEnd)) throw new Error(`StickyScrollTrigger: createStickyPin's end "${resolvedEnd}" uses GSAP's 'max' keyword, which isn't supported here (the pin's own spacer height contributes to the document's max scroll position, so the spacer height would depend on itself and never converge). Use a plain position clause such as 'top top' against endTrigger instead.`);
|
|
596
|
-
const
|
|
610
|
+
const resolvedStart = resolveMaybeFn(layer.start);
|
|
597
611
|
const triggerTop = documentTop(layer.trigger);
|
|
598
612
|
const triggerHeight = layer.trigger.offsetHeight;
|
|
613
|
+
if (isAbsoluteFormat(resolvedStart)) throw new Error(`StickyScrollTrigger: createStickyPin's start "${resolvedStart}" is an absolute scroll position (GSAP treats any bare-number start this way). A pin can't act on one: it engages when position:sticky engages, so start says where trigger sits in the viewport while pinned. Use the top option (top: ${resolvedStart}) for a px distance from the viewport's top edge, or a position clause such as 'top top'.`);
|
|
614
|
+
const topPx = resolveAnchorTop(resolvedStart, triggerHeight, viewportHeight);
|
|
599
615
|
const height = (isAbsoluteFormat(resolvedEnd) ? resolveAbsolute(resolvedEnd) : documentTop(layer.endTrigger) - resolveAnchorTop(resolvedEnd, layer.endTrigger.offsetHeight, viewportHeight)) - triggerTop + topPx + triggerHeight;
|
|
600
616
|
applyStickyPosition(layer.trigger, topPx);
|
|
601
617
|
layer.inner.style.height = `${Math.max(0, height)}px`;
|
|
602
618
|
});
|
|
603
|
-
restoreSceneCoverStickyState();
|
|
604
619
|
}
|
|
605
620
|
#refreshScenesAndCovers(viewportHeight) {
|
|
606
621
|
if (!this.#layers.length) return;
|
|
@@ -724,8 +739,9 @@ var StickyScrollTrigger = class {
|
|
|
724
739
|
this.#coverRestoreByLayer.set(layer, restoreCoverStyles);
|
|
725
740
|
return vars;
|
|
726
741
|
}
|
|
727
|
-
createStickyPin({ trigger: triggerInput, top
|
|
742
|
+
createStickyPin({ trigger: triggerInput, start, top, endTrigger: endTriggerInput, end = "top top", onKill, ...rest }) {
|
|
728
743
|
if (this.#destroyed) throw new Error("StickyScrollTrigger: cannot register a new pin after destroy() has been called.");
|
|
744
|
+
if (start !== void 0 && top !== void 0) throw new Error("StickyScrollTrigger: createStickyPin accepts either start or top, not both (top: 20 is start: 'top 20px'). Use top for a plain px distance from the viewport's top edge, start for anything else.");
|
|
729
745
|
const trigger = resolveElement(triggerInput, "createStickyPin");
|
|
730
746
|
const endTrigger = resolveElement(endTriggerInput, "createStickyPin");
|
|
731
747
|
this.#assertTriggerAvailable(this.#pinLayers, trigger, `StickyScrollTrigger: ${describeElement(trigger)} is already registered as a pin trigger.`);
|
|
@@ -734,7 +750,7 @@ var StickyScrollTrigger = class {
|
|
|
734
750
|
trigger,
|
|
735
751
|
outer: null,
|
|
736
752
|
inner: null,
|
|
737
|
-
top,
|
|
753
|
+
start: top === void 0 ? start ?? "top top" : topToStartClause(top),
|
|
738
754
|
endTrigger,
|
|
739
755
|
end
|
|
740
756
|
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @license
|
|
3
|
-
* StickyScrollTrigger v0.
|
|
3
|
+
* StickyScrollTrigger v0.4.0
|
|
4
4
|
* Copyright (c) 2026 Kuninori Ogino
|
|
5
5
|
* Released under the MIT License
|
|
6
6
|
* https://github.com/kuninori-ogino/sticky-scroll-trigger
|
|
7
7
|
*/
|
|
8
|
-
const e=e=>{let t=typeof e==`string`?document.querySelector(e):e;if(!t){let t=typeof e==`string`?`"${e}"`:String(e);throw Error(`StickyScrollTrigger: root ${t} not found`)}return t},t=(e,t)=>{let n=typeof e==`string`?document.querySelector(e):e;if(!n){let n=typeof e==`string`?`"${e}"`:String(e);throw Error(`${t}: element ${n} not found`)}return n},n=(e,n,r)=>n===void 0?e:t(n,r),r=e=>{let t=e.id?`#${e.id}`:``,n=e.classList.length>0?`.${Array.from(e.classList).join(`.`)}`:``;return`<${e.tagName.toLowerCase()}${t}${n}>`},i=e=>{let t=0,n=e;for(;n;){t+=n.offsetTop;let e=n.offsetParent;e&&(t+=e.clientTop),n=e}return t},a=()=>{let e=document.createElement(`div`);e.style.height=`100vh`,e.style.position=`absolute`,document.body.appendChild(e);let t=e.offsetHeight||window.innerHeight;return e.remove(),t},o=e=>{let t=document.documentElement;return Math.max(0,(t.scrollHeight||document.body.scrollHeight)-e)},s=(e,t)=>{let n=e.compareDocumentPosition(t);return n&Node.DOCUMENT_POSITION_FOLLOWING?-1:n&Node.DOCUMENT_POSITION_PRECEDING?1:0},c=e=>{e.style.position=``,e.style.top=``},l=(e,t)=>{e.style.position=`sticky`,e.style.top=`${t}px`},u=e=>{if(!e.parentNode)throw Error(`StickyScrollTrigger: ${r(e)} is not attached to the document`);let t=document.createElement(`div`),n=document.createElement(`div`),i=document.createElement(`div`);return i.setAttribute(`aria-hidden`,`true`),i.style.pointerEvents=`none`,e.parentNode.insertBefore(t,e),t.appendChild(n),n.appendChild(e),t.appendChild(i),{container:t,wrapper:n,padding:i}},d=e=>{let t=e.parentNode;if(!t)throw Error(`createOverlapScroll: trigger ${r(e)} is not attached to the document`);let n=document.createElement(`div`);for(t.insertBefore(n,t.firstChild);n.nextSibling;){let t=n.nextSibling;if(n.appendChild(t),t===e)break}return n},f=e=>{let t=e.parentNode;if(t){for(;e.firstChild;)t.insertBefore(e.firstChild,e);e.remove()}},p=e=>{if(!e.parentNode)throw Error(`createStickyPin: trigger ${r(e)} is not attached to the document`);let t=document.createElement(`div`),n=document.createElement(`div`);return t.style.height=`0`,t.style.contain=`layout`,e.parentNode.insertBefore(t,e),t.appendChild(n),n.appendChild(e),{outer:t,inner:n}},m=(e,t)=>{let n=e.parentNode;c(t),n&&n.insertBefore(t,e),e.remove()},h=new WeakMap,g=e=>{let t=[];for(let n=e;n;n=n.nextElementSibling){if(!(n instanceof HTMLElement))continue;let e=h.get(n);if(e)e.count+=1;else{let e=getComputedStyle(n),t={count:1,position:n.style.position,zIndex:n.style.zIndex};e.position===`static`&&(n.style.position=`relative`),e.zIndex===`auto`&&(n.style.zIndex=`1`),h.set(n,t)}t.push(n)}return()=>{t.forEach(e=>{let t=h.get(e);t&&(--t.count,!(t.count>0)&&(e.style.position=t.position,e.style.zIndex=t.zIndex,h.delete(e)))}),t.length=0}};let _=0;const v=typeof CSS<`u`&&typeof CSS.supports==`function`&&CSS.supports(`animation-timeline`,`scroll()`),y=(e,t)=>{let n=[],r=[],i=[],a=[];return t.forEach((t,o)=>{let s=`--${e}-c${o}`,c=`${e}-k${o}`;n.push(`@property ${s}{syntax:"<length>";inherits:true;initial-value:0px}`,`@keyframes ${c}{from{${s}:0px}to{${s}:${t.freezeEnd-t.freezeStart}px}}`),r.push(`${c} linear both`),i.push(`scroll(root block)`),a.push(`${t.freezeStart}px ${t.freezeEnd}px`)}),n.push(`@supports (animation-timeline: scroll()) {[data-${e}]{animation:${r.join(`,`)};animation-timeline:${i.join(`,`)};animation-range:${a.join(`,`)}}}`),n.join(``)},b=e=>{let t=`sst${_++}`,n=new WeakMap,r=new Set,i=null,a=null,o=[],c=null,l=()=>{if(!c)return;let e=window.scrollY;o.forEach((n,r)=>{let i=Math.min(Math.max(e-n.freezeStart,0),n.freezeEnd-n.freezeStart);c.style.setProperty(`--${t}-c${r}`,`${i}px`)})};v||window.addEventListener(`scroll`,l,{passive:!0});let u=e=>{let t=n.get(e);e.style.scrollMarginTop=t?t.inline:``},d=()=>{i?.remove(),i=null,a?.removeAttribute(`data-${t}`),a=null,o=[],c=null};return{sync(f,p,m){let h=f.filter(e=>e.freezeEnd>e.freezeStart);if(m===null||!h.length||!p){r.forEach(u),r=new Set,d();return}a!==p&&(a?.removeAttribute(`data-${t}`),a=p,a.setAttribute(`data-${t}`,``)),v||(o=h,c=p,l()),i||(i=document.createElement(`style`),document.head.appendChild(i)),i.textContent=y(t,h);let g=Array.from(e.querySelectorAll(m));g.forEach(e=>{let t=n.get(e);t?r.has(e)&&(e.style.scrollMarginTop=t.inline):n.set(e,{inline:e.style.scrollMarginTop})});let _=h.map((e,n)=>`var(--${t}-c${n}, 0px)`).join(` + `);g.forEach(e=>{let t=parseFloat(getComputedStyle(e).scrollMarginTop)||0,n=0;h.forEach(t=>{s(t.trigger,e)>=0||(n+=t.freezeEnd-t.freezeStart)}),e.style.scrollMarginTop=`calc(${t}px + var(--sst-scroll-margin-top-offset, 0px) + ${_} - ${n}px)`});let b=new Set(g);r.forEach(e=>{b.has(e)||u(e)}),r=b},restore(){v||window.removeEventListener(`scroll`,l),r.forEach(u),r=new Set,d()}}},x=e=>{for(let t=1;t<e.length;t+=1)if(s(e[t-1].trigger,e[t].trigger)>0)return!0;return!1},S=(e,t)=>{t.sort((e,t)=>s(e.trigger,t.trigger));for(let e of t)e.kind===`cover`&&(e.wrapper=d(e.trigger));let n=e,r=null;for(let e of t){if(e.kind!==`scene`)continue;let t=u(n);e.container=t.container,e.wrapper=t.wrapper,e.padding=t.padding,n=t.container,r=t.container}return r},C=(e,t,n)=>{n&&(n.parentNode?.insertBefore(e,n),n.remove());for(let e=t.length-1;e>=0;--e){let n=t[e];n.kind===`cover`&&n.wrapper&&f(n.wrapper)}t.forEach(e=>{e.wrapper=null,e.kind===`scene`&&(e.container=null,e.padding=null)})},w=e=>typeof e==`function`?e():e,T={top:0,center:.5,bottom:1},E=(e,t,n,r)=>{let i=n===`%`?parseFloat(t)/100*r:parseFloat(t);return e?.startsWith(`-`)?-i:i},D=/^(top|center|bottom|(?:\d+\.?\d*|\.\d+))?(%|px)?(?:([+-]=?)(\d+\.?\d*|\.\d+)(%|px)?)?$/,O=(e,t)=>{let n=D.exec(e);if(!n||!n[1]&&!n[3])throw Error(`StickyScrollTrigger: unsupported position clause "${e}"`);let[,r,i,a,o,s]=n;if(r!==void 0&&a&&!a.includes(`=`)){let t=`${r}${i??``}${a}=${o}${s??``}`;throw Error(`StickyScrollTrigger: unsupported position clause "${e}": a signed offset needs an explicit '=' when it follows a keyword or number (GSAP itself requires this too, to tell "${r}" and the offset apart). Did you mean "${t}"?`)}let c=r!==void 0&&r in T,l=i===`%`;return{fraction:c?T[r]:l?parseFloat(r)/100:0,offsetPx:(!c&&!l&&r!==void 0?parseFloat(r):0)+(a?E(a,o,s,t):0)}},k=e=>typeof e==`number`||e.trim()!==``&&!Number.isNaN(Number(e)),A=e=>typeof e==`number`?e:Number(e),j=(e,t,n)=>{let[r,i=`top`]=e.trim().split(/\s+/),a=O(r,t),o=O(i,n);return n*o.fraction+o.offsetPx-(t*a.fraction+a.offsetPx)},M=/^\+=(\d+\.?\d*|\.\d+)(%|px)?$/,N=e=>typeof e==`string`&&M.test(e.trim()),P=(e,t)=>{let n=M.exec(String(e).trim());if(!n)throw Error(`StickyScrollTrigger: unsupported end format "${e}"`);let[,r,i]=n;return E(void 0,r,i,t)},F=/^max(?:([+-]=)(\d+\.?\d*|\.\d+)(%|px)?)?$/,I=e=>typeof e==`string`&&F.test(e.trim()),L=(e,t)=>{let n=F.exec(e.trim());if(!n)throw Error(`StickyScrollTrigger: unsupported max-position format "${e}"`);let[,r,i,a]=n;return r?E(r,i,a,t):0},R=(e,t,n,r,i)=>{let a=0;return e.forEach((e,o)=>{if(o===i||e.triggerTop>=r)return;let s=t[o]===void 0?n?n.paddings[o]:null:t[o];s!=null&&(a+=s)}),a},z=(e,{viewportHeight:t,structureTop:n,documentMaxScroll:r,measureLiveEndTriggerTop:i},a)=>{let o=[],s=[],c=0;return{plans:e.map((l,u)=>{let d=l.triggerTop+c,f=l.start.mode===`absolute`?l.start.value:d-l.start.anchorOffset,p;switch(l.end.mode){case`auto`:if(l.start.mode!==`clause`)throw Error(`StickyScrollTrigger: internal error: an absolute start reached 'auto' end mode, which resolveStartSpec should have already rejected.`);p=f+Math.max(0,l.start.anchorOffset+(l.coverTop-l.triggerTop));break;case`dwell`:p=f+l.end.distancePx;break;case`absolute`:p=Math.max(f,l.end.value);break;case`max`:p=Math.max(f,r+l.end.offsetPx);break;case`clause`:{let n;if(l.endTriggerIsSelf)n=d;else{let t=l.endTriggerIndex===null?void 0:o[l.endTriggerIndex]===void 0?a?a.naturalTops[l.endTriggerIndex]:void 0:o[l.endTriggerIndex];n=t===void 0?l.end.measureLive?i(u):l.end.rawTop===null?0:l.end.rawTop+R(e,s,a,l.end.rawTop,u):t}let r=j(l.end.clause,l.endTriggerHeight,t);p=Math.max(f,n-r);break}}let m;if(l.kind===`cover`){if(l.start.mode!==`clause`)throw Error(`StickyScrollTrigger: internal error: a cover layer measurement carries an absolute start, which resolveStartSpec should have already rejected.`);m={freezeStart:f,freezeEnd:p,stickyTop:l.start.anchorOffset-(l.triggerTop-l.wrapperTop),paddingHeight:null}}else m={freezeStart:f,freezeEnd:p,stickyTop:n-f,paddingHeight:Math.max(0,p-f)};return m.paddingHeight!==null&&(c+=m.paddingHeight),o[u]=d,s[u]=m.paddingHeight,m}),naturalTops:o}},B=(e,t)=>{let n=e.some((e,t)=>e.end.mode!==`clause`||e.endTriggerIsSelf?!1:e.endTriggerIndex===null?e.end.rawTop!==null:e.endTriggerIndex>=t),r=new Map,i={...t,measureLiveEndTriggerTop:e=>{let n=r.get(e);if(n!==void 0)return n;let i=t.measureLiveEndTriggerTop(e);return r.set(e,i),i}},{plans:a,naturalTops:o}=z(e,i,null);if(n)for(let t=0;t<e.length;t+=1){let n={paddings:a.map(e=>e.paddingHeight),naturalTops:o},r=z(e,i,n),s=r.plans.every((e,t)=>e.paddingHeight===a[t].paddingHeight);if(a=r.plans,o=r.naturalTops,s)break;if(t===e.length-1)throw Error(`StickyScrollTrigger: could not resolve endTrigger positions: some Scene layers' endTrigger references form a circular structural dependency (either two or more triggers reference each other's endTrigger, or an unregistered endTrigger's dwell depends on a layer whose own end depends back on it). Point each endTrigger at a layer that doesn't depend on it.`)}return a.forEach((e,n)=>t.onPlanned(n,e)),a},V=(e,t,n,a,o,s)=>{if(a===null)return{mode:`auto`};if(N(a))return{mode:`dwell`,distancePx:Math.max(0,P(a,t))};if(k(a))return{mode:`absolute`,value:A(a)};if(I(a)){if(n.kind!==`cover`)throw Error(`StickyScrollTrigger: createStickyTrigger's trigger ${r(n.trigger)} has end "${a}", which uses GSAP's 'max' keyword. This isn't supported here (a Scene layer's own dwell padding contributes to the document's max scroll position, so the freeze window would depend on itself and never converge). Use createOverlapScroll instead (cover layers never add padding), or a dwell distance such as '+=500'.`);return{mode:`max`,offsetPx:L(a,t)}}if(n.kind!==`cover`&&s!==null&&s>o)throw Error(`StickyScrollTrigger: createStickyTrigger's trigger ${r(n.trigger)}'s endTrigger (${r(n.endTrigger)}) refers to a layer positioned later in DOM order. This isn't supported (the layer's own dwell padding always precedes and pushes down that later layer's position, so the freeze window would depend on its own dwell and never converge). Point endTrigger at a layer registered earlier in DOM order, or use a dwell distance such as '+=500' instead.`);let c=s===null,l=e.contains(n.endTrigger);if(c&&!l&&n.kind!==`cover`)throw Error(`StickyScrollTrigger: createStickyTrigger's trigger ${r(n.trigger)} has an endTrigger (${r(n.endTrigger)}) outside the shared container (the layer's own dwell would push it away, so the freeze window never converges). Point endTrigger at an element inside the container, or use a dwell distance such as '+=500'.`);return{mode:`clause`,clause:a,rawTop:c&&l?i(n.endTrigger):null,measureLive:c&&!l}},H=(e,t,n,i)=>{if(!k(t))return{mode:`clause`,anchorOffset:j(t,n,i)};if(e.kind===`cover`)throw Error(`StickyScrollTrigger: createOverlapScroll's trigger ${r(e.trigger)} has start "${t}", an absolute scroll position (GSAP treats any bare-number start this way). This isn't supported here: a cover layer's sticky position is always computed relative to its own wrapper, which needs start to be a position clause (e.g. 'bottom bottom'), not an absolute scroll position.`);return{mode:`absolute`,value:A(t)}},U=(e,t,n,r,a)=>{let o=w(n.start),s=n.end===null?null:w(n.end),c=n.trigger.offsetHeight,l=n.endTrigger===n.trigger,u=a.get(n.endTrigger)??null,d=V(e,t,n,s,r,u),f=H(n,o,c,t);return{kind:n.kind,start:f,triggerTop:i(n.trigger),wrapperTop:n.wrapper?i(n.wrapper):0,coverTop:n.kind===`cover`?i(n.cover):0,end:d,endTriggerIsSelf:l,endTriggerIndex:u,endTriggerHeight:d.mode===`clause`?n.endTrigger.offsetHeight:0}},W=(e,t)=>e.some(e=>e.trigger===t);var G=class{#e;#t=[];#n=[];#r=new WeakMap;#i=!1;#a=!1;#o=null;#s=!1;#c=!1;#l=null;#u=[];#d;#f;constructor(t,n={}){this.#e=e(t),this.#d=b(this.#e),this.#f=n.scrollMarginTargets===void 0?`[id]`:n.scrollMarginTargets}#p(){this.#i&&=(C(this.#e,this.#n,this.#o),this.#o=null,!1)}#m(){this.#o=S(this.#e,this.#t),this.#n=[...this.#t],this.#i=!0,this.#a=!1}#h(){this.#c||this.#s||(this.#c=!0,queueMicrotask(()=>{this.#c=!1,!this.#s&&(this.refresh(),typeof this.#l?.refresh==`function`&&this.#l.refresh())}))}#g(e){this.#l=e.constructor}#_(e,t,n){return r=>{this.#g(r),e[0]===t&&this.refresh(),n?.(r)}}#v(){this.#u.forEach(e=>{if(e.outer)return;let t=p(e.trigger);e.outer=t.outer,e.inner=t.inner})}#y(){let e=this.#t.map(e=>e.wrapper).filter(e=>e!==null).map(e=>({wrapper:e,position:e.style.position,top:e.style.top}));return e.forEach(({wrapper:e})=>c(e)),()=>{e.forEach(({wrapper:e,position:t,top:n})=>{e.style.position=t,e.style.top=n})}}#b(e){if(!this.#u.length)return;let t=this.#y();this.#u.forEach(e=>{c(e.trigger),e.inner&&(e.inner.style.height=``)}),this.#u.forEach(t=>{if(!t.inner)return;let n=w(t.end);if(I(n))throw Error(`StickyScrollTrigger: createStickyPin's end "${n}" uses GSAP's 'max' keyword, which isn't supported here (the pin's own spacer height contributes to the document's max scroll position, so the spacer height would depend on itself and never converge). Use a plain position clause such as 'top top' against endTrigger instead.`);let r=w(t.top),a=i(t.trigger),o=t.trigger.offsetHeight,s=(k(n)?A(n):i(t.endTrigger)-j(n,t.endTrigger.offsetHeight,e))-a+r+o;l(t.trigger,r),t.inner.style.height=`${Math.max(0,s)}px`}),t()}#x(e){if(!this.#t.length)return;let t=[];if(this.#t.forEach(e=>{e.wrapper&&(c(e.wrapper),t.push(e))}),!t.length)return;let n=this.#o?i(this.#o):0,r=new Map;t.forEach((e,t)=>{r.set(e.trigger,t)});let a=t.map((t,n)=>U(this.#e,e,t,n,r)),s=0,u=()=>B(a,{viewportHeight:e,structureTop:n,documentMaxScroll:s,measureLiveEndTriggerTop:e=>i(t[e].endTrigger),onPlanned:(e,n)=>{let r=t[e];r.freezeStart=n.freezeStart,r.freezeEnd=n.freezeEnd,r.wrapper&&(l(r.wrapper,n.stickyTop),r.kind===`scene`&&r.padding&&n.paddingHeight!==null&&(r.padding.style.height=`${n.paddingHeight}px`))}});u();let d=a.some(e=>e.end.mode===`clause`&&e.end.measureLive),f=a.some(e=>e.end.mode===`max`);f&&(s=o(e)),(d||f)&&u()}refresh(){if(this.#s)return;(this.#a||!this.#i||x(this.#t))&&(this.#p(),this.#m()),this.#v();let e=this.#t.length||this.#u.length?a():0;this.#x(e),this.#b(e),this.#S()}#S(){this.#d.sync(this.#t.filter(e=>e.kind===`scene`).map(({trigger:e,freezeStart:t,freezeEnd:n})=>({trigger:e,freezeStart:t,freezeEnd:n})),this.#o,this.#f)}#C(e,t,n){if(W(e,t))throw Error(n)}#w(e,t,n){if(this.#s)throw Error(`StickyScrollTrigger: cannot register a new layer after destroy() has been called.`);return this.#C(this.#t,e.trigger,`StickyScrollTrigger: ${r(e.trigger)} is already registered as a trigger for another layer. Each element can only be used once across createStickyTrigger/createOverlapScroll calls in the same controller instance (reusing it makes endTrigger resolution ambiguous).`),this.#C(this.#u,e.trigger,`StickyScrollTrigger: ${r(e.trigger)} is already registered as a pin trigger (createStickyPin). Using the same element as both a Scene/Cover trigger and a pin trigger wraps it twice and produces conflicting position:sticky behavior.`),this.#t.push(e),this.#a=!0,{...n,trigger:e.trigger,start:()=>e.freezeStart,end:()=>e.freezeEnd||.001,invalidateOnRefresh:n.invalidateOnRefresh??!0,onKill:n=>{this.#g(n);let r=this.#t.indexOf(e);r!==-1&&this.#t.splice(r,1),this.#a=!0,this.#h(),t?.(n)},onRefreshInit:this.#_(this.#t,e,n.onRefreshInit)}}createStickyTrigger({trigger:e,start:r=`0 0`,end:i=`100% 0`,endTrigger:a,onKill:o,...s}){let c=t(e,`createStickyTrigger`),l=n(c,a,`createStickyTrigger`);return this.#w({kind:`scene`,trigger:c,endTrigger:l,container:null,wrapper:null,padding:null,start:r,end:i,freezeStart:0,freezeEnd:0},o,s)}createOverlapScroll({trigger:e,cover:i,start:a=`bottom bottom`,end:o=null,endTrigger:s,onKill:c,...l}){let u=t(e,`createOverlapScroll`),d=n(u,s,`createOverlapScroll`),f=i===void 0?u.nextElementSibling:t(i,`createOverlapScroll`);if(!(f instanceof HTMLElement))throw Error(`createOverlapScroll: cover element (trigger.nextElementSibling) not found for trigger ${r(u)}`);if(f.parentNode!==u.parentNode)throw Error(`createOverlapScroll: cover ${r(f)} must be a sibling of trigger `+r(u));let p={kind:`cover`,trigger:u,endTrigger:d,cover:f,wrapper:null,start:a,end:o,freezeStart:0,freezeEnd:0},m=this.#w(p,e=>{h(),c?.(e)},l),h=g(f);return this.#r.set(p,h),m}createStickyPin({trigger:e,top:n=0,endTrigger:i,end:a=`top top`,onKill:o,...s}){if(this.#s)throw Error(`StickyScrollTrigger: cannot register a new pin after destroy() has been called.`);let c=t(e,`createStickyPin`),l=t(i,`createStickyPin`);this.#C(this.#u,c,`StickyScrollTrigger: ${r(c)} is already registered as a pin trigger.`),this.#C(this.#t,c,`StickyScrollTrigger: ${r(c)} is already registered as a trigger for another layer (createStickyTrigger/createOverlapScroll). Using the same element as both a Scene/Cover trigger and a pin trigger wraps it twice and produces conflicting position:sticky behavior.`);let u={trigger:c,outer:null,inner:null,top:n,endTrigger:l,end:a};return this.#u.push(u),{...s,trigger:c,onKill:e=>{this.#g(e);let t=this.#u.indexOf(u);t!==-1&&this.#u.splice(t,1),u.outer&&(m(u.outer,u.trigger),u.outer=null,u.inner=null),o?.(e)},onRefreshInit:this.#_(this.#u,u,s.onRefreshInit)}}resolveScrollPosition(e,n){let r=t(e,`resolveScrollPosition`),c=w(n),l=a();if(I(c))return o(l)+L(c,l);if(k(c))return A(c);let u=0;this.#t.forEach(e=>{e.kind===`scene`&&(s(e.trigger,r)>=0||(u+=e.freezeEnd-e.freezeStart))});let d=j(c,r.offsetHeight,l),f=this.#y(),p=i(r)+u-d;return f(),p}static getScrollTop(e,n){let r=t(e,`StickyScrollTrigger.getScrollTop`),a=n.find(e=>e.#e.contains(r));return a?a.resolveScrollPosition(r,`top top`):i(r)}createResolvedTrigger({trigger:e,start:r,end:i,endTrigger:a,...o}){let s=t(e,`createResolvedTrigger`),c=n(s,a,`createResolvedTrigger`);return{...o,trigger:s,start:()=>this.resolveScrollPosition(s,r),end:()=>this.resolveScrollPosition(c,i)}}destroy(){this.#s||(this.#s=!0,this.#d.restore(),this.#t.forEach(e=>{e.kind===`cover`&&this.#r.get(e)?.()}),this.#p(),this.#t.length=0,this.#n=[],this.#u.forEach(e=>{e.outer&&m(e.outer,e.trigger)}),this.#u.length=0)}};export{G as default};
|
|
8
|
+
const e=e=>{let t=typeof e==`string`?document.querySelector(e):e;if(!t){let t=typeof e==`string`?`"${e}"`:String(e);throw Error(`StickyScrollTrigger: root ${t} not found`)}return t},t=(e,t)=>{let n=typeof e==`string`?document.querySelector(e):e;if(!n){let n=typeof e==`string`?`"${e}"`:String(e);throw Error(`${t}: element ${n} not found`)}return n},n=(e,n,r)=>n===void 0?e:t(n,r),r=e=>{let t=e.id?`#${e.id}`:``,n=e.classList.length>0?`.${Array.from(e.classList).join(`.`)}`:``;return`<${e.tagName.toLowerCase()}${t}${n}>`},i=e=>{let t=0,n=e;for(;n;){t+=n.offsetTop;let e=n.offsetParent;e&&(t+=e.clientTop),n=e}return t},a=()=>{let e=document.createElement(`div`);e.style.height=`100vh`,e.style.position=`absolute`,document.body.appendChild(e);let t=e.offsetHeight||window.innerHeight;return e.remove(),t},o=e=>{let t=document.documentElement;return Math.max(0,(t.scrollHeight||document.body.scrollHeight)-e)},s=(e,t)=>{let n=e.compareDocumentPosition(t);return n&Node.DOCUMENT_POSITION_FOLLOWING?-1:n&Node.DOCUMENT_POSITION_PRECEDING?1:0},c=e=>{e.style.position=``,e.style.top=``},l=(e,t)=>{e.style.position=`sticky`,e.style.top=`${t}px`},u=e=>{if(!e.parentNode)throw Error(`StickyScrollTrigger: ${r(e)} is not attached to the document`);let t=document.createElement(`div`),n=document.createElement(`div`),i=document.createElement(`div`);return i.setAttribute(`aria-hidden`,`true`),i.style.pointerEvents=`none`,e.parentNode.insertBefore(t,e),t.appendChild(n),n.appendChild(e),t.appendChild(i),{container:t,wrapper:n,padding:i}},d=e=>{let t=e.parentNode;if(!t)throw Error(`createOverlapScroll: trigger ${r(e)} is not attached to the document`);let n=document.createElement(`div`);for(t.insertBefore(n,t.firstChild);n.nextSibling;){let t=n.nextSibling;if(n.appendChild(t),t===e)break}return n},f=e=>{let t=e.parentNode;if(t){for(;e.firstChild;)t.insertBefore(e.firstChild,e);e.remove()}},p=e=>{if(!e.parentNode)throw Error(`createStickyPin: trigger ${r(e)} is not attached to the document`);let t=document.createElement(`div`),n=document.createElement(`div`);return t.style.height=`0`,t.style.contain=`layout`,e.parentNode.insertBefore(t,e),t.appendChild(n),n.appendChild(e),{outer:t,inner:n}},m=(e,t)=>{let n=e.parentNode;c(t),n&&n.insertBefore(t,e),e.remove()},h=new WeakMap,g=e=>{let t=[];for(let n=e;n;n=n.nextElementSibling){if(!(n instanceof HTMLElement))continue;let e=h.get(n);if(e)e.count+=1;else{let e=getComputedStyle(n),t={count:1,position:n.style.position,zIndex:n.style.zIndex};e.position===`static`&&(n.style.position=`relative`),e.zIndex===`auto`&&(n.style.zIndex=`1`),h.set(n,t)}t.push(n)}return()=>{t.forEach(e=>{let t=h.get(e);t&&(--t.count,!(t.count>0)&&(e.style.position=t.position,e.style.zIndex=t.zIndex,h.delete(e)))}),t.length=0}};let _=0;const v=typeof CSS<`u`&&typeof CSS.supports==`function`&&CSS.supports(`animation-timeline`,`scroll()`),y=(e,t)=>{let n=[],r=[],i=[],a=[];return t.forEach((t,o)=>{let s=`--${e}-c${o}`,c=`${e}-k${o}`;n.push(`@property ${s}{syntax:"<length>";inherits:true;initial-value:0px}`,`@keyframes ${c}{from{${s}:0px}to{${s}:${t.freezeEnd-t.freezeStart}px}}`),r.push(`${c} linear both`),i.push(`scroll(root block)`),a.push(`${t.freezeStart}px ${t.freezeEnd}px`)}),n.push(`@supports (animation-timeline: scroll()) {[data-${e}]{animation:${r.join(`,`)};animation-timeline:${i.join(`,`)};animation-range:${a.join(`,`)}}}`),n.join(``)},b=e=>{let t=`sst${_++}`,n=new WeakMap,r=new Set,i=null,a=null,o=[],c=null,l=()=>{if(!c)return;let e=window.scrollY;o.forEach((n,r)=>{let i=Math.min(Math.max(e-n.freezeStart,0),n.freezeEnd-n.freezeStart);c.style.setProperty(`--${t}-c${r}`,`${i}px`)})};v||window.addEventListener(`scroll`,l,{passive:!0});let u=e=>{let t=n.get(e);e.style.scrollMarginTop=t?t.inline:``},d=()=>{i?.remove(),i=null,a?.removeAttribute(`data-${t}`),a=null,o=[],c=null};return{sync(f,p,m){let h=f.filter(e=>e.freezeEnd>e.freezeStart);if(m===null||!h.length||!p){r.forEach(u),r=new Set,d();return}a!==p&&(a?.removeAttribute(`data-${t}`),a=p,a.setAttribute(`data-${t}`,``)),v||(o=h,c=p,l()),i||(i=document.createElement(`style`),document.head.appendChild(i)),i.textContent=y(t,h);let g=Array.from(e.querySelectorAll(m));g.forEach(e=>{let t=n.get(e);t?r.has(e)&&(e.style.scrollMarginTop=t.inline):n.set(e,{inline:e.style.scrollMarginTop})});let _=h.map((e,n)=>`var(--${t}-c${n}, 0px)`).join(` + `);g.forEach(e=>{let t=parseFloat(getComputedStyle(e).scrollMarginTop)||0,n=0;h.forEach(t=>{s(t.trigger,e)>=0||(n+=t.freezeEnd-t.freezeStart)}),e.style.scrollMarginTop=`calc(${t}px + var(--sst-scroll-margin-top-offset, 0px) + ${_} - ${n}px)`});let b=new Set(g);r.forEach(e=>{b.has(e)||u(e)}),r=b},restore(){v||window.removeEventListener(`scroll`,l),r.forEach(u),r=new Set,d()}}},x=e=>{for(let t=1;t<e.length;t+=1)if(s(e[t-1].trigger,e[t].trigger)>0)return!0;return!1},S=(e,t)=>{t.sort((e,t)=>s(e.trigger,t.trigger));for(let e of t)e.kind===`cover`&&(e.wrapper=d(e.trigger));let n=e,r=null;for(let e of t){if(e.kind!==`scene`)continue;let t=u(n);e.container=t.container,e.wrapper=t.wrapper,e.padding=t.padding,n=t.container,r=t.container}return r},C=(e,t,n)=>{n&&(n.parentNode?.insertBefore(e,n),n.remove());for(let e=t.length-1;e>=0;--e){let n=t[e];n.kind===`cover`&&n.wrapper&&f(n.wrapper)}t.forEach(e=>{e.wrapper=null,e.kind===`scene`&&(e.container=null,e.padding=null)})},w=e=>typeof e==`function`?e():e,T={top:0,center:.5,bottom:1},E=(e,t,n,r)=>{let i=n===`%`?parseFloat(t)/100*r:parseFloat(t);return e?.startsWith(`-`)?-i:i},D=/^(top|center|bottom|(?:\d+\.?\d*|\.\d+))?(%|px)?(?:([+-]=?)(\d+\.?\d*|\.\d+)(%|px)?)?$/,O=(e,t)=>{let n=D.exec(e);if(!n||!n[1]&&!n[3])throw Error(`StickyScrollTrigger: unsupported position clause "${e}"`);let[,r,i,a,o,s]=n;if(r!==void 0&&a&&!a.includes(`=`)){let t=`${r}${i??``}${a}=${o}${s??``}`;throw Error(`StickyScrollTrigger: unsupported position clause "${e}": a signed offset needs an explicit '=' when it follows a keyword or number (GSAP itself requires this too, to tell "${r}" and the offset apart). Did you mean "${t}"?`)}let c=r!==void 0&&r in T,l=i===`%`;return{fraction:c?T[r]:l?parseFloat(r)/100:0,offsetPx:(!c&&!l&&r!==void 0?parseFloat(r):0)+(a?E(a,o,s,t):0)}},k=e=>typeof e==`number`||e.trim()!==``&&!Number.isNaN(Number(e)),A=e=>typeof e==`number`?e:Number(e),j=(e,t,n)=>{let[r,i=`top`]=e.trim().split(/\s+/),a=O(r,t),o=O(i,n);return n*o.fraction+o.offsetPx-(t*a.fraction+a.offsetPx)},M=/^\+=(\d+\.?\d*|\.\d+)(%|px)?$/,N=e=>typeof e==`string`&&M.test(e.trim()),P=(e,t)=>{let n=M.exec(String(e).trim());if(!n)throw Error(`StickyScrollTrigger: unsupported end format "${e}"`);let[,r,i]=n;return E(void 0,r,i,t)},F=/^max(?:([+-]=)(\d+\.?\d*|\.\d+)(%|px)?)?$/,I=e=>typeof e==`string`&&F.test(e.trim()),L=(e,t)=>{let n=F.exec(e.trim());if(!n)throw Error(`StickyScrollTrigger: unsupported max-position format "${e}"`);let[,r,i,a]=n;return r?E(r,i,a,t):0},R=(e,t,n,r,i)=>{let a=0;return e.forEach((e,o)=>{if(o===i||e.triggerTop>=r)return;let s=t[o]===void 0?n?n.paddings[o]:null:t[o];s!=null&&(a+=s)}),a},z=(e,{viewportHeight:t,structureTop:n,documentMaxScroll:r,measureLiveEndTriggerTop:i},a)=>{let o=[],s=[],c=0;return{plans:e.map((l,u)=>{let d=l.triggerTop+c,f=l.start.mode===`absolute`?l.start.value:d-l.start.anchorOffset,p;switch(l.end.mode){case`auto`:if(l.start.mode!==`clause`)throw Error(`StickyScrollTrigger: internal error: an absolute start reached 'auto' end mode, which resolveStartSpec should have already rejected.`);p=f+Math.max(0,l.start.anchorOffset+(l.coverTop-l.triggerTop));break;case`dwell`:p=f+l.end.distancePx;break;case`absolute`:p=Math.max(f,l.end.value);break;case`max`:p=Math.max(f,r+l.end.offsetPx);break;case`clause`:{let n;if(l.endTriggerIsSelf)n=d;else{let t=l.endTriggerIndex===null?void 0:o[l.endTriggerIndex]===void 0?a?a.naturalTops[l.endTriggerIndex]:void 0:o[l.endTriggerIndex];n=t===void 0?l.end.measureLive?i(u):l.end.rawTop===null?0:l.end.rawTop+R(e,s,a,l.end.rawTop,u):t}let r=j(l.end.clause,l.endTriggerHeight,t);p=Math.max(f,n-r);break}}let m;if(l.kind===`cover`){if(l.start.mode!==`clause`)throw Error(`StickyScrollTrigger: internal error: a cover layer measurement carries an absolute start, which resolveStartSpec should have already rejected.`);m={freezeStart:f,freezeEnd:p,stickyTop:l.start.anchorOffset-(l.triggerTop-l.wrapperTop),paddingHeight:null}}else m={freezeStart:f,freezeEnd:p,stickyTop:n-f,paddingHeight:Math.max(0,p-f)};return m.paddingHeight!==null&&(c+=m.paddingHeight),o[u]=d,s[u]=m.paddingHeight,m}),naturalTops:o}},B=(e,t)=>{let n=e.some((e,t)=>e.end.mode!==`clause`||e.endTriggerIsSelf?!1:e.endTriggerIndex===null?e.end.rawTop!==null:e.endTriggerIndex>=t),r=new Map,i={...t,measureLiveEndTriggerTop:e=>{let n=r.get(e);if(n!==void 0)return n;let i=t.measureLiveEndTriggerTop(e);return r.set(e,i),i}},{plans:a,naturalTops:o}=z(e,i,null);if(n)for(let t=0;t<e.length;t+=1){let n={paddings:a.map(e=>e.paddingHeight),naturalTops:o},r=z(e,i,n),s=r.plans.every((e,t)=>e.paddingHeight===a[t].paddingHeight);if(a=r.plans,o=r.naturalTops,s)break;if(t===e.length-1)throw Error(`StickyScrollTrigger: could not resolve endTrigger positions: some Scene layers' endTrigger references form a circular structural dependency (either two or more triggers reference each other's endTrigger, or an unregistered endTrigger's dwell depends on a layer whose own end depends back on it). Point each endTrigger at a layer that doesn't depend on it.`)}return a.forEach((e,n)=>t.onPlanned(n,e)),a},V=(e,t,n,a,o,s)=>{if(a===null)return{mode:`auto`};if(N(a))return{mode:`dwell`,distancePx:Math.max(0,P(a,t))};if(k(a))return{mode:`absolute`,value:A(a)};if(I(a)){if(n.kind!==`cover`)throw Error(`StickyScrollTrigger: createStickyTrigger's trigger ${r(n.trigger)} has end "${a}", which uses GSAP's 'max' keyword. This isn't supported here (a Scene layer's own dwell padding contributes to the document's max scroll position, so the freeze window would depend on itself and never converge). Use createOverlapScroll instead (cover layers never add padding), or a dwell distance such as '+=500'.`);return{mode:`max`,offsetPx:L(a,t)}}if(n.kind!==`cover`&&s!==null&&s>o)throw Error(`StickyScrollTrigger: createStickyTrigger's trigger ${r(n.trigger)}'s endTrigger (${r(n.endTrigger)}) refers to a layer positioned later in DOM order. This isn't supported (the layer's own dwell padding always precedes and pushes down that later layer's position, so the freeze window would depend on its own dwell and never converge). Point endTrigger at a layer registered earlier in DOM order, or use a dwell distance such as '+=500' instead.`);let c=s===null,l=e.contains(n.endTrigger);if(c&&!l&&n.kind!==`cover`)throw Error(`StickyScrollTrigger: createStickyTrigger's trigger ${r(n.trigger)} has an endTrigger (${r(n.endTrigger)}) outside the shared container (the layer's own dwell would push it away, so the freeze window never converges). Point endTrigger at an element inside the container, or use a dwell distance such as '+=500'.`);return{mode:`clause`,clause:a,rawTop:c&&l?i(n.endTrigger):null,measureLive:c&&!l}},H=(e,t,n,i)=>{if(!k(t))return{mode:`clause`,anchorOffset:j(t,n,i)};if(e.kind===`cover`)throw Error(`StickyScrollTrigger: createOverlapScroll's trigger ${r(e.trigger)} has start "${t}", an absolute scroll position (GSAP treats any bare-number start this way). This isn't supported here: a cover layer's sticky position is always computed relative to its own wrapper, which needs start to be a position clause (e.g. 'bottom bottom'), not an absolute scroll position.`);return{mode:`absolute`,value:A(t)}},U=(e,t,n,r,a)=>{let o=w(n.start),s=n.end===null?null:w(n.end),c=n.trigger.offsetHeight,l=n.endTrigger===n.trigger,u=a.get(n.endTrigger)??null,d=V(e,t,n,s,r,u),f=H(n,o,c,t);return{kind:n.kind,start:f,triggerTop:i(n.trigger),wrapperTop:n.wrapper?i(n.wrapper):0,coverTop:n.kind===`cover`?i(n.cover):0,end:d,endTriggerIsSelf:l,endTriggerIndex:u,endTriggerHeight:d.mode===`clause`?n.endTrigger.offsetHeight:0}},W=e=>{let t=e=>{if(!Number.isFinite(e))throw Error(`StickyScrollTrigger: createStickyPin's top must be a finite number, got ${e}.`);return`top ${e}px`};return typeof e==`function`?()=>t(e()):t(e)},G=(e,t)=>e.some(e=>e.trigger===t);var K=class{#e;#t=[];#n=[];#r=new WeakMap;#i=!1;#a=!1;#o=null;#s=!1;#c=!1;#l=null;#u=[];#d;#f;constructor(t,n={}){this.#e=e(t),this.#d=b(this.#e),this.#f=n.scrollMarginTargets===void 0?`[id]`:n.scrollMarginTargets}#p(){this.#i&&=(C(this.#e,this.#n,this.#o),this.#o=null,!1)}#m(){this.#o=S(this.#e,this.#t),this.#n=[...this.#t],this.#i=!0,this.#a=!1}#h(){this.#c||this.#s||(this.#c=!0,queueMicrotask(()=>{this.#c=!1,!this.#s&&(this.refresh(),typeof this.#l?.refresh==`function`&&this.#l.refresh())}))}#g(e){this.#l=e.constructor}#_(e,t,n){return r=>{this.#g(r),e[0]===t&&this.refresh(),n?.(r)}}#v(){this.#u.forEach(e=>{if(e.outer)return;let t=p(e.trigger);e.outer=t.outer,e.inner=t.inner})}#y(){let e=this.#t.map(e=>e.wrapper).filter(e=>e!==null).map(e=>({wrapper:e,position:e.style.position,top:e.style.top}));return e.forEach(({wrapper:e})=>c(e)),()=>{e.forEach(({wrapper:e,position:t,top:n})=>{e.style.position=t,e.style.top=n})}}#b(e){if(!this.#u.length)return;let t=this.#y();try{this.#x(e)}finally{t()}}#x(e){this.#u.forEach(e=>{c(e.trigger),e.inner&&(e.inner.style.height=``)}),this.#u.forEach(t=>{if(!t.inner)return;let n=w(t.end);if(I(n))throw Error(`StickyScrollTrigger: createStickyPin's end "${n}" uses GSAP's 'max' keyword, which isn't supported here (the pin's own spacer height contributes to the document's max scroll position, so the spacer height would depend on itself and never converge). Use a plain position clause such as 'top top' against endTrigger instead.`);let r=w(t.start),a=i(t.trigger),o=t.trigger.offsetHeight;if(k(r))throw Error(`StickyScrollTrigger: createStickyPin's start "${r}" is an absolute scroll position (GSAP treats any bare-number start this way). A pin can't act on one: it engages when position:sticky engages, so start says where trigger sits in the viewport while pinned. Use the top option (top: ${r}) for a px distance from the viewport's top edge, or a position clause such as 'top top'.`);let s=j(r,o,e),c=(k(n)?A(n):i(t.endTrigger)-j(n,t.endTrigger.offsetHeight,e))-a+s+o;l(t.trigger,s),t.inner.style.height=`${Math.max(0,c)}px`})}#S(e){if(!this.#t.length)return;let t=[];if(this.#t.forEach(e=>{e.wrapper&&(c(e.wrapper),t.push(e))}),!t.length)return;let n=this.#o?i(this.#o):0,r=new Map;t.forEach((e,t)=>{r.set(e.trigger,t)});let a=t.map((t,n)=>U(this.#e,e,t,n,r)),s=0,u=()=>B(a,{viewportHeight:e,structureTop:n,documentMaxScroll:s,measureLiveEndTriggerTop:e=>i(t[e].endTrigger),onPlanned:(e,n)=>{let r=t[e];r.freezeStart=n.freezeStart,r.freezeEnd=n.freezeEnd,r.wrapper&&(l(r.wrapper,n.stickyTop),r.kind===`scene`&&r.padding&&n.paddingHeight!==null&&(r.padding.style.height=`${n.paddingHeight}px`))}});u();let d=a.some(e=>e.end.mode===`clause`&&e.end.measureLive),f=a.some(e=>e.end.mode===`max`);f&&(s=o(e)),(d||f)&&u()}refresh(){if(this.#s)return;(this.#a||!this.#i||x(this.#t))&&(this.#p(),this.#m()),this.#v();let e=this.#t.length||this.#u.length?a():0;this.#S(e),this.#b(e),this.#C()}#C(){this.#d.sync(this.#t.filter(e=>e.kind===`scene`).map(({trigger:e,freezeStart:t,freezeEnd:n})=>({trigger:e,freezeStart:t,freezeEnd:n})),this.#o,this.#f)}#w(e,t,n){if(G(e,t))throw Error(n)}#T(e,t,n){if(this.#s)throw Error(`StickyScrollTrigger: cannot register a new layer after destroy() has been called.`);return this.#w(this.#t,e.trigger,`StickyScrollTrigger: ${r(e.trigger)} is already registered as a trigger for another layer. Each element can only be used once across createStickyTrigger/createOverlapScroll calls in the same controller instance (reusing it makes endTrigger resolution ambiguous).`),this.#w(this.#u,e.trigger,`StickyScrollTrigger: ${r(e.trigger)} is already registered as a pin trigger (createStickyPin). Using the same element as both a Scene/Cover trigger and a pin trigger wraps it twice and produces conflicting position:sticky behavior.`),this.#t.push(e),this.#a=!0,{...n,trigger:e.trigger,start:()=>e.freezeStart,end:()=>e.freezeEnd||.001,invalidateOnRefresh:n.invalidateOnRefresh??!0,onKill:n=>{this.#g(n);let r=this.#t.indexOf(e);r!==-1&&this.#t.splice(r,1),this.#a=!0,this.#h(),t?.(n)},onRefreshInit:this.#_(this.#t,e,n.onRefreshInit)}}createStickyTrigger({trigger:e,start:r=`0 0`,end:i=`100% 0`,endTrigger:a,onKill:o,...s}){let c=t(e,`createStickyTrigger`),l=n(c,a,`createStickyTrigger`);return this.#T({kind:`scene`,trigger:c,endTrigger:l,container:null,wrapper:null,padding:null,start:r,end:i,freezeStart:0,freezeEnd:0},o,s)}createOverlapScroll({trigger:e,cover:i,start:a=`bottom bottom`,end:o=null,endTrigger:s,onKill:c,...l}){let u=t(e,`createOverlapScroll`),d=n(u,s,`createOverlapScroll`),f=i===void 0?u.nextElementSibling:t(i,`createOverlapScroll`);if(!(f instanceof HTMLElement))throw Error(`createOverlapScroll: cover element (trigger.nextElementSibling) not found for trigger ${r(u)}`);if(f.parentNode!==u.parentNode)throw Error(`createOverlapScroll: cover ${r(f)} must be a sibling of trigger `+r(u));let p={kind:`cover`,trigger:u,endTrigger:d,cover:f,wrapper:null,start:a,end:o,freezeStart:0,freezeEnd:0},m=this.#T(p,e=>{h(),c?.(e)},l),h=g(f);return this.#r.set(p,h),m}createStickyPin({trigger:e,start:n,top:i,endTrigger:a,end:o=`top top`,onKill:s,...c}){if(this.#s)throw Error(`StickyScrollTrigger: cannot register a new pin after destroy() has been called.`);if(n!==void 0&&i!==void 0)throw Error(`StickyScrollTrigger: createStickyPin accepts either start or top, not both (top: 20 is start: 'top 20px'). Use top for a plain px distance from the viewport's top edge, start for anything else.`);let l=t(e,`createStickyPin`),u=t(a,`createStickyPin`);this.#w(this.#u,l,`StickyScrollTrigger: ${r(l)} is already registered as a pin trigger.`),this.#w(this.#t,l,`StickyScrollTrigger: ${r(l)} is already registered as a trigger for another layer (createStickyTrigger/createOverlapScroll). Using the same element as both a Scene/Cover trigger and a pin trigger wraps it twice and produces conflicting position:sticky behavior.`);let d={trigger:l,outer:null,inner:null,start:i===void 0?n??`top top`:W(i),endTrigger:u,end:o};return this.#u.push(d),{...c,trigger:l,onKill:e=>{this.#g(e);let t=this.#u.indexOf(d);t!==-1&&this.#u.splice(t,1),d.outer&&(m(d.outer,d.trigger),d.outer=null,d.inner=null),s?.(e)},onRefreshInit:this.#_(this.#u,d,c.onRefreshInit)}}resolveScrollPosition(e,n){let r=t(e,`resolveScrollPosition`),c=w(n),l=a();if(I(c))return o(l)+L(c,l);if(k(c))return A(c);let u=0;this.#t.forEach(e=>{e.kind===`scene`&&(s(e.trigger,r)>=0||(u+=e.freezeEnd-e.freezeStart))});let d=j(c,r.offsetHeight,l),f=this.#y(),p=i(r)+u-d;return f(),p}static getScrollTop(e,n){let r=t(e,`StickyScrollTrigger.getScrollTop`),a=n.find(e=>e.#e.contains(r));return a?a.resolveScrollPosition(r,`top top`):i(r)}createResolvedTrigger({trigger:e,start:r,end:i,endTrigger:a,...o}){let s=t(e,`createResolvedTrigger`),c=n(s,a,`createResolvedTrigger`);return{...o,trigger:s,start:()=>this.resolveScrollPosition(s,r),end:()=>this.resolveScrollPosition(c,i)}}destroy(){this.#s||(this.#s=!0,this.#d.restore(),this.#t.forEach(e=>{e.kind===`cover`&&this.#r.get(e)?.()}),this.#p(),this.#t.length=0,this.#n=[],this.#u.forEach(e=>{e.outer&&m(e.outer,e.trigger)}),this.#u.length=0)}};export{K as default};
|