sticky-scroll-trigger 0.4.0 → 0.6.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
|
@@ -135,13 +135,13 @@ Avoid debounce logic that assumes it runs before GSAP's 200ms delay. Use `refres
|
|
|
135
135
|
|
|
136
136
|
### `new StickyScrollTrigger(root, options?)`
|
|
137
137
|
|
|
138
|
-
Creates an instance that treats `root` (a selector string or `HTMLElement`) as the shared container
|
|
138
|
+
Creates an instance that treats `root` (a selector string or `HTMLElement`) as the shared container. Everything documented below is an instance method on it, apart from the static [`getScrollTop`](#stickyscrolltriggergetscrolltopelement-instances), which resolves a position across more than one instance. Throws if the shared container can't be found.
|
|
139
139
|
|
|
140
140
|
| option | default | description |
|
|
141
141
|
| --------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
142
142
|
| `scrollMarginTargets` | `'[id]'` | Which elements inside the shared container get their `scroll-margin-top` kept in sync (see [Same-page links](#same-page-links)). `null` disables it entirely |
|
|
143
143
|
|
|
144
|
-
|
|
144
|
+
Call them on the instance (`sticky.createStickyTrigger(...)`) rather than destructuring them out: a destructured method loses its `this` binding.
|
|
145
145
|
|
|
146
146
|
Like `root`, every `trigger`/`endTrigger`/`cover` option below also accepts a CSS selector string (in addition to an `HTMLElement`), resolved via `document.querySelector`, the same as GSAP ScrollTrigger's own `trigger`/`endTrigger`. Throws an error if the selector matches no elements.
|
|
147
147
|
|
|
@@ -155,11 +155,11 @@ Registers a layer that pins a scene and advances its effect, and returns `Scroll
|
|
|
155
155
|
| `pin` / `pinSpacing` / `anticipatePin` / `pinnedContainer` / `pinReparent` / `pinSpacer` / `pinType` | Pinning is handled by `position:sticky`, not GSAP pinning, so these have no effect |
|
|
156
156
|
| `horizontal` / `scroller` / `containerAnimation` | This module assumes vertical, window-based scrolling; setting these shifts positions |
|
|
157
157
|
|
|
158
|
-
You can specify `onKill`, `invalidateOnRefresh`, and `onRefreshInit`; the module also uses them internally. `invalidateOnRefresh` defaults to `true` (GSAP defaults to `false`) so function-valued tween props are re-measured on refresh
|
|
158
|
+
You can specify `onKill`, `invalidateOnRefresh`, and `onRefreshInit`; the module also uses them internally. `invalidateOnRefresh` defaults to `true` (GSAP defaults to `false`) so function-valued tween props are re-measured on refresh, and an explicit value is respected. Freeze-window tracking is independent of the flag, so `invalidateOnRefresh: false` won't break it.
|
|
159
159
|
|
|
160
160
|
| option | default | description |
|
|
161
161
|
| ------------ | -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
162
|
-
| `trigger` | (required) | The scene element to pin and animate. Must be inside the shared container
|
|
162
|
+
| `trigger` | (required) | The scene element to pin and animate. Must be inside the shared container; throws otherwise |
|
|
163
163
|
| `start` | `'top top'` | Where the scene lands in the viewport while pinned (same idea as sticky's CSS `top`). Same default GSAP itself uses for a pinned trigger. See [position syntax](#position-syntax) |
|
|
164
164
|
| `end` | `'bottom top'` | End of the freeze window. Auto-detects between a dwell distance and a position clause (see [end syntax](#end-syntax)). Same default GSAP uses too: dwell for the trigger's own height, not a fixed distance |
|
|
165
165
|
| `endTrigger` | `trigger` | Reference element when `end` uses position-clause syntax. Must be inside the shared container (see below) |
|
|
@@ -168,19 +168,73 @@ You can specify `onKill`, `invalidateOnRefresh`, and `onRefreshInit`; the module
|
|
|
168
168
|
|
|
169
169
|
Registers an overlap-scroll effect (`trigger` gets pinned while its siblings from `cover` onward scroll normally and rise up to cover it) and returns `ScrollTrigger.Vars`. No tween or dwell spacer is created. Since the return value only describes the freeze window, the effect works even without passing it to `ScrollTrigger.create()`, unless you need `markers` or callbacks.
|
|
170
170
|
|
|
171
|
-
| option | default | description
|
|
172
|
-
| ------------ | ---------------------------- |
|
|
173
|
-
| `trigger` | (required) | The element that gets pinned and covered. Must be
|
|
174
|
-
| `cover` | `trigger.nextElementSibling` | The first element of the covering side; this and all following siblings cover together. Must share the same parent as `trigger`
|
|
175
|
-
| `start` | `'bottom bottom'` | Pinned position of `trigger`. See [position syntax](#position-syntax). Unlike `createStickyTrigger`'s `start`, this doesn't support an absolute scroll position (a bare number): it throws instead
|
|
176
|
-
| `end` | `null` (auto-computed) | End of the freeze window. When `null`, computed automatically as "the distance until `cover`'s top edge reaches the top of the viewport"
|
|
177
|
-
| `endTrigger` | `trigger` | Reference element when `end` uses position-clause syntax
|
|
171
|
+
| option | default | description |
|
|
172
|
+
| ------------ | ---------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
173
|
+
| `trigger` | (required) | The element that gets pinned and covered. Must be inside the shared container; throws otherwise. Its own parent, which doesn't have to be the container itself, bounds how far the rise can run (see below) |
|
|
174
|
+
| `cover` | `trigger.nextElementSibling` | The first element of the covering side; this and all following siblings cover together. Must share the same parent as `trigger` |
|
|
175
|
+
| `start` | `'bottom bottom'` | Pinned position of `trigger`. See [position syntax](#position-syntax). Unlike `createStickyTrigger`'s `start`, this doesn't support an absolute scroll position (a bare number): it throws instead |
|
|
176
|
+
| `end` | `null` (auto-computed) | End of the freeze window. When `null`, computed automatically as "the distance until `cover`'s top edge reaches the top of the viewport" |
|
|
177
|
+
| `endTrigger` | `trigger` | Reference element when `end` uses position-clause syntax |
|
|
178
178
|
|
|
179
179
|
`createOverlapScroll` adjusts `position`/`z-index` on the covering side only when needed, never overrides explicit values, and restores its changes on GSAP `kill()`.
|
|
180
180
|
|
|
181
|
+
#### Room for the rise
|
|
182
|
+
|
|
183
|
+
Pinning here is plain `position:sticky`, and the browser keeps a sticky element stuck only until its containing block's bottom edge catches up with it. That containing block is `trigger`'s own parent, not the shared container:
|
|
184
|
+
|
|
185
|
+
```
|
|
186
|
+
trigger's parent's bottom edge - trigger's bottom edge >= end - start
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
With the auto `end`, that distance is where `cover`'s top edge sits when the freeze starts: `start`'s pinned offset, plus `trigger`'s own height, plus any gap between the two. Under the defaults (`start: 'bottom bottom'`, `cover` directly after `trigger`) it comes to one viewport height; a `trigger` taller than the viewport, pinned near the top, needs more.
|
|
190
|
+
|
|
191
|
+
With less room than that, the browser releases the wrapper mid-rise: `trigger` scrolls away before `cover` has covered it, at a point that moves with the window size, since the requirement scales with the viewport while the room doesn't. Content after `trigger`'s parent never counts, however long the page is. Add the missing height below `trigger` (a taller `cover`, or a spacer after it), or keep the sections that follow inside the same parent rather than wrapping `trigger` and `cover` in a box of their own.
|
|
192
|
+
|
|
193
|
+
#### Delaying the rise
|
|
194
|
+
|
|
195
|
+
`cover` starts rising as soon as the freeze window opens, so a scrubbed effect on `trigger` plays out underneath it. To let the effect finish first, put a Scene layer in front of the cover layer: freezing one freezes the whole shared container, `cover` included, and the scene's dwell doubles as the hold. Since the two layers can't share a trigger element, the cover layer gets a zero-height marker between the scene and `cover`:
|
|
196
|
+
|
|
197
|
+
```html
|
|
198
|
+
<div class="container__inner">
|
|
199
|
+
<section class="scene">...</section>
|
|
200
|
+
<div class="hold" style="height: 0"></div>
|
|
201
|
+
<section class="cover">...</section>
|
|
202
|
+
</div>
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
```ts
|
|
206
|
+
gsap.fromTo(
|
|
207
|
+
".scene",
|
|
208
|
+
{ opacity: 0 },
|
|
209
|
+
{
|
|
210
|
+
opacity: 1,
|
|
211
|
+
scrollTrigger: sticky.createStickyTrigger({
|
|
212
|
+
trigger: ".scene",
|
|
213
|
+
start: "bottom bottom",
|
|
214
|
+
end: "+=800",
|
|
215
|
+
scrub: true,
|
|
216
|
+
}),
|
|
217
|
+
},
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
ScrollTrigger.create(
|
|
221
|
+
sticky.createOverlapScroll({
|
|
222
|
+
trigger: ".hold",
|
|
223
|
+
cover: ".cover",
|
|
224
|
+
start: "bottom bottom",
|
|
225
|
+
}),
|
|
226
|
+
);
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
The rise picks up where the scrub ends, with no arithmetic on your side, because `refresh()` adds a Scene layer's dwell to the freeze window of every layer whose trigger comes later in DOM order. That is also why the marker goes after the scene rather than inside it.
|
|
230
|
+
|
|
231
|
+
The marker sits on the scene's bottom edge, so one `'bottom bottom'` describes both layers and nothing moves at the handover. The scene pins with that edge on the viewport's bottom, and `cover`, which begins there, stays just off screen for the whole hold. Pin the scene anywhere else and you have to reconcile the two clauses by hand, since a zero-height marker resolves any clause to a plain viewport position. Its `start` has to name wherever the scene's bottom edge sits while pinned, or `trigger` jumps.
|
|
232
|
+
|
|
233
|
+
The marker is the cover layer's `trigger`, so [the room](#room-for-the-rise) the rise needs is measured below it, not below the scene.
|
|
234
|
+
|
|
181
235
|
### `createStickyPin(options)`
|
|
182
236
|
|
|
183
|
-
Pins small elements (badges, labels, etc.) with plain `position:sticky` (pinning handled entirely by CSS, not GSAP). Pinning starts when `trigger` reaches its sticky position and releases when `endTrigger` reaches the `end` clause. This is independent of Scene/Cover layers and works the same inside or outside the shared container.
|
|
237
|
+
Pins small elements (badges, labels, etc.) with plain `position:sticky` (pinning handled entirely by CSS, not GSAP). Pinning starts when `trigger` reaches its sticky position and releases when `endTrigger` reaches the `end` clause. This is independent of Scene/Cover layers and works the same inside or outside the shared container. The only `trigger` it rejects is one that encloses the container (see [Constraints and caveats](#constraints-and-caveats)).
|
|
184
238
|
|
|
185
239
|
Because pinning is handled by CSS, the returned `ScrollTrigger.Vars` does not define GSAP `start`/`end`; it exists for hooks such as `onKill`/`onRefreshInit`. Pass it to `ScrollTrigger.create()` if you want automatic cleanup and auto refresh-binding.
|
|
186
240
|
|
|
@@ -210,15 +264,15 @@ sticky.refresh();
|
|
|
210
264
|
| `endTrigger` | (required) | Reference element for releasing the pin |
|
|
211
265
|
| `end` | `'top top'` | Which clause of `endTrigger` releases the pin (standard GSAP [position syntax](#position-syntax)) |
|
|
212
266
|
|
|
213
|
-
Here `start` means only
|
|
267
|
+
Here `start` means only where the element 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 one setting.
|
|
214
268
|
|
|
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
|
|
269
|
+
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, and why `top` is its own option: with the bare-number slot reserved for GSAP's meaning, `'top 20px'` would be a pin's only way to say "20px below the top of the viewport". `end` does take an absolute scroll position (`end: 3000` releases at scroll 3000), since a release point is a real thing to ask for on the scroll axis.
|
|
216
270
|
|
|
217
|
-
|
|
271
|
+
Clauses resolve exactly as they do elsewhere, including the one-token forms that name the element's own side: `'top 20px'` puts the element's top 20px below the viewport's top edge, while `'20px'` puts it 20px above.
|
|
218
272
|
|
|
219
273
|
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: ...`).
|
|
220
274
|
|
|
221
|
-
See the repository's `ARCHITECTURE.md` ("Why `createStickyPin` is unaffected by nested-sticky lag") for why
|
|
275
|
+
See the repository's `ARCHITECTURE.md` ("Why `createStickyPin` is unaffected by nested-sticky lag") for why the lag never applies here, and why `trigger` needs two nested wrapper divs.
|
|
222
276
|
|
|
223
277
|
### `createResolvedTrigger(options)`
|
|
224
278
|
|
|
@@ -274,7 +328,7 @@ Using it with `pin: true` is discouraged because the element can jump when pinni
|
|
|
274
328
|
|
|
275
329
|
Only the dwell of Scene layers registered via `createStickyTrigger` is accumulated here (`createOverlapScroll` never changes the document height, so it doesn't contribute to the lag).
|
|
276
330
|
|
|
277
|
-
|
|
331
|
+
When the target might belong to some other instance than the one at hand, use the static [`getScrollTop`](#stickyscrolltriggergetscrolltopelement-instances) below instead.
|
|
278
332
|
|
|
279
333
|
### `StickyScrollTrigger.getScrollTop(element, instances)`
|
|
280
334
|
|
|
@@ -335,7 +389,7 @@ Pinning decouples an element's position in the document from the scroll position
|
|
|
335
389
|
```
|
|
336
390
|
|
|
337
391
|
- Your own `scroll-margin-top` still applies. The correction is added to whatever value the element already computes to, never written over it, re-read on every `refresh()` (so a later change, e.g. a responsive breakpoint, is picked up too), and `destroy()` puts the original inline value back
|
|
338
|
-
- A fixed header's
|
|
392
|
+
- A fixed header's offset is a different case, applying to every scroll rather than only those inside the shared container. CSSOM View says `scroll-padding-top` (on the scroller) and `scroll-margin-top` (on the target) add together, so in principle the header offset could live there, independently of this module's correction. In practice, don't: Firefox drops `scroll-padding-top` from a fragment jump once any `position:sticky` element on the page has engaged, landing short by exactly the header height (see ARCHITECTURE.md's "Firefox drops scroll-padding-top" for how this was verified). Fold it into `--sst-scroll-margin-top-offset` instead (below), as the repository's demo does in `style.css`'s `html` rule
|
|
339
393
|
- To land deliberately short of or past a target, set the `--sst-scroll-margin-top-offset` custom property (a length) on it, or on any ancestor to cover several targets at once (it inherits like any other custom property). A positive value lands short, settling below the viewport's top edge instead of flush with it; negative overshoots. Being a plain `var()`, the browser reads it live at scroll-into-view time, so unlike the author-`scroll-margin-top` case above, no `refresh()` call is needed for a change to take effect
|
|
340
394
|
|
|
341
395
|
```css
|
|
@@ -372,13 +426,17 @@ A px/% offset needs the `=` when it follows a keyword or number (`top+=50`, `80-
|
|
|
372
426
|
|
|
373
427
|
A trailing `px` (`top 100px`, `center+=50px`) is accepted and ignored, the same idea as GSAP's own `_offsetToPx`, which only special-cases `%` and otherwise passes the value through `parseFloat`. This module isn't as lenient as GSAP toward other unrecognized suffixes, though: an actually-unsupported unit like `top 100vh` still throws, rather than silently behaving like `top 100`.
|
|
374
428
|
|
|
429
|
+
The table above is the whole accepted grammar. GSAP's `clamp()` wrapper (`start: 'clamp(top center)'`, which holds the resolved position inside the scroller's own range) is not part of it and throws. Nothing clamps the freeze window either, so a clause that resolves past the top of the document keeps its negative value: a Scene layer sitting at the very start of the container with `start: 'top bottom'` begins one viewport height before scroll 0. `end` is the exception, already clamped to `start`.
|
|
430
|
+
|
|
431
|
+
GSAP passes the `ScrollTrigger` instance to its own function-valued `start`/`end` (`start: (self) => ...`), and this module doesn't, so a function declaring that parameter is a TypeScript error. There is nothing to pass: this module's `refresh()` resolves these functions, and it runs whether or not the returned `ScrollTrigger.Vars` was ever handed to `ScrollTrigger.create()`. `self.start`/`self.end` would be circular anyway, since the freeze window they report is what `refresh()` is in the middle of computing.
|
|
432
|
+
|
|
375
433
|
### Absolute scroll position
|
|
376
434
|
|
|
377
435
|
If the entire value is just a number (a plain JS number, or a string that's nothing but digits, with no keyword, second token, or `%`/`px` suffix), GSAP treats it as an absolute scroll position rather than a clause, ignoring the reference element entirely (`ScrollTrigger.js`'s `_parsePosition`: `isNaN(value) || (value = +value)`). This module follows the same rule for `start` and `end`, and for [`createStickyPin`](#createstickypinoptions)'s `end`, [`createResolvedTrigger`](#createresolvedtriggeroptions)'s `start`/`end`, and [`resolveScrollPosition`](#resolvescrollpositionelement-position)'s `position`.
|
|
378
436
|
|
|
379
437
|
`start: '500'` (or `start: 500`) freezes starting at absolute scroll position 500px, ignoring the trigger's natural position and any preceding dwell. `end: '500'` ends the freeze window at 500px the same way, clamped to `start` if that would put `end` before it, matching GSAP's own `end = Math.max(start, ...)`.
|
|
380
438
|
|
|
381
|
-
`'500 top'` (two tokens) and `'500px'` (a suffix) don't qualify
|
|
439
|
+
`'500 top'` (two tokens) and `'500px'` (a suffix) don't qualify; they resolve as offsets via the bare-number row in the table above, same as GSAP.
|
|
382
440
|
|
|
383
441
|
> [`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.
|
|
384
442
|
|
|
@@ -409,9 +467,7 @@ sticky.createStickyTrigger({
|
|
|
409
467
|
});
|
|
410
468
|
```
|
|
411
469
|
|
|
412
|
-
If `endTrigger` points to another registered layer, its position is resolved using the same computation this module already does for that layer.
|
|
413
|
-
|
|
414
|
-
A `createStickyTrigger` Scene layer can't forward-reference a later layer this way: its own dwell padding always precedes and pushes down everything after it, so the reference would depend on its own dwell and never converge. It throws immediately instead of trying.
|
|
470
|
+
If `endTrigger` points to another registered layer, its position is resolved using the same computation this module already does for that layer. A forward reference (pointing to a layer later in DOM order) only works from `createOverlapScroll`'s cover layer, which adds no padding and so doesn't depend on its own dwell. A `createStickyTrigger` Scene layer throws immediately instead: its own dwell padding pushes down everything after it, so the reference would depend on that dwell and never converge.
|
|
415
471
|
|
|
416
472
|
For an unregistered `endTrigger`, its raw DOM position is measured directly, then adjusted by the dwell of every registered Scene layer structurally positioned before it, including ones registered after this call, if their `trigger` sits between this layer's `trigger` and `endTrigger`.
|
|
417
473
|
|
|
@@ -419,7 +475,9 @@ For an unregistered `endTrigger`, its raw DOM position is measured directly, the
|
|
|
419
475
|
>
|
|
420
476
|
> `createOverlapScroll` isn't subject to this restriction.
|
|
421
477
|
|
|
422
|
-
"`'max'` keyword":
|
|
478
|
+
"`'max'` keyword": the scroller's max scroll position, optionally offset (`'max-=100'`, `'max+=10%'`). Ignores `endTrigger` entirely.
|
|
479
|
+
|
|
480
|
+
Bare `'max'` matches GSAP's own `end: 'max'` notation exactly. The offset forms are this module's own extension, not a GSAP reproduction: raw GSAP 3.15.0 silently drops both the offset and the `'max'` itself for `end: 'max-=100'`/`'max+=10%'`, collapsing `end` down to `start`'s position.
|
|
423
481
|
|
|
424
482
|
```ts
|
|
425
483
|
sticky.createOverlapScroll({
|
|
@@ -429,17 +487,22 @@ sticky.createOverlapScroll({
|
|
|
429
487
|
}); // pin until the very bottom of the page
|
|
430
488
|
```
|
|
431
489
|
|
|
432
|
-
`'max'` is only supported by [`createOverlapScroll`](#createoverlapscrolloptions) and [`resolveScrollPosition`](#resolvescrollpositionelement-position)/[`createResolvedTrigger`](#createresolvedtriggeroptions). `createStickyTrigger` and `createStickyPin` throw
|
|
490
|
+
`'max'` is only supported by [`createOverlapScroll`](#createoverlapscrolloptions) and [`resolveScrollPosition`](#resolvescrollpositionelement-position)/[`createResolvedTrigger`](#createresolvedtriggeroptions). `createStickyTrigger` and `createStickyPin` throw on it, because their own dwell padding or pin spacer adds to the document height `'max'` measures: the freeze window would depend on itself, growing the page a little more on every `refresh()`. GSAP defines `'max'` for `end`, not `start`: in raw GSAP 3.15.0, `start: 'max'` silently resolves to `0` rather than the scroller's max, so this module rejects it for `start` instead of reproducing that.
|
|
433
491
|
|
|
434
492
|
## Constraints and caveats
|
|
435
493
|
|
|
436
494
|
- The structure directly under the shared container changes because of the added wrapper elements. Direct-child selectors like `.container__inner > .scene`, `:nth-child()`, adjacent-sibling selectors (`+`/`~`), and flex/grid layout on the shared container itself may break across the wrapper boundary
|
|
437
495
|
- `position:sticky` doesn't work if any ancestor of the pinned target has `overflow:hidden`/`clip`
|
|
438
496
|
- `createOverlapScroll`'s `trigger` and `cover` must be siblings sharing the same parent
|
|
497
|
+
- That shared parent also caps how far `cover` can rise: it needs `end - start` worth of content below `trigger`, or the browser releases the wrapper mid-rise, at a point that shifts with the window size (see [Room for the rise](#room-for-the-rise))
|
|
498
|
+
- `createStickyTrigger` and `createOverlapScroll` throw if `trigger` isn't inside the shared container. Both build their structure inside it, so a `trigger` elsewhere would wrap, move and style elements the instance doesn't own
|
|
499
|
+
- `createStickyPin` works on either side of the container, and throws only for a `trigger` that encloses it (the container itself, or an ancestor). A pin wraps `trigger` in a `height:0`, `contain:layout` box, which would pull the container and the layers' dwell padding out of the flow
|
|
500
|
+
- Selector strings for `trigger`/`endTrigger`/`cover`/`element` resolve against the whole document, as GSAP's own `trigger` does, rather than within the shared container; only the `trigger` rules above restrict where a match may land. Validate or scope these values yourself if they come from content you don't control
|
|
439
501
|
- ScrollTrigger's `pin`/`pinSpacing`/`anticipatePin` can't be used (pinning is handled by sticky; these are already excluded at the type level)
|
|
440
|
-
-
|
|
502
|
+
- GSAP's `clamp()` position wrapper isn't accepted, and a function-valued `start`/`end` is called with no arguments rather than with the `ScrollTrigger` instance GSAP passes (see [Position syntax](#position-syntax))
|
|
503
|
+
- A `createStickyTrigger` Scene layer can't point `endTrigger` at a registered layer later in DOM order; `createOverlapScroll`'s cover layer can, since it adds no padding (see [End syntax](#end-syntax))
|
|
441
504
|
- If a Scene layer's `end` uses a position clause, don't set `endTrigger` to an element that gets pushed down by that same scene's own dwell; the value won't converge
|
|
442
|
-
- If several layers' unregistered or cover-layer `endTrigger`s
|
|
505
|
+
- If several layers' unregistered or cover-layer `endTrigger`s form a dependency cycle, `refresh()` throws instead of settling on a wrong value
|
|
443
506
|
- `end: 'max'` throws on `createStickyTrigger` and `createStickyPin` for the same reason (their own padding/spacer would depend on itself); use `createOverlapScroll` instead
|
|
444
507
|
- Using the same element as the `trigger` of two different `createStickyTrigger`/`createOverlapScroll`/`createStickyPin` calls throws
|
|
445
508
|
- You must call `refresh()` once manually after registration. Window resize/load recomputation is automatically wired to GSAP's own `refreshInit`, but for layout changes that don't involve those (e.g. content height changes), call `ScrollTrigger.refresh()` yourself (see [Calling refresh](#calling-refresh))
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @license
|
|
3
|
-
* StickyScrollTrigger v0.
|
|
3
|
+
* StickyScrollTrigger v0.6.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
|
|
@@ -41,7 +41,9 @@ export declare type EndInput = EndValue | (() => EndValue);
|
|
|
41
41
|
|
|
42
42
|
export declare type EndValue = string | number;
|
|
43
43
|
|
|
44
|
-
declare
|
|
44
|
+
declare const EXCLUDED_VAR_KEYS: readonly ["trigger", "start", "end", "endTrigger", "pin", "pinSpacing", "anticipatePin", "pinnedContainer", "pinReparent", "pinSpacer", "pinType", "horizontal", "scroller", "containerAnimation"];
|
|
45
|
+
|
|
46
|
+
declare type PassThroughVars = Omit<ScrollTrigger.Vars, typeof EXCLUDED_VAR_KEYS[number]>;
|
|
45
47
|
|
|
46
48
|
export declare type PositionInput = PositionValue | (() => PositionValue);
|
|
47
49
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @license
|
|
3
|
-
* StickyScrollTrigger v0.
|
|
3
|
+
* StickyScrollTrigger v0.6.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
|
|
@@ -66,6 +66,14 @@ var StickyScrollTrigger = (function() {
|
|
|
66
66
|
el.style.position = "sticky";
|
|
67
67
|
el.style.top = `${topPx}px`;
|
|
68
68
|
};
|
|
69
|
+
const captureInlinePosition = (el) => ({
|
|
70
|
+
position: el.style.position,
|
|
71
|
+
top: el.style.top
|
|
72
|
+
});
|
|
73
|
+
const restoreInlinePosition = (el, saved) => {
|
|
74
|
+
el.style.position = saved.position;
|
|
75
|
+
el.style.top = saved.top;
|
|
76
|
+
};
|
|
69
77
|
const wrapScene = (inner) => {
|
|
70
78
|
if (!inner.parentNode) throw new Error(`StickyScrollTrigger: ${describeElement(inner)} is not attached to the document`);
|
|
71
79
|
const container = document.createElement("div");
|
|
@@ -115,9 +123,9 @@ var StickyScrollTrigger = (function() {
|
|
|
115
123
|
inner
|
|
116
124
|
};
|
|
117
125
|
};
|
|
118
|
-
const unwrapPin = (outer, trigger) => {
|
|
126
|
+
const unwrapPin = (outer, trigger, saved) => {
|
|
119
127
|
const parent = outer.parentNode;
|
|
120
|
-
|
|
128
|
+
restoreInlinePosition(trigger, saved);
|
|
121
129
|
if (parent) parent.insertBefore(trigger, outer);
|
|
122
130
|
outer.remove();
|
|
123
131
|
};
|
|
@@ -306,14 +314,17 @@ var StickyScrollTrigger = (function() {
|
|
|
306
314
|
const magnitude = unit === "%" ? parseFloat(value) / 100 * refSize : parseFloat(value);
|
|
307
315
|
return sign?.startsWith("-") ? -magnitude : magnitude;
|
|
308
316
|
};
|
|
309
|
-
const CLAUSE_TOKEN_RE = /^(top|center|bottom|(?:\d
|
|
317
|
+
const CLAUSE_TOKEN_RE = /^(top|center|bottom|(?:\d+(?:\.\d*)?|\.\d+))?(%|px)?(?:([+-]=?)(\d+(?:\.\d*)?|\.\d+)(%|px)?)?$/;
|
|
310
318
|
const parseClauseToken = (token, refSize) => {
|
|
311
319
|
const match = CLAUSE_TOKEN_RE.exec(token);
|
|
312
|
-
if (!match || !match[1] && !match[3])
|
|
320
|
+
if (!match || !match[1] && !match[3]) {
|
|
321
|
+
if (MAX_TOKEN_RE.test(token)) throw new Error(`StickyScrollTrigger: unsupported position clause "${token}": 'max' is GSAP's end-only keyword for the scroller's maximum scroll position.`);
|
|
322
|
+
throw new Error(`StickyScrollTrigger: unsupported position clause "${token}"`);
|
|
323
|
+
}
|
|
313
324
|
const [, base, baseUnit, sign, offsetValue, offsetUnit] = match;
|
|
314
325
|
if (base !== void 0 && sign && !sign.includes("=")) {
|
|
315
326
|
const suggestion = `${base}${baseUnit ?? ""}${sign}=${offsetValue}${offsetUnit ?? ""}`;
|
|
316
|
-
throw new Error(`StickyScrollTrigger: unsupported position clause "${token}": a signed offset needs an explicit '=' when it follows a keyword or number (GSAP
|
|
327
|
+
throw new Error(`StickyScrollTrigger: unsupported position clause "${token}": a signed offset needs an explicit '=' when it follows a keyword or number (GSAP needs it to tell "${base}" from the offset). Did you mean "${suggestion}"?`);
|
|
317
328
|
}
|
|
318
329
|
const isKeyword = base !== void 0 && base in KEYWORD_FRACTIONS;
|
|
319
330
|
const basePercent = baseUnit === "%";
|
|
@@ -327,13 +338,19 @@ var StickyScrollTrigger = (function() {
|
|
|
327
338
|
return resolved.trim() !== "" && !Number.isNaN(Number(resolved));
|
|
328
339
|
};
|
|
329
340
|
const resolveAbsolute = (resolved) => typeof resolved === "number" ? resolved : Number(resolved);
|
|
341
|
+
const CLAMP_PREFIX = "clamp(";
|
|
330
342
|
const resolveAnchorTop = (position, elementHeight, viewportHeight) => {
|
|
331
|
-
const
|
|
343
|
+
const trimmed = position.trim();
|
|
344
|
+
if (trimmed.startsWith(CLAMP_PREFIX)) {
|
|
345
|
+
const inner = trimmed.slice(6).replace(/\)$/, "").trim();
|
|
346
|
+
throw new Error(`StickyScrollTrigger: unsupported position clause "${trimmed}": GSAP's clamp() wrapper isn't supported here.` + (inner ? ` Did you mean "${inner}"?` : ""));
|
|
347
|
+
}
|
|
348
|
+
const [elementToken, viewportToken = "top"] = trimmed.split(/\s+/);
|
|
332
349
|
const elementClause = parseClauseToken(elementToken, elementHeight);
|
|
333
350
|
const viewportClause = parseClauseToken(viewportToken, viewportHeight);
|
|
334
351
|
return viewportHeight * viewportClause.fraction + viewportClause.offsetPx - (elementHeight * elementClause.fraction + elementClause.offsetPx);
|
|
335
352
|
};
|
|
336
|
-
const DWELL_RELATIVE_RE = /^\+=(\d
|
|
353
|
+
const DWELL_RELATIVE_RE = /^\+=(\d+(?:\.\d*)?|\.\d+)(%|px)?$/;
|
|
337
354
|
const isDwellFormat = (resolved) => typeof resolved === "string" && DWELL_RELATIVE_RE.test(resolved.trim());
|
|
338
355
|
const resolveDwell = (resolved, viewportHeight) => {
|
|
339
356
|
const match = DWELL_RELATIVE_RE.exec(String(resolved).trim());
|
|
@@ -341,7 +358,7 @@ var StickyScrollTrigger = (function() {
|
|
|
341
358
|
const [, value, unit] = match;
|
|
342
359
|
return signedPx(void 0, value, unit, viewportHeight);
|
|
343
360
|
};
|
|
344
|
-
const MAX_TOKEN_RE = /^max(?:([+-]=)(\d
|
|
361
|
+
const MAX_TOKEN_RE = /^max(?:([+-]=)(\d+(?:\.\d*)?|\.\d+)(%|px)?)?$/;
|
|
345
362
|
const isMaxFormat = (resolved) => typeof resolved === "string" && MAX_TOKEN_RE.test(resolved.trim());
|
|
346
363
|
const resolveMaxOffset = (resolved, viewportHeight) => {
|
|
347
364
|
const match = MAX_TOKEN_RE.exec(resolved.trim());
|
|
@@ -445,11 +462,27 @@ var StickyScrollTrigger = (function() {
|
|
|
445
462
|
plans = next.plans;
|
|
446
463
|
naturalTops = next.naturalTops;
|
|
447
464
|
if (stable) break;
|
|
448
|
-
if (pass === measurements.length - 1) throw new Error("StickyScrollTrigger: could not resolve endTrigger positions: some
|
|
465
|
+
if (pass === measurements.length - 1) throw new Error("StickyScrollTrigger: could not resolve endTrigger positions: some endTrigger references form a circular structural dependency, each layer's end depending on a layer whose own end depends back on it. Point each endTrigger at a layer that doesn't depend on it.");
|
|
449
466
|
}
|
|
450
467
|
plans.forEach((plan, index) => deps.onPlanned(index, plan));
|
|
451
468
|
return plans;
|
|
452
469
|
};
|
|
470
|
+
const EXCLUDED_VAR_KEYS = [
|
|
471
|
+
"trigger",
|
|
472
|
+
"start",
|
|
473
|
+
"end",
|
|
474
|
+
"endTrigger",
|
|
475
|
+
"pin",
|
|
476
|
+
"pinSpacing",
|
|
477
|
+
"anticipatePin",
|
|
478
|
+
"pinnedContainer",
|
|
479
|
+
"pinReparent",
|
|
480
|
+
"pinSpacer",
|
|
481
|
+
"pinType",
|
|
482
|
+
"horizontal",
|
|
483
|
+
"scroller",
|
|
484
|
+
"containerAnimation"
|
|
485
|
+
];
|
|
453
486
|
const resolveEndSpec = (rootElement, viewportHeight, layer, endResolved, ownIndex, endTriggerIndex) => {
|
|
454
487
|
if (endResolved === null) return { mode: "auto" };
|
|
455
488
|
if (isDwellFormat(endResolved)) return {
|
|
@@ -461,16 +494,16 @@ var StickyScrollTrigger = (function() {
|
|
|
461
494
|
value: resolveAbsolute(endResolved)
|
|
462
495
|
};
|
|
463
496
|
if (isMaxFormat(endResolved)) {
|
|
464
|
-
if (layer.kind !== "cover") throw new Error(`StickyScrollTrigger: createStickyTrigger's trigger ${describeElement(layer.trigger)} has end "${endResolved}"
|
|
497
|
+
if (layer.kind !== "cover") throw new Error(`StickyScrollTrigger: createStickyTrigger's trigger ${describeElement(layer.trigger)} has end "${endResolved}". GSAP's 'max' keyword isn't supported here: the layer's own dwell padding adds to the document's max scroll position, so the freeze window would depend on itself. Use createOverlapScroll, which adds no padding, or a dwell distance such as '+=500'.`);
|
|
465
498
|
return {
|
|
466
499
|
mode: "max",
|
|
467
500
|
offsetPx: resolveMaxOffset(endResolved, viewportHeight)
|
|
468
501
|
};
|
|
469
502
|
}
|
|
470
|
-
if (layer.kind !== "cover" && endTriggerIndex !== null && endTriggerIndex > ownIndex) throw new Error(`StickyScrollTrigger: createStickyTrigger's trigger ${describeElement(layer.trigger)}'s endTrigger (${describeElement(layer.endTrigger)}) refers to a layer positioned later in DOM order
|
|
503
|
+
if (layer.kind !== "cover" && endTriggerIndex !== null && endTriggerIndex > ownIndex) throw new Error(`StickyScrollTrigger: createStickyTrigger's trigger ${describeElement(layer.trigger)}'s endTrigger (${describeElement(layer.endTrigger)}) refers to a layer positioned later in DOM order, which isn't supported: this layer's own dwell padding pushes that one down, so the freeze window would depend on its own dwell. Point endTrigger at a layer earlier in DOM order, or use a dwell distance such as '+=500'.`);
|
|
471
504
|
const unregistered = endTriggerIndex === null;
|
|
472
505
|
const insideRoot = rootElement.contains(layer.endTrigger);
|
|
473
|
-
if (unregistered && !insideRoot && layer.kind !== "cover") throw new Error(`StickyScrollTrigger: createStickyTrigger's trigger ${describeElement(layer.trigger)} has an endTrigger (${describeElement(layer.endTrigger)}) outside the shared container
|
|
506
|
+
if (unregistered && !insideRoot && layer.kind !== "cover") throw new Error(`StickyScrollTrigger: createStickyTrigger's trigger ${describeElement(layer.trigger)} has an endTrigger (${describeElement(layer.endTrigger)}) outside the shared container: the layer's own dwell keeps pushing it away, so the freeze window never settles. Point endTrigger at an element inside the container, or use a dwell distance such as '+=500'.`);
|
|
474
507
|
return {
|
|
475
508
|
mode: "clause",
|
|
476
509
|
clause: endResolved,
|
|
@@ -483,7 +516,7 @@ var StickyScrollTrigger = (function() {
|
|
|
483
516
|
mode: "clause",
|
|
484
517
|
anchorOffset: resolveAnchorTop(startResolved, elementHeight, viewportHeight)
|
|
485
518
|
};
|
|
486
|
-
if (layer.kind === "cover") throw new Error(`StickyScrollTrigger: createOverlapScroll's trigger ${describeElement(layer.trigger)} has start "${startResolved}", an absolute scroll position (GSAP
|
|
519
|
+
if (layer.kind === "cover") throw new Error(`StickyScrollTrigger: createOverlapScroll's trigger ${describeElement(layer.trigger)} has start "${startResolved}", an absolute scroll position (GSAP reads any bare number this way). That isn't supported here: a cover layer's sticky position is computed relative to its own wrapper, so start must be a position clause such as 'bottom bottom'.`);
|
|
487
520
|
return {
|
|
488
521
|
mode: "absolute",
|
|
489
522
|
value: resolveAbsolute(startResolved)
|
|
@@ -535,6 +568,11 @@ var StickyScrollTrigger = (function() {
|
|
|
535
568
|
this.#rootElement = resolveRoot(root);
|
|
536
569
|
this.#scrollMarginSync = createScrollMarginSync(this.#rootElement);
|
|
537
570
|
this.#scrollMarginTargets = options.scrollMarginTargets === void 0 ? "[id]" : options.scrollMarginTargets;
|
|
571
|
+
if (this.#scrollMarginTargets !== null) try {
|
|
572
|
+
this.#rootElement.querySelectorAll(this.#scrollMarginTargets);
|
|
573
|
+
} catch {
|
|
574
|
+
throw new Error(`StickyScrollTrigger: scrollMarginTargets "${this.#scrollMarginTargets}" is not a valid CSS selector.`);
|
|
575
|
+
}
|
|
538
576
|
}
|
|
539
577
|
#unbuild() {
|
|
540
578
|
if (!this.#built) return;
|
|
@@ -564,13 +602,15 @@ var StickyScrollTrigger = (function() {
|
|
|
564
602
|
#createAutoRefreshHandler(list, entry, userOnRefreshInit) {
|
|
565
603
|
return (self) => {
|
|
566
604
|
this.#captureScrollTriggerClass(self);
|
|
605
|
+
const result = userOnRefreshInit?.(self);
|
|
567
606
|
if (list[0] === entry) this.refresh();
|
|
568
|
-
|
|
607
|
+
return result;
|
|
569
608
|
};
|
|
570
609
|
}
|
|
571
610
|
#wrapUnwrappedPins() {
|
|
572
611
|
this.#pinLayers.forEach((layer) => {
|
|
573
612
|
if (layer.outer) return;
|
|
613
|
+
layer.savedPosition = captureInlinePosition(layer.trigger);
|
|
574
614
|
const wrapped = wrapPin(layer.trigger);
|
|
575
615
|
layer.outer = wrapped.outer;
|
|
576
616
|
layer.inner = wrapped.inner;
|
|
@@ -590,28 +630,45 @@ var StickyScrollTrigger = (function() {
|
|
|
590
630
|
});
|
|
591
631
|
};
|
|
592
632
|
}
|
|
633
|
+
#resetPinState() {
|
|
634
|
+
const snapshots = this.#pinLayers.map((layer) => ({
|
|
635
|
+
layer,
|
|
636
|
+
position: captureInlinePosition(layer.trigger),
|
|
637
|
+
height: layer.inner ? layer.inner.style.height : ""
|
|
638
|
+
}));
|
|
639
|
+
snapshots.forEach(({ layer }) => {
|
|
640
|
+
resetStickyPosition(layer.trigger);
|
|
641
|
+
if (layer.inner) layer.inner.style.height = "";
|
|
642
|
+
});
|
|
643
|
+
return () => {
|
|
644
|
+
snapshots.forEach(({ layer, position, height }) => {
|
|
645
|
+
restoreInlinePosition(layer.trigger, position);
|
|
646
|
+
if (layer.inner) layer.inner.style.height = height;
|
|
647
|
+
});
|
|
648
|
+
};
|
|
649
|
+
}
|
|
593
650
|
#refreshPins(viewportHeight) {
|
|
594
651
|
if (!this.#pinLayers.length) return;
|
|
595
652
|
const restoreSceneCoverStickyState = this.#resetSceneCoverStickyState();
|
|
653
|
+
const restorePinState = this.#resetPinState();
|
|
596
654
|
try {
|
|
597
655
|
this.#refreshPinLayers(viewportHeight);
|
|
656
|
+
} catch (error) {
|
|
657
|
+
restorePinState();
|
|
658
|
+
throw error;
|
|
598
659
|
} finally {
|
|
599
660
|
restoreSceneCoverStickyState();
|
|
600
661
|
}
|
|
601
662
|
}
|
|
602
663
|
#refreshPinLayers(viewportHeight) {
|
|
603
|
-
this.#pinLayers.forEach((layer) => {
|
|
604
|
-
resetStickyPosition(layer.trigger);
|
|
605
|
-
if (layer.inner) layer.inner.style.height = "";
|
|
606
|
-
});
|
|
607
664
|
this.#pinLayers.forEach((layer) => {
|
|
608
665
|
if (!layer.inner) return;
|
|
609
666
|
const resolvedEnd = resolveMaybeFn(layer.end);
|
|
610
|
-
if (isMaxFormat(resolvedEnd)) throw new Error(`StickyScrollTrigger: createStickyPin's end "${resolvedEnd}" uses GSAP's 'max' keyword, which isn't supported here
|
|
667
|
+
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 adds to the document's max scroll position, so it would depend on itself. Use a position clause against endTrigger, such as 'top top'.`);
|
|
611
668
|
const resolvedStart = resolveMaybeFn(layer.start);
|
|
612
669
|
const triggerTop = documentTop(layer.trigger);
|
|
613
670
|
const triggerHeight = layer.trigger.offsetHeight;
|
|
614
|
-
if (isAbsoluteFormat(resolvedStart)) throw new Error(`StickyScrollTrigger: createStickyPin's start "${resolvedStart}" is an absolute scroll position (GSAP
|
|
671
|
+
if (isAbsoluteFormat(resolvedStart)) throw new Error(`StickyScrollTrigger: createStickyPin's start "${resolvedStart}" is an absolute scroll position (GSAP reads any bare number 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
672
|
const topPx = resolveAnchorTop(resolvedStart, triggerHeight, viewportHeight);
|
|
616
673
|
const height = (isAbsoluteFormat(resolvedEnd) ? resolveAbsolute(resolvedEnd) : documentTop(layer.endTrigger) - resolveAnchorTop(resolvedEnd, layer.endTrigger.offsetHeight, viewportHeight)) - triggerTop + topPx + triggerHeight;
|
|
617
674
|
applyStickyPosition(layer.trigger, topPx);
|
|
@@ -620,13 +677,17 @@ var StickyScrollTrigger = (function() {
|
|
|
620
677
|
}
|
|
621
678
|
#refreshScenesAndCovers(viewportHeight) {
|
|
622
679
|
if (!this.#layers.length) return;
|
|
623
|
-
const active =
|
|
624
|
-
this.#layers.forEach((layer) => {
|
|
625
|
-
if (!layer.wrapper) return;
|
|
626
|
-
resetStickyPosition(layer.wrapper);
|
|
627
|
-
active.push(layer);
|
|
628
|
-
});
|
|
680
|
+
const active = this.#layers.filter((layer) => layer.wrapper !== null);
|
|
629
681
|
if (!active.length) return;
|
|
682
|
+
const restoreStickyState = this.#resetSceneCoverStickyState();
|
|
683
|
+
try {
|
|
684
|
+
this.#planLayerPositions(viewportHeight, active);
|
|
685
|
+
} catch (error) {
|
|
686
|
+
restoreStickyState();
|
|
687
|
+
throw error;
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
#planLayerPositions(viewportHeight, active) {
|
|
630
691
|
const structureTop = this.#outermostContainer ? documentTop(this.#outermostContainer) : 0;
|
|
631
692
|
const indexByTrigger = new Map();
|
|
632
693
|
active.forEach((layer, index) => {
|
|
@@ -673,12 +734,26 @@ var StickyScrollTrigger = (function() {
|
|
|
673
734
|
freezeEnd
|
|
674
735
|
})), this.#outermostContainer, this.#scrollMarginTargets);
|
|
675
736
|
}
|
|
737
|
+
#assertNoExcludedVars(rest, context) {
|
|
738
|
+
const found = EXCLUDED_VAR_KEYS.filter((key) => key in rest);
|
|
739
|
+
if (found.length === 0) return;
|
|
740
|
+
throw new Error(`${context}: ${found.join(", ")} ${found.length > 1 ? "are" : "is"} not supported here; this module handles pinning and the scroll axis itself.`);
|
|
741
|
+
}
|
|
676
742
|
#assertTriggerAvailable(list, trigger, message) {
|
|
677
743
|
if (hasDuplicateTrigger(list, trigger)) throw new Error(message);
|
|
678
744
|
}
|
|
745
|
+
#assertTriggerInsideRoot(trigger, context) {
|
|
746
|
+
if (trigger === this.#rootElement) throw new Error(`${context}: trigger ${describeElement(trigger)} is the shared container itself. Point trigger at an element inside the container.`);
|
|
747
|
+
if (!this.#rootElement.contains(trigger)) throw new Error(`${context}: trigger ${describeElement(trigger)} is outside the shared container. Point trigger at an element inside it, or use createStickyPin, which works on either side.`);
|
|
748
|
+
}
|
|
749
|
+
#assertTriggerNotEnclosingRoot(trigger, context) {
|
|
750
|
+
if (!trigger.contains(this.#rootElement)) return;
|
|
751
|
+
const relation = trigger === this.#rootElement ? "is the shared container itself" : "is an ancestor of the shared container";
|
|
752
|
+
throw new Error(`${context}: trigger ${describeElement(trigger)} ${relation}. A pin wraps trigger in a height:0, contain:layout box, which would pull the container and every layer's dwell padding out of the flow. Pin an element that doesn't contain the container.`);
|
|
753
|
+
}
|
|
679
754
|
#registerLayer(layer, onKill, rest) {
|
|
680
755
|
if (this.#destroyed) throw new Error("StickyScrollTrigger: cannot register a new layer after destroy() has been called.");
|
|
681
|
-
this.#assertTriggerAvailable(this.#layers, layer.trigger, `StickyScrollTrigger: ${describeElement(layer.trigger)} is already registered as a trigger for another layer. Each element can
|
|
756
|
+
this.#assertTriggerAvailable(this.#layers, layer.trigger, `StickyScrollTrigger: ${describeElement(layer.trigger)} is already registered as a trigger for another layer. Each element can be used once per controller instance, across createStickyTrigger and createOverlapScroll: reusing one makes endTrigger resolution ambiguous.`);
|
|
682
757
|
this.#assertTriggerAvailable(this.#pinLayers, layer.trigger, `StickyScrollTrigger: ${describeElement(layer.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.`);
|
|
683
758
|
this.#layers.push(layer);
|
|
684
759
|
this.#dirty = true;
|
|
@@ -700,7 +775,9 @@ var StickyScrollTrigger = (function() {
|
|
|
700
775
|
};
|
|
701
776
|
}
|
|
702
777
|
createStickyTrigger({ trigger: triggerInput, start = "0 0", end = "100% 0", endTrigger: endTriggerInput, onKill, ...rest }) {
|
|
778
|
+
this.#assertNoExcludedVars(rest, "createStickyTrigger");
|
|
703
779
|
const trigger = resolveElement(triggerInput, "createStickyTrigger");
|
|
780
|
+
this.#assertTriggerInsideRoot(trigger, "createStickyTrigger");
|
|
704
781
|
const endTrigger = resolveEndTrigger(trigger, endTriggerInput, "createStickyTrigger");
|
|
705
782
|
return this.#registerLayer({
|
|
706
783
|
kind: "scene",
|
|
@@ -716,7 +793,9 @@ var StickyScrollTrigger = (function() {
|
|
|
716
793
|
}, onKill, rest);
|
|
717
794
|
}
|
|
718
795
|
createOverlapScroll({ trigger: triggerInput, cover, start = "bottom bottom", end = null, endTrigger: endTriggerInput, onKill, ...rest }) {
|
|
796
|
+
this.#assertNoExcludedVars(rest, "createOverlapScroll");
|
|
719
797
|
const trigger = resolveElement(triggerInput, "createOverlapScroll");
|
|
798
|
+
this.#assertTriggerInsideRoot(trigger, "createOverlapScroll");
|
|
720
799
|
const endTrigger = resolveEndTrigger(trigger, endTriggerInput, "createOverlapScroll");
|
|
721
800
|
const coverElement = cover === void 0 ? trigger.nextElementSibling : resolveElement(cover, "createOverlapScroll");
|
|
722
801
|
if (!(coverElement instanceof HTMLElement)) throw new Error(`createOverlapScroll: cover element (trigger.nextElementSibling) not found for trigger ${describeElement(trigger)}`);
|
|
@@ -741,14 +820,17 @@ var StickyScrollTrigger = (function() {
|
|
|
741
820
|
return vars;
|
|
742
821
|
}
|
|
743
822
|
createStickyPin({ trigger: triggerInput, start, top, endTrigger: endTriggerInput, end = "top top", onKill, ...rest }) {
|
|
823
|
+
this.#assertNoExcludedVars(rest, "createStickyPin");
|
|
744
824
|
if (this.#destroyed) throw new Error("StickyScrollTrigger: cannot register a new pin after destroy() has been called.");
|
|
745
825
|
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.");
|
|
746
826
|
const trigger = resolveElement(triggerInput, "createStickyPin");
|
|
747
827
|
const endTrigger = resolveElement(endTriggerInput, "createStickyPin");
|
|
828
|
+
this.#assertTriggerNotEnclosingRoot(trigger, "createStickyPin");
|
|
748
829
|
this.#assertTriggerAvailable(this.#pinLayers, trigger, `StickyScrollTrigger: ${describeElement(trigger)} is already registered as a pin trigger.`);
|
|
749
830
|
this.#assertTriggerAvailable(this.#layers, trigger, `StickyScrollTrigger: ${describeElement(trigger)} 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.`);
|
|
750
831
|
const layer = {
|
|
751
832
|
trigger,
|
|
833
|
+
savedPosition: captureInlinePosition(trigger),
|
|
752
834
|
outer: null,
|
|
753
835
|
inner: null,
|
|
754
836
|
start: top === void 0 ? start ?? "top top" : topToStartClause(top),
|
|
@@ -764,7 +846,7 @@ var StickyScrollTrigger = (function() {
|
|
|
764
846
|
const index = this.#pinLayers.indexOf(layer);
|
|
765
847
|
if (index !== -1) this.#pinLayers.splice(index, 1);
|
|
766
848
|
if (layer.outer) {
|
|
767
|
-
unwrapPin(layer.outer, layer.trigger);
|
|
849
|
+
unwrapPin(layer.outer, layer.trigger, layer.savedPosition);
|
|
768
850
|
layer.outer = null;
|
|
769
851
|
layer.inner = null;
|
|
770
852
|
}
|
|
@@ -798,6 +880,7 @@ var StickyScrollTrigger = (function() {
|
|
|
798
880
|
return documentTop(element);
|
|
799
881
|
}
|
|
800
882
|
createResolvedTrigger({ trigger: triggerInput, start, end, endTrigger: endTriggerInput, ...rest }) {
|
|
883
|
+
this.#assertNoExcludedVars(rest, "createResolvedTrigger");
|
|
801
884
|
const trigger = resolveElement(triggerInput, "createResolvedTrigger");
|
|
802
885
|
const endTrigger = resolveEndTrigger(trigger, endTriggerInput, "createResolvedTrigger");
|
|
803
886
|
return {
|
|
@@ -818,7 +901,7 @@ var StickyScrollTrigger = (function() {
|
|
|
818
901
|
this.#layers.length = 0;
|
|
819
902
|
this.#builtLayers = [];
|
|
820
903
|
this.#pinLayers.forEach((layer) => {
|
|
821
|
-
if (layer.outer) unwrapPin(layer.outer, layer.trigger);
|
|
904
|
+
if (layer.outer) unwrapPin(layer.outer, layer.trigger, layer.savedPosition);
|
|
822
905
|
});
|
|
823
906
|
this.#pinLayers.length = 0;
|
|
824
907
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @license
|
|
3
|
-
* StickyScrollTrigger v0.
|
|
3
|
+
* StickyScrollTrigger v0.6.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=>{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)}}})();
|
|
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=>({position:e.style.position,top:e.style.top}),d=(e,t)=>{e.style.position=t.position,e.style.top=t.top},f=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}},p=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},m=e=>{let t=e.parentNode;if(t){for(;e.firstChild;)t.insertBefore(e.firstChild,e);e.remove()}},h=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}},g=(e,t,n)=>{let r=e.parentNode;d(t,n),r&&r.insertBefore(t,e),e.remove()},_=new WeakMap,v=e=>{let t=[];for(let n=e;n;n=n.nextElementSibling){if(!(n instanceof HTMLElement))continue;let e=_.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`),_.set(n,t)}t.push(n)}return()=>{t.forEach(e=>{let t=_.get(e);t&&(--t.count,!(t.count>0)&&(e.style.position=t.position,e.style.zIndex=t.zIndex,_.delete(e)))}),t.length=0}},y=0,b=typeof CSS<`u`&&typeof CSS.supports==`function`&&CSS.supports(`animation-timeline`,`scroll()`),x=(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(``)},S=e=>{let t=`sst${y++}`,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`)})};b||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}`,``)),b||(o=h,c=p,l()),i||(i=document.createElement(`style`),document.head.appendChild(i)),i.textContent=x(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 v=new Set(g);r.forEach(e=>{v.has(e)||u(e)}),r=v},restore(){b||window.removeEventListener(`scroll`,l),r.forEach(u),r=new Set,d()}}},C=e=>{for(let t=1;t<e.length;t+=1)if(s(e[t-1].trigger,e[t].trigger)>0)return!0;return!1},w=(e,t)=>{t.sort((e,t)=>s(e.trigger,t.trigger));for(let e of t)e.kind===`cover`&&(e.wrapper=p(e.trigger));let n=e,r=null;for(let e of t){if(e.kind!==`scene`)continue;let t=f(n);e.container=t.container,e.wrapper=t.wrapper,e.padding=t.padding,n=t.container,r=t.container}return r},T=(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&&m(n.wrapper)}t.forEach(e=>{e.wrapper=null,e.kind===`scene`&&(e.container=null,e.padding=null)})},E=e=>typeof e==`function`?e():e,D={top:0,center:.5,bottom:1},O=(e,t,n,r)=>{let i=n===`%`?parseFloat(t)/100*r:parseFloat(t);return e?.startsWith(`-`)?-i:i},k=/^(top|center|bottom|(?:\d+(?:\.\d*)?|\.\d+))?(%|px)?(?:([+-]=?)(\d+(?:\.\d*)?|\.\d+)(%|px)?)?$/,A=(e,t)=>{let n=k.exec(e);if(!n||!n[1]&&!n[3])throw L.test(e)?Error(`StickyScrollTrigger: unsupported position clause "${e}": 'max' is GSAP's end-only keyword for the scroller's maximum scroll position.`):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 needs it to tell "${r}" from the offset). Did you mean "${t}"?`)}let c=r!==void 0&&r in D,l=i===`%`;return{fraction:c?D[r]:l?parseFloat(r)/100:0,offsetPx:(!c&&!l&&r!==void 0?parseFloat(r):0)+(a?O(a,o,s,t):0)}},j=e=>typeof e==`number`||e.trim()!==``&&!Number.isNaN(Number(e)),M=e=>typeof e==`number`?e:Number(e),N=(e,t,n)=>{let r=e.trim();if(r.startsWith(`clamp(`)){let e=r.slice(6).replace(/\)$/,``).trim();throw Error(`StickyScrollTrigger: unsupported position clause "${r}": GSAP's clamp() wrapper isn't supported here.`+(e?` Did you mean "${e}"?`:``))}let[i,a=`top`]=r.split(/\s+/),o=A(i,t),s=A(a,n);return n*s.fraction+s.offsetPx-(t*o.fraction+o.offsetPx)},P=/^\+=(\d+(?:\.\d*)?|\.\d+)(%|px)?$/,F=e=>typeof e==`string`&&P.test(e.trim()),I=(e,t)=>{let n=P.exec(String(e).trim());if(!n)throw Error(`StickyScrollTrigger: unsupported end format "${e}"`);let[,r,i]=n;return O(void 0,r,i,t)},L=/^max(?:([+-]=)(\d+(?:\.\d*)?|\.\d+)(%|px)?)?$/,R=e=>typeof e==`string`&&L.test(e.trim()),z=(e,t)=>{let n=L.exec(e.trim());if(!n)throw Error(`StickyScrollTrigger: unsupported max-position format "${e}"`);let[,r,i,a]=n;return r?O(r,i,a,t):0},B=(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},V=(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+B(e,s,a,l.end.rawTop,u):t}let r=N(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}},H=(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}=V(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=V(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 endTrigger references form a circular structural dependency, each layer's end depending 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},U=[`trigger`,`start`,`end`,`endTrigger`,`pin`,`pinSpacing`,`anticipatePin`,`pinnedContainer`,`pinReparent`,`pinSpacer`,`pinType`,`horizontal`,`scroller`,`containerAnimation`],W=(e,t,n,a,o,s)=>{if(a===null)return{mode:`auto`};if(F(a))return{mode:`dwell`,distancePx:Math.max(0,I(a,t))};if(j(a))return{mode:`absolute`,value:M(a)};if(R(a)){if(n.kind!==`cover`)throw Error(`StickyScrollTrigger: createStickyTrigger's trigger ${r(n.trigger)} has end "${a}". GSAP's 'max' keyword isn't supported here: the layer's own dwell padding adds to the document's max scroll position, so the freeze window would depend on itself. Use createOverlapScroll, which adds no padding, or a dwell distance such as '+=500'.`);return{mode:`max`,offsetPx:z(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, which isn't supported: this layer's own dwell padding pushes that one down, so the freeze window would depend on its own dwell. Point endTrigger at a layer earlier in DOM order, or use a dwell distance such as '+=500'.`);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 keeps pushing it away, so the freeze window never settles. 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}},G=(e,t,n,i)=>{if(!j(t))return{mode:`clause`,anchorOffset:N(t,n,i)};if(e.kind===`cover`)throw Error(`StickyScrollTrigger: createOverlapScroll's trigger ${r(e.trigger)} has start "${t}", an absolute scroll position (GSAP reads any bare number this way). That isn't supported here: a cover layer's sticky position is computed relative to its own wrapper, so start must be a position clause such as 'bottom bottom'.`);return{mode:`absolute`,value:M(t)}},K=(e,t,n,r,a)=>{let o=E(n.start),s=n.end===null?null:E(n.end),c=n.trigger.offsetHeight,l=n.endTrigger===n.trigger,u=a.get(n.endTrigger)??null,d=W(e,t,n,s,r,u),f=G(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}},q=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)},J=(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={}){if(this.#e=e(t),this.#d=S(this.#e),this.#f=n.scrollMarginTargets===void 0?`[id]`:n.scrollMarginTargets,this.#f!==null)try{this.#e.querySelectorAll(this.#f)}catch{throw Error(`StickyScrollTrigger: scrollMarginTargets "${this.#f}" is not a valid CSS selector.`)}}#p(){this.#i&&=(T(this.#e,this.#n,this.#o),this.#o=null,!1)}#m(){this.#o=w(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);let i=n?.(r);return e[0]===t&&this.refresh(),i}}#v(){this.#u.forEach(e=>{if(e.outer)return;e.savedPosition=u(e.trigger);let t=h(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(){let e=this.#u.map(e=>({layer:e,position:u(e.trigger),height:e.inner?e.inner.style.height:``}));return e.forEach(({layer:e})=>{c(e.trigger),e.inner&&(e.inner.style.height=``)}),()=>{e.forEach(({layer:e,position:t,height:n})=>{d(e.trigger,t),e.inner&&(e.inner.style.height=n)})}}#x(e){if(!this.#u.length)return;let t=this.#y(),n=this.#b();try{this.#S(e)}catch(e){throw n(),e}finally{t()}}#S(e){this.#u.forEach(t=>{if(!t.inner)return;let n=E(t.end);if(R(n))throw Error(`StickyScrollTrigger: createStickyPin's end "${n}" uses GSAP's 'max' keyword, which isn't supported here: the pin's own spacer height adds to the document's max scroll position, so it would depend on itself. Use a position clause against endTrigger, such as 'top top'.`);let r=E(t.start),a=i(t.trigger),o=t.trigger.offsetHeight;if(j(r))throw Error(`StickyScrollTrigger: createStickyPin's start "${r}" is an absolute scroll position (GSAP reads any bare number 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=N(r,o,e),c=(j(n)?M(n):i(t.endTrigger)-N(n,t.endTrigger.offsetHeight,e))-a+s+o;l(t.trigger,s),t.inner.style.height=`${Math.max(0,c)}px`})}#C(e){if(!this.#t.length)return;let t=this.#t.filter(e=>e.wrapper!==null);if(!t.length)return;let n=this.#y();try{this.#w(e,t)}catch(e){throw n(),e}}#w(e,t){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)=>K(this.#e,e,t,n,r)),s=0,c=()=>H(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`))}});c();let u=a.some(e=>e.end.mode===`clause`&&e.end.measureLive),d=a.some(e=>e.end.mode===`max`);d&&(s=o(e)),(u||d)&&c()}refresh(){if(this.#s)return;(this.#a||!this.#i||C(this.#t))&&(this.#p(),this.#m()),this.#v();let e=this.#t.length||this.#u.length?a():0;this.#C(e),this.#x(e),this.#T()}#T(){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)}#E(e,t){let n=U.filter(t=>t in e);if(n.length!==0)throw Error(`${t}: ${n.join(`, `)} ${n.length>1?`are`:`is`} not supported here; this module handles pinning and the scroll axis itself.`)}#D(e,t,n){if(J(e,t))throw Error(n)}#O(e,t){if(e===this.#e)throw Error(`${t}: trigger ${r(e)} is the shared container itself. Point trigger at an element inside the container.`);if(!this.#e.contains(e))throw Error(`${t}: trigger ${r(e)} is outside the shared container. Point trigger at an element inside it, or use createStickyPin, which works on either side.`)}#k(e,t){if(!e.contains(this.#e))return;let n=e===this.#e?`is the shared container itself`:`is an ancestor of the shared container`;throw Error(`${t}: trigger ${r(e)} ${n}. A pin wraps trigger in a height:0, contain:layout box, which would pull the container and every layer's dwell padding out of the flow. Pin an element that doesn't contain the container.`)}#A(e,t,n){if(this.#s)throw Error(`StickyScrollTrigger: cannot register a new layer after destroy() has been called.`);return this.#D(this.#t,e.trigger,`StickyScrollTrigger: ${r(e.trigger)} is already registered as a trigger for another layer. Each element can be used once per controller instance, across createStickyTrigger and createOverlapScroll: reusing one makes endTrigger resolution ambiguous.`),this.#D(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}){this.#E(s,`createStickyTrigger`);let c=t(e,`createStickyTrigger`);this.#O(c,`createStickyTrigger`);let l=n(c,a,`createStickyTrigger`);return this.#A({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}){this.#E(l,`createOverlapScroll`);let u=t(e,`createOverlapScroll`);this.#O(u,`createOverlapScroll`);let 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.#A(p,e=>{h(),c?.(e)},l),h=v(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.#E(c,`createStickyPin`),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`),d=t(a,`createStickyPin`);this.#k(l,`createStickyPin`),this.#D(this.#u,l,`StickyScrollTrigger: ${r(l)} is already registered as a pin trigger.`),this.#D(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 f={trigger:l,savedPosition:u(l),outer:null,inner:null,start:i===void 0?n??`top top`:q(i),endTrigger:d,end:o};return this.#u.push(f),{...c,trigger:l,onKill:e=>{this.#g(e);let t=this.#u.indexOf(f);t!==-1&&this.#u.splice(t,1),f.outer&&(g(f.outer,f.trigger,f.savedPosition),f.outer=null,f.inner=null),s?.(e)},onRefreshInit:this.#_(this.#u,f,c.onRefreshInit)}}resolveScrollPosition(e,n){let r=t(e,`resolveScrollPosition`),c=E(n),l=a();if(R(c))return o(l)+z(c,l);if(j(c))return M(c);let u=0;this.#t.forEach(e=>{e.kind===`scene`&&(s(e.trigger,r)>=0||(u+=e.freezeEnd-e.freezeStart))});let d=N(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}){this.#E(o,`createResolvedTrigger`);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&&g(e.outer,e.trigger,e.savedPosition)}),this.#u.length=0)}}})();
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @license
|
|
3
|
-
* StickyScrollTrigger v0.
|
|
3
|
+
* StickyScrollTrigger v0.6.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
|
|
@@ -65,6 +65,14 @@ const applyStickyPosition = (el, topPx) => {
|
|
|
65
65
|
el.style.position = "sticky";
|
|
66
66
|
el.style.top = `${topPx}px`;
|
|
67
67
|
};
|
|
68
|
+
const captureInlinePosition = (el) => ({
|
|
69
|
+
position: el.style.position,
|
|
70
|
+
top: el.style.top
|
|
71
|
+
});
|
|
72
|
+
const restoreInlinePosition = (el, saved) => {
|
|
73
|
+
el.style.position = saved.position;
|
|
74
|
+
el.style.top = saved.top;
|
|
75
|
+
};
|
|
68
76
|
const wrapScene = (inner) => {
|
|
69
77
|
if (!inner.parentNode) throw new Error(`StickyScrollTrigger: ${describeElement(inner)} is not attached to the document`);
|
|
70
78
|
const container = document.createElement("div");
|
|
@@ -114,9 +122,9 @@ const wrapPin = (trigger) => {
|
|
|
114
122
|
inner
|
|
115
123
|
};
|
|
116
124
|
};
|
|
117
|
-
const unwrapPin = (outer, trigger) => {
|
|
125
|
+
const unwrapPin = (outer, trigger, saved) => {
|
|
118
126
|
const parent = outer.parentNode;
|
|
119
|
-
|
|
127
|
+
restoreInlinePosition(trigger, saved);
|
|
120
128
|
if (parent) parent.insertBefore(trigger, outer);
|
|
121
129
|
outer.remove();
|
|
122
130
|
};
|
|
@@ -305,14 +313,17 @@ const signedPx = (sign, value, unit, refSize) => {
|
|
|
305
313
|
const magnitude = unit === "%" ? parseFloat(value) / 100 * refSize : parseFloat(value);
|
|
306
314
|
return sign?.startsWith("-") ? -magnitude : magnitude;
|
|
307
315
|
};
|
|
308
|
-
const CLAUSE_TOKEN_RE = /^(top|center|bottom|(?:\d
|
|
316
|
+
const CLAUSE_TOKEN_RE = /^(top|center|bottom|(?:\d+(?:\.\d*)?|\.\d+))?(%|px)?(?:([+-]=?)(\d+(?:\.\d*)?|\.\d+)(%|px)?)?$/;
|
|
309
317
|
const parseClauseToken = (token, refSize) => {
|
|
310
318
|
const match = CLAUSE_TOKEN_RE.exec(token);
|
|
311
|
-
if (!match || !match[1] && !match[3])
|
|
319
|
+
if (!match || !match[1] && !match[3]) {
|
|
320
|
+
if (MAX_TOKEN_RE.test(token)) throw new Error(`StickyScrollTrigger: unsupported position clause "${token}": 'max' is GSAP's end-only keyword for the scroller's maximum scroll position.`);
|
|
321
|
+
throw new Error(`StickyScrollTrigger: unsupported position clause "${token}"`);
|
|
322
|
+
}
|
|
312
323
|
const [, base, baseUnit, sign, offsetValue, offsetUnit] = match;
|
|
313
324
|
if (base !== void 0 && sign && !sign.includes("=")) {
|
|
314
325
|
const suggestion = `${base}${baseUnit ?? ""}${sign}=${offsetValue}${offsetUnit ?? ""}`;
|
|
315
|
-
throw new Error(`StickyScrollTrigger: unsupported position clause "${token}": a signed offset needs an explicit '=' when it follows a keyword or number (GSAP
|
|
326
|
+
throw new Error(`StickyScrollTrigger: unsupported position clause "${token}": a signed offset needs an explicit '=' when it follows a keyword or number (GSAP needs it to tell "${base}" from the offset). Did you mean "${suggestion}"?`);
|
|
316
327
|
}
|
|
317
328
|
const isKeyword = base !== void 0 && base in KEYWORD_FRACTIONS;
|
|
318
329
|
const basePercent = baseUnit === "%";
|
|
@@ -326,13 +337,19 @@ const isAbsoluteFormat = (resolved) => {
|
|
|
326
337
|
return resolved.trim() !== "" && !Number.isNaN(Number(resolved));
|
|
327
338
|
};
|
|
328
339
|
const resolveAbsolute = (resolved) => typeof resolved === "number" ? resolved : Number(resolved);
|
|
340
|
+
const CLAMP_PREFIX = "clamp(";
|
|
329
341
|
const resolveAnchorTop = (position, elementHeight, viewportHeight) => {
|
|
330
|
-
const
|
|
342
|
+
const trimmed = position.trim();
|
|
343
|
+
if (trimmed.startsWith(CLAMP_PREFIX)) {
|
|
344
|
+
const inner = trimmed.slice(6).replace(/\)$/, "").trim();
|
|
345
|
+
throw new Error(`StickyScrollTrigger: unsupported position clause "${trimmed}": GSAP's clamp() wrapper isn't supported here.` + (inner ? ` Did you mean "${inner}"?` : ""));
|
|
346
|
+
}
|
|
347
|
+
const [elementToken, viewportToken = "top"] = trimmed.split(/\s+/);
|
|
331
348
|
const elementClause = parseClauseToken(elementToken, elementHeight);
|
|
332
349
|
const viewportClause = parseClauseToken(viewportToken, viewportHeight);
|
|
333
350
|
return viewportHeight * viewportClause.fraction + viewportClause.offsetPx - (elementHeight * elementClause.fraction + elementClause.offsetPx);
|
|
334
351
|
};
|
|
335
|
-
const DWELL_RELATIVE_RE = /^\+=(\d
|
|
352
|
+
const DWELL_RELATIVE_RE = /^\+=(\d+(?:\.\d*)?|\.\d+)(%|px)?$/;
|
|
336
353
|
const isDwellFormat = (resolved) => typeof resolved === "string" && DWELL_RELATIVE_RE.test(resolved.trim());
|
|
337
354
|
const resolveDwell = (resolved, viewportHeight) => {
|
|
338
355
|
const match = DWELL_RELATIVE_RE.exec(String(resolved).trim());
|
|
@@ -340,7 +357,7 @@ const resolveDwell = (resolved, viewportHeight) => {
|
|
|
340
357
|
const [, value, unit] = match;
|
|
341
358
|
return signedPx(void 0, value, unit, viewportHeight);
|
|
342
359
|
};
|
|
343
|
-
const MAX_TOKEN_RE = /^max(?:([+-]=)(\d
|
|
360
|
+
const MAX_TOKEN_RE = /^max(?:([+-]=)(\d+(?:\.\d*)?|\.\d+)(%|px)?)?$/;
|
|
344
361
|
const isMaxFormat = (resolved) => typeof resolved === "string" && MAX_TOKEN_RE.test(resolved.trim());
|
|
345
362
|
const resolveMaxOffset = (resolved, viewportHeight) => {
|
|
346
363
|
const match = MAX_TOKEN_RE.exec(resolved.trim());
|
|
@@ -444,11 +461,27 @@ const planLayers = (measurements, deps) => {
|
|
|
444
461
|
plans = next.plans;
|
|
445
462
|
naturalTops = next.naturalTops;
|
|
446
463
|
if (stable) break;
|
|
447
|
-
if (pass === measurements.length - 1) throw new Error("StickyScrollTrigger: could not resolve endTrigger positions: some
|
|
464
|
+
if (pass === measurements.length - 1) throw new Error("StickyScrollTrigger: could not resolve endTrigger positions: some endTrigger references form a circular structural dependency, each layer's end depending on a layer whose own end depends back on it. Point each endTrigger at a layer that doesn't depend on it.");
|
|
448
465
|
}
|
|
449
466
|
plans.forEach((plan, index) => deps.onPlanned(index, plan));
|
|
450
467
|
return plans;
|
|
451
468
|
};
|
|
469
|
+
const EXCLUDED_VAR_KEYS = [
|
|
470
|
+
"trigger",
|
|
471
|
+
"start",
|
|
472
|
+
"end",
|
|
473
|
+
"endTrigger",
|
|
474
|
+
"pin",
|
|
475
|
+
"pinSpacing",
|
|
476
|
+
"anticipatePin",
|
|
477
|
+
"pinnedContainer",
|
|
478
|
+
"pinReparent",
|
|
479
|
+
"pinSpacer",
|
|
480
|
+
"pinType",
|
|
481
|
+
"horizontal",
|
|
482
|
+
"scroller",
|
|
483
|
+
"containerAnimation"
|
|
484
|
+
];
|
|
452
485
|
const resolveEndSpec = (rootElement, viewportHeight, layer, endResolved, ownIndex, endTriggerIndex) => {
|
|
453
486
|
if (endResolved === null) return { mode: "auto" };
|
|
454
487
|
if (isDwellFormat(endResolved)) return {
|
|
@@ -460,16 +493,16 @@ const resolveEndSpec = (rootElement, viewportHeight, layer, endResolved, ownInde
|
|
|
460
493
|
value: resolveAbsolute(endResolved)
|
|
461
494
|
};
|
|
462
495
|
if (isMaxFormat(endResolved)) {
|
|
463
|
-
if (layer.kind !== "cover") throw new Error(`StickyScrollTrigger: createStickyTrigger's trigger ${describeElement(layer.trigger)} has end "${endResolved}"
|
|
496
|
+
if (layer.kind !== "cover") throw new Error(`StickyScrollTrigger: createStickyTrigger's trigger ${describeElement(layer.trigger)} has end "${endResolved}". GSAP's 'max' keyword isn't supported here: the layer's own dwell padding adds to the document's max scroll position, so the freeze window would depend on itself. Use createOverlapScroll, which adds no padding, or a dwell distance such as '+=500'.`);
|
|
464
497
|
return {
|
|
465
498
|
mode: "max",
|
|
466
499
|
offsetPx: resolveMaxOffset(endResolved, viewportHeight)
|
|
467
500
|
};
|
|
468
501
|
}
|
|
469
|
-
if (layer.kind !== "cover" && endTriggerIndex !== null && endTriggerIndex > ownIndex) throw new Error(`StickyScrollTrigger: createStickyTrigger's trigger ${describeElement(layer.trigger)}'s endTrigger (${describeElement(layer.endTrigger)}) refers to a layer positioned later in DOM order
|
|
502
|
+
if (layer.kind !== "cover" && endTriggerIndex !== null && endTriggerIndex > ownIndex) throw new Error(`StickyScrollTrigger: createStickyTrigger's trigger ${describeElement(layer.trigger)}'s endTrigger (${describeElement(layer.endTrigger)}) refers to a layer positioned later in DOM order, which isn't supported: this layer's own dwell padding pushes that one down, so the freeze window would depend on its own dwell. Point endTrigger at a layer earlier in DOM order, or use a dwell distance such as '+=500'.`);
|
|
470
503
|
const unregistered = endTriggerIndex === null;
|
|
471
504
|
const insideRoot = rootElement.contains(layer.endTrigger);
|
|
472
|
-
if (unregistered && !insideRoot && layer.kind !== "cover") throw new Error(`StickyScrollTrigger: createStickyTrigger's trigger ${describeElement(layer.trigger)} has an endTrigger (${describeElement(layer.endTrigger)}) outside the shared container
|
|
505
|
+
if (unregistered && !insideRoot && layer.kind !== "cover") throw new Error(`StickyScrollTrigger: createStickyTrigger's trigger ${describeElement(layer.trigger)} has an endTrigger (${describeElement(layer.endTrigger)}) outside the shared container: the layer's own dwell keeps pushing it away, so the freeze window never settles. Point endTrigger at an element inside the container, or use a dwell distance such as '+=500'.`);
|
|
473
506
|
return {
|
|
474
507
|
mode: "clause",
|
|
475
508
|
clause: endResolved,
|
|
@@ -482,7 +515,7 @@ const resolveStartSpec = (layer, startResolved, elementHeight, viewportHeight) =
|
|
|
482
515
|
mode: "clause",
|
|
483
516
|
anchorOffset: resolveAnchorTop(startResolved, elementHeight, viewportHeight)
|
|
484
517
|
};
|
|
485
|
-
if (layer.kind === "cover") throw new Error(`StickyScrollTrigger: createOverlapScroll's trigger ${describeElement(layer.trigger)} has start "${startResolved}", an absolute scroll position (GSAP
|
|
518
|
+
if (layer.kind === "cover") throw new Error(`StickyScrollTrigger: createOverlapScroll's trigger ${describeElement(layer.trigger)} has start "${startResolved}", an absolute scroll position (GSAP reads any bare number this way). That isn't supported here: a cover layer's sticky position is computed relative to its own wrapper, so start must be a position clause such as 'bottom bottom'.`);
|
|
486
519
|
return {
|
|
487
520
|
mode: "absolute",
|
|
488
521
|
value: resolveAbsolute(startResolved)
|
|
@@ -534,6 +567,11 @@ var StickyScrollTrigger = class {
|
|
|
534
567
|
this.#rootElement = resolveRoot(root);
|
|
535
568
|
this.#scrollMarginSync = createScrollMarginSync(this.#rootElement);
|
|
536
569
|
this.#scrollMarginTargets = options.scrollMarginTargets === void 0 ? "[id]" : options.scrollMarginTargets;
|
|
570
|
+
if (this.#scrollMarginTargets !== null) try {
|
|
571
|
+
this.#rootElement.querySelectorAll(this.#scrollMarginTargets);
|
|
572
|
+
} catch {
|
|
573
|
+
throw new Error(`StickyScrollTrigger: scrollMarginTargets "${this.#scrollMarginTargets}" is not a valid CSS selector.`);
|
|
574
|
+
}
|
|
537
575
|
}
|
|
538
576
|
#unbuild() {
|
|
539
577
|
if (!this.#built) return;
|
|
@@ -563,13 +601,15 @@ var StickyScrollTrigger = class {
|
|
|
563
601
|
#createAutoRefreshHandler(list, entry, userOnRefreshInit) {
|
|
564
602
|
return (self) => {
|
|
565
603
|
this.#captureScrollTriggerClass(self);
|
|
604
|
+
const result = userOnRefreshInit?.(self);
|
|
566
605
|
if (list[0] === entry) this.refresh();
|
|
567
|
-
|
|
606
|
+
return result;
|
|
568
607
|
};
|
|
569
608
|
}
|
|
570
609
|
#wrapUnwrappedPins() {
|
|
571
610
|
this.#pinLayers.forEach((layer) => {
|
|
572
611
|
if (layer.outer) return;
|
|
612
|
+
layer.savedPosition = captureInlinePosition(layer.trigger);
|
|
573
613
|
const wrapped = wrapPin(layer.trigger);
|
|
574
614
|
layer.outer = wrapped.outer;
|
|
575
615
|
layer.inner = wrapped.inner;
|
|
@@ -589,28 +629,45 @@ var StickyScrollTrigger = class {
|
|
|
589
629
|
});
|
|
590
630
|
};
|
|
591
631
|
}
|
|
632
|
+
#resetPinState() {
|
|
633
|
+
const snapshots = this.#pinLayers.map((layer) => ({
|
|
634
|
+
layer,
|
|
635
|
+
position: captureInlinePosition(layer.trigger),
|
|
636
|
+
height: layer.inner ? layer.inner.style.height : ""
|
|
637
|
+
}));
|
|
638
|
+
snapshots.forEach(({ layer }) => {
|
|
639
|
+
resetStickyPosition(layer.trigger);
|
|
640
|
+
if (layer.inner) layer.inner.style.height = "";
|
|
641
|
+
});
|
|
642
|
+
return () => {
|
|
643
|
+
snapshots.forEach(({ layer, position, height }) => {
|
|
644
|
+
restoreInlinePosition(layer.trigger, position);
|
|
645
|
+
if (layer.inner) layer.inner.style.height = height;
|
|
646
|
+
});
|
|
647
|
+
};
|
|
648
|
+
}
|
|
592
649
|
#refreshPins(viewportHeight) {
|
|
593
650
|
if (!this.#pinLayers.length) return;
|
|
594
651
|
const restoreSceneCoverStickyState = this.#resetSceneCoverStickyState();
|
|
652
|
+
const restorePinState = this.#resetPinState();
|
|
595
653
|
try {
|
|
596
654
|
this.#refreshPinLayers(viewportHeight);
|
|
655
|
+
} catch (error) {
|
|
656
|
+
restorePinState();
|
|
657
|
+
throw error;
|
|
597
658
|
} finally {
|
|
598
659
|
restoreSceneCoverStickyState();
|
|
599
660
|
}
|
|
600
661
|
}
|
|
601
662
|
#refreshPinLayers(viewportHeight) {
|
|
602
|
-
this.#pinLayers.forEach((layer) => {
|
|
603
|
-
resetStickyPosition(layer.trigger);
|
|
604
|
-
if (layer.inner) layer.inner.style.height = "";
|
|
605
|
-
});
|
|
606
663
|
this.#pinLayers.forEach((layer) => {
|
|
607
664
|
if (!layer.inner) return;
|
|
608
665
|
const resolvedEnd = resolveMaybeFn(layer.end);
|
|
609
|
-
if (isMaxFormat(resolvedEnd)) throw new Error(`StickyScrollTrigger: createStickyPin's end "${resolvedEnd}" uses GSAP's 'max' keyword, which isn't supported here
|
|
666
|
+
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 adds to the document's max scroll position, so it would depend on itself. Use a position clause against endTrigger, such as 'top top'.`);
|
|
610
667
|
const resolvedStart = resolveMaybeFn(layer.start);
|
|
611
668
|
const triggerTop = documentTop(layer.trigger);
|
|
612
669
|
const triggerHeight = layer.trigger.offsetHeight;
|
|
613
|
-
if (isAbsoluteFormat(resolvedStart)) throw new Error(`StickyScrollTrigger: createStickyPin's start "${resolvedStart}" is an absolute scroll position (GSAP
|
|
670
|
+
if (isAbsoluteFormat(resolvedStart)) throw new Error(`StickyScrollTrigger: createStickyPin's start "${resolvedStart}" is an absolute scroll position (GSAP reads any bare number 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
671
|
const topPx = resolveAnchorTop(resolvedStart, triggerHeight, viewportHeight);
|
|
615
672
|
const height = (isAbsoluteFormat(resolvedEnd) ? resolveAbsolute(resolvedEnd) : documentTop(layer.endTrigger) - resolveAnchorTop(resolvedEnd, layer.endTrigger.offsetHeight, viewportHeight)) - triggerTop + topPx + triggerHeight;
|
|
616
673
|
applyStickyPosition(layer.trigger, topPx);
|
|
@@ -619,13 +676,17 @@ var StickyScrollTrigger = class {
|
|
|
619
676
|
}
|
|
620
677
|
#refreshScenesAndCovers(viewportHeight) {
|
|
621
678
|
if (!this.#layers.length) return;
|
|
622
|
-
const active =
|
|
623
|
-
this.#layers.forEach((layer) => {
|
|
624
|
-
if (!layer.wrapper) return;
|
|
625
|
-
resetStickyPosition(layer.wrapper);
|
|
626
|
-
active.push(layer);
|
|
627
|
-
});
|
|
679
|
+
const active = this.#layers.filter((layer) => layer.wrapper !== null);
|
|
628
680
|
if (!active.length) return;
|
|
681
|
+
const restoreStickyState = this.#resetSceneCoverStickyState();
|
|
682
|
+
try {
|
|
683
|
+
this.#planLayerPositions(viewportHeight, active);
|
|
684
|
+
} catch (error) {
|
|
685
|
+
restoreStickyState();
|
|
686
|
+
throw error;
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
#planLayerPositions(viewportHeight, active) {
|
|
629
690
|
const structureTop = this.#outermostContainer ? documentTop(this.#outermostContainer) : 0;
|
|
630
691
|
const indexByTrigger = new Map();
|
|
631
692
|
active.forEach((layer, index) => {
|
|
@@ -672,12 +733,26 @@ var StickyScrollTrigger = class {
|
|
|
672
733
|
freezeEnd
|
|
673
734
|
})), this.#outermostContainer, this.#scrollMarginTargets);
|
|
674
735
|
}
|
|
736
|
+
#assertNoExcludedVars(rest, context) {
|
|
737
|
+
const found = EXCLUDED_VAR_KEYS.filter((key) => key in rest);
|
|
738
|
+
if (found.length === 0) return;
|
|
739
|
+
throw new Error(`${context}: ${found.join(", ")} ${found.length > 1 ? "are" : "is"} not supported here; this module handles pinning and the scroll axis itself.`);
|
|
740
|
+
}
|
|
675
741
|
#assertTriggerAvailable(list, trigger, message) {
|
|
676
742
|
if (hasDuplicateTrigger(list, trigger)) throw new Error(message);
|
|
677
743
|
}
|
|
744
|
+
#assertTriggerInsideRoot(trigger, context) {
|
|
745
|
+
if (trigger === this.#rootElement) throw new Error(`${context}: trigger ${describeElement(trigger)} is the shared container itself. Point trigger at an element inside the container.`);
|
|
746
|
+
if (!this.#rootElement.contains(trigger)) throw new Error(`${context}: trigger ${describeElement(trigger)} is outside the shared container. Point trigger at an element inside it, or use createStickyPin, which works on either side.`);
|
|
747
|
+
}
|
|
748
|
+
#assertTriggerNotEnclosingRoot(trigger, context) {
|
|
749
|
+
if (!trigger.contains(this.#rootElement)) return;
|
|
750
|
+
const relation = trigger === this.#rootElement ? "is the shared container itself" : "is an ancestor of the shared container";
|
|
751
|
+
throw new Error(`${context}: trigger ${describeElement(trigger)} ${relation}. A pin wraps trigger in a height:0, contain:layout box, which would pull the container and every layer's dwell padding out of the flow. Pin an element that doesn't contain the container.`);
|
|
752
|
+
}
|
|
678
753
|
#registerLayer(layer, onKill, rest) {
|
|
679
754
|
if (this.#destroyed) throw new Error("StickyScrollTrigger: cannot register a new layer after destroy() has been called.");
|
|
680
|
-
this.#assertTriggerAvailable(this.#layers, layer.trigger, `StickyScrollTrigger: ${describeElement(layer.trigger)} is already registered as a trigger for another layer. Each element can
|
|
755
|
+
this.#assertTriggerAvailable(this.#layers, layer.trigger, `StickyScrollTrigger: ${describeElement(layer.trigger)} is already registered as a trigger for another layer. Each element can be used once per controller instance, across createStickyTrigger and createOverlapScroll: reusing one makes endTrigger resolution ambiguous.`);
|
|
681
756
|
this.#assertTriggerAvailable(this.#pinLayers, layer.trigger, `StickyScrollTrigger: ${describeElement(layer.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.`);
|
|
682
757
|
this.#layers.push(layer);
|
|
683
758
|
this.#dirty = true;
|
|
@@ -699,7 +774,9 @@ var StickyScrollTrigger = class {
|
|
|
699
774
|
};
|
|
700
775
|
}
|
|
701
776
|
createStickyTrigger({ trigger: triggerInput, start = "0 0", end = "100% 0", endTrigger: endTriggerInput, onKill, ...rest }) {
|
|
777
|
+
this.#assertNoExcludedVars(rest, "createStickyTrigger");
|
|
702
778
|
const trigger = resolveElement(triggerInput, "createStickyTrigger");
|
|
779
|
+
this.#assertTriggerInsideRoot(trigger, "createStickyTrigger");
|
|
703
780
|
const endTrigger = resolveEndTrigger(trigger, endTriggerInput, "createStickyTrigger");
|
|
704
781
|
return this.#registerLayer({
|
|
705
782
|
kind: "scene",
|
|
@@ -715,7 +792,9 @@ var StickyScrollTrigger = class {
|
|
|
715
792
|
}, onKill, rest);
|
|
716
793
|
}
|
|
717
794
|
createOverlapScroll({ trigger: triggerInput, cover, start = "bottom bottom", end = null, endTrigger: endTriggerInput, onKill, ...rest }) {
|
|
795
|
+
this.#assertNoExcludedVars(rest, "createOverlapScroll");
|
|
718
796
|
const trigger = resolveElement(triggerInput, "createOverlapScroll");
|
|
797
|
+
this.#assertTriggerInsideRoot(trigger, "createOverlapScroll");
|
|
719
798
|
const endTrigger = resolveEndTrigger(trigger, endTriggerInput, "createOverlapScroll");
|
|
720
799
|
const coverElement = cover === void 0 ? trigger.nextElementSibling : resolveElement(cover, "createOverlapScroll");
|
|
721
800
|
if (!(coverElement instanceof HTMLElement)) throw new Error(`createOverlapScroll: cover element (trigger.nextElementSibling) not found for trigger ${describeElement(trigger)}`);
|
|
@@ -740,14 +819,17 @@ var StickyScrollTrigger = class {
|
|
|
740
819
|
return vars;
|
|
741
820
|
}
|
|
742
821
|
createStickyPin({ trigger: triggerInput, start, top, endTrigger: endTriggerInput, end = "top top", onKill, ...rest }) {
|
|
822
|
+
this.#assertNoExcludedVars(rest, "createStickyPin");
|
|
743
823
|
if (this.#destroyed) throw new Error("StickyScrollTrigger: cannot register a new pin after destroy() has been called.");
|
|
744
824
|
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.");
|
|
745
825
|
const trigger = resolveElement(triggerInput, "createStickyPin");
|
|
746
826
|
const endTrigger = resolveElement(endTriggerInput, "createStickyPin");
|
|
827
|
+
this.#assertTriggerNotEnclosingRoot(trigger, "createStickyPin");
|
|
747
828
|
this.#assertTriggerAvailable(this.#pinLayers, trigger, `StickyScrollTrigger: ${describeElement(trigger)} is already registered as a pin trigger.`);
|
|
748
829
|
this.#assertTriggerAvailable(this.#layers, trigger, `StickyScrollTrigger: ${describeElement(trigger)} 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.`);
|
|
749
830
|
const layer = {
|
|
750
831
|
trigger,
|
|
832
|
+
savedPosition: captureInlinePosition(trigger),
|
|
751
833
|
outer: null,
|
|
752
834
|
inner: null,
|
|
753
835
|
start: top === void 0 ? start ?? "top top" : topToStartClause(top),
|
|
@@ -763,7 +845,7 @@ var StickyScrollTrigger = class {
|
|
|
763
845
|
const index = this.#pinLayers.indexOf(layer);
|
|
764
846
|
if (index !== -1) this.#pinLayers.splice(index, 1);
|
|
765
847
|
if (layer.outer) {
|
|
766
|
-
unwrapPin(layer.outer, layer.trigger);
|
|
848
|
+
unwrapPin(layer.outer, layer.trigger, layer.savedPosition);
|
|
767
849
|
layer.outer = null;
|
|
768
850
|
layer.inner = null;
|
|
769
851
|
}
|
|
@@ -797,6 +879,7 @@ var StickyScrollTrigger = class {
|
|
|
797
879
|
return documentTop(element);
|
|
798
880
|
}
|
|
799
881
|
createResolvedTrigger({ trigger: triggerInput, start, end, endTrigger: endTriggerInput, ...rest }) {
|
|
882
|
+
this.#assertNoExcludedVars(rest, "createResolvedTrigger");
|
|
800
883
|
const trigger = resolveElement(triggerInput, "createResolvedTrigger");
|
|
801
884
|
const endTrigger = resolveEndTrigger(trigger, endTriggerInput, "createResolvedTrigger");
|
|
802
885
|
return {
|
|
@@ -817,7 +900,7 @@ var StickyScrollTrigger = class {
|
|
|
817
900
|
this.#layers.length = 0;
|
|
818
901
|
this.#builtLayers = [];
|
|
819
902
|
this.#pinLayers.forEach((layer) => {
|
|
820
|
-
if (layer.outer) unwrapPin(layer.outer, layer.trigger);
|
|
903
|
+
if (layer.outer) unwrapPin(layer.outer, layer.trigger, layer.savedPosition);
|
|
821
904
|
});
|
|
822
905
|
this.#pinLayers.length = 0;
|
|
823
906
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @license
|
|
3
|
-
* StickyScrollTrigger v0.
|
|
3
|
+
* StickyScrollTrigger v0.6.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=>{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};
|
|
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=>({position:e.style.position,top:e.style.top}),d=(e,t)=>{e.style.position=t.position,e.style.top=t.top},f=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}},p=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},m=e=>{let t=e.parentNode;if(t){for(;e.firstChild;)t.insertBefore(e.firstChild,e);e.remove()}},h=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}},g=(e,t,n)=>{let r=e.parentNode;d(t,n),r&&r.insertBefore(t,e),e.remove()},_=new WeakMap,v=e=>{let t=[];for(let n=e;n;n=n.nextElementSibling){if(!(n instanceof HTMLElement))continue;let e=_.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`),_.set(n,t)}t.push(n)}return()=>{t.forEach(e=>{let t=_.get(e);t&&(--t.count,!(t.count>0)&&(e.style.position=t.position,e.style.zIndex=t.zIndex,_.delete(e)))}),t.length=0}};let y=0;const b=typeof CSS<`u`&&typeof CSS.supports==`function`&&CSS.supports(`animation-timeline`,`scroll()`),x=(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(``)},S=e=>{let t=`sst${y++}`,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`)})};b||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}`,``)),b||(o=h,c=p,l()),i||(i=document.createElement(`style`),document.head.appendChild(i)),i.textContent=x(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 v=new Set(g);r.forEach(e=>{v.has(e)||u(e)}),r=v},restore(){b||window.removeEventListener(`scroll`,l),r.forEach(u),r=new Set,d()}}},C=e=>{for(let t=1;t<e.length;t+=1)if(s(e[t-1].trigger,e[t].trigger)>0)return!0;return!1},w=(e,t)=>{t.sort((e,t)=>s(e.trigger,t.trigger));for(let e of t)e.kind===`cover`&&(e.wrapper=p(e.trigger));let n=e,r=null;for(let e of t){if(e.kind!==`scene`)continue;let t=f(n);e.container=t.container,e.wrapper=t.wrapper,e.padding=t.padding,n=t.container,r=t.container}return r},T=(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&&m(n.wrapper)}t.forEach(e=>{e.wrapper=null,e.kind===`scene`&&(e.container=null,e.padding=null)})},E=e=>typeof e==`function`?e():e,D={top:0,center:.5,bottom:1},O=(e,t,n,r)=>{let i=n===`%`?parseFloat(t)/100*r:parseFloat(t);return e?.startsWith(`-`)?-i:i},k=/^(top|center|bottom|(?:\d+(?:\.\d*)?|\.\d+))?(%|px)?(?:([+-]=?)(\d+(?:\.\d*)?|\.\d+)(%|px)?)?$/,A=(e,t)=>{let n=k.exec(e);if(!n||!n[1]&&!n[3])throw L.test(e)?Error(`StickyScrollTrigger: unsupported position clause "${e}": 'max' is GSAP's end-only keyword for the scroller's maximum scroll position.`):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 needs it to tell "${r}" from the offset). Did you mean "${t}"?`)}let c=r!==void 0&&r in D,l=i===`%`;return{fraction:c?D[r]:l?parseFloat(r)/100:0,offsetPx:(!c&&!l&&r!==void 0?parseFloat(r):0)+(a?O(a,o,s,t):0)}},j=e=>typeof e==`number`||e.trim()!==``&&!Number.isNaN(Number(e)),M=e=>typeof e==`number`?e:Number(e),N=(e,t,n)=>{let r=e.trim();if(r.startsWith(`clamp(`)){let e=r.slice(6).replace(/\)$/,``).trim();throw Error(`StickyScrollTrigger: unsupported position clause "${r}": GSAP's clamp() wrapper isn't supported here.`+(e?` Did you mean "${e}"?`:``))}let[i,a=`top`]=r.split(/\s+/),o=A(i,t),s=A(a,n);return n*s.fraction+s.offsetPx-(t*o.fraction+o.offsetPx)},P=/^\+=(\d+(?:\.\d*)?|\.\d+)(%|px)?$/,F=e=>typeof e==`string`&&P.test(e.trim()),I=(e,t)=>{let n=P.exec(String(e).trim());if(!n)throw Error(`StickyScrollTrigger: unsupported end format "${e}"`);let[,r,i]=n;return O(void 0,r,i,t)},L=/^max(?:([+-]=)(\d+(?:\.\d*)?|\.\d+)(%|px)?)?$/,R=e=>typeof e==`string`&&L.test(e.trim()),z=(e,t)=>{let n=L.exec(e.trim());if(!n)throw Error(`StickyScrollTrigger: unsupported max-position format "${e}"`);let[,r,i,a]=n;return r?O(r,i,a,t):0},B=(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},V=(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+B(e,s,a,l.end.rawTop,u):t}let r=N(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}},H=(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}=V(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=V(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 endTrigger references form a circular structural dependency, each layer's end depending 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},U=[`trigger`,`start`,`end`,`endTrigger`,`pin`,`pinSpacing`,`anticipatePin`,`pinnedContainer`,`pinReparent`,`pinSpacer`,`pinType`,`horizontal`,`scroller`,`containerAnimation`],W=(e,t,n,a,o,s)=>{if(a===null)return{mode:`auto`};if(F(a))return{mode:`dwell`,distancePx:Math.max(0,I(a,t))};if(j(a))return{mode:`absolute`,value:M(a)};if(R(a)){if(n.kind!==`cover`)throw Error(`StickyScrollTrigger: createStickyTrigger's trigger ${r(n.trigger)} has end "${a}". GSAP's 'max' keyword isn't supported here: the layer's own dwell padding adds to the document's max scroll position, so the freeze window would depend on itself. Use createOverlapScroll, which adds no padding, or a dwell distance such as '+=500'.`);return{mode:`max`,offsetPx:z(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, which isn't supported: this layer's own dwell padding pushes that one down, so the freeze window would depend on its own dwell. Point endTrigger at a layer earlier in DOM order, or use a dwell distance such as '+=500'.`);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 keeps pushing it away, so the freeze window never settles. 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}},G=(e,t,n,i)=>{if(!j(t))return{mode:`clause`,anchorOffset:N(t,n,i)};if(e.kind===`cover`)throw Error(`StickyScrollTrigger: createOverlapScroll's trigger ${r(e.trigger)} has start "${t}", an absolute scroll position (GSAP reads any bare number this way). That isn't supported here: a cover layer's sticky position is computed relative to its own wrapper, so start must be a position clause such as 'bottom bottom'.`);return{mode:`absolute`,value:M(t)}},K=(e,t,n,r,a)=>{let o=E(n.start),s=n.end===null?null:E(n.end),c=n.trigger.offsetHeight,l=n.endTrigger===n.trigger,u=a.get(n.endTrigger)??null,d=W(e,t,n,s,r,u),f=G(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}},q=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)},J=(e,t)=>e.some(e=>e.trigger===t);var Y=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={}){if(this.#e=e(t),this.#d=S(this.#e),this.#f=n.scrollMarginTargets===void 0?`[id]`:n.scrollMarginTargets,this.#f!==null)try{this.#e.querySelectorAll(this.#f)}catch{throw Error(`StickyScrollTrigger: scrollMarginTargets "${this.#f}" is not a valid CSS selector.`)}}#p(){this.#i&&=(T(this.#e,this.#n,this.#o),this.#o=null,!1)}#m(){this.#o=w(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);let i=n?.(r);return e[0]===t&&this.refresh(),i}}#v(){this.#u.forEach(e=>{if(e.outer)return;e.savedPosition=u(e.trigger);let t=h(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(){let e=this.#u.map(e=>({layer:e,position:u(e.trigger),height:e.inner?e.inner.style.height:``}));return e.forEach(({layer:e})=>{c(e.trigger),e.inner&&(e.inner.style.height=``)}),()=>{e.forEach(({layer:e,position:t,height:n})=>{d(e.trigger,t),e.inner&&(e.inner.style.height=n)})}}#x(e){if(!this.#u.length)return;let t=this.#y(),n=this.#b();try{this.#S(e)}catch(e){throw n(),e}finally{t()}}#S(e){this.#u.forEach(t=>{if(!t.inner)return;let n=E(t.end);if(R(n))throw Error(`StickyScrollTrigger: createStickyPin's end "${n}" uses GSAP's 'max' keyword, which isn't supported here: the pin's own spacer height adds to the document's max scroll position, so it would depend on itself. Use a position clause against endTrigger, such as 'top top'.`);let r=E(t.start),a=i(t.trigger),o=t.trigger.offsetHeight;if(j(r))throw Error(`StickyScrollTrigger: createStickyPin's start "${r}" is an absolute scroll position (GSAP reads any bare number 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=N(r,o,e),c=(j(n)?M(n):i(t.endTrigger)-N(n,t.endTrigger.offsetHeight,e))-a+s+o;l(t.trigger,s),t.inner.style.height=`${Math.max(0,c)}px`})}#C(e){if(!this.#t.length)return;let t=this.#t.filter(e=>e.wrapper!==null);if(!t.length)return;let n=this.#y();try{this.#w(e,t)}catch(e){throw n(),e}}#w(e,t){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)=>K(this.#e,e,t,n,r)),s=0,c=()=>H(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`))}});c();let u=a.some(e=>e.end.mode===`clause`&&e.end.measureLive),d=a.some(e=>e.end.mode===`max`);d&&(s=o(e)),(u||d)&&c()}refresh(){if(this.#s)return;(this.#a||!this.#i||C(this.#t))&&(this.#p(),this.#m()),this.#v();let e=this.#t.length||this.#u.length?a():0;this.#C(e),this.#x(e),this.#T()}#T(){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)}#E(e,t){let n=U.filter(t=>t in e);if(n.length!==0)throw Error(`${t}: ${n.join(`, `)} ${n.length>1?`are`:`is`} not supported here; this module handles pinning and the scroll axis itself.`)}#D(e,t,n){if(J(e,t))throw Error(n)}#O(e,t){if(e===this.#e)throw Error(`${t}: trigger ${r(e)} is the shared container itself. Point trigger at an element inside the container.`);if(!this.#e.contains(e))throw Error(`${t}: trigger ${r(e)} is outside the shared container. Point trigger at an element inside it, or use createStickyPin, which works on either side.`)}#k(e,t){if(!e.contains(this.#e))return;let n=e===this.#e?`is the shared container itself`:`is an ancestor of the shared container`;throw Error(`${t}: trigger ${r(e)} ${n}. A pin wraps trigger in a height:0, contain:layout box, which would pull the container and every layer's dwell padding out of the flow. Pin an element that doesn't contain the container.`)}#A(e,t,n){if(this.#s)throw Error(`StickyScrollTrigger: cannot register a new layer after destroy() has been called.`);return this.#D(this.#t,e.trigger,`StickyScrollTrigger: ${r(e.trigger)} is already registered as a trigger for another layer. Each element can be used once per controller instance, across createStickyTrigger and createOverlapScroll: reusing one makes endTrigger resolution ambiguous.`),this.#D(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}){this.#E(s,`createStickyTrigger`);let c=t(e,`createStickyTrigger`);this.#O(c,`createStickyTrigger`);let l=n(c,a,`createStickyTrigger`);return this.#A({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}){this.#E(l,`createOverlapScroll`);let u=t(e,`createOverlapScroll`);this.#O(u,`createOverlapScroll`);let 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.#A(p,e=>{h(),c?.(e)},l),h=v(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.#E(c,`createStickyPin`),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`),d=t(a,`createStickyPin`);this.#k(l,`createStickyPin`),this.#D(this.#u,l,`StickyScrollTrigger: ${r(l)} is already registered as a pin trigger.`),this.#D(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 f={trigger:l,savedPosition:u(l),outer:null,inner:null,start:i===void 0?n??`top top`:q(i),endTrigger:d,end:o};return this.#u.push(f),{...c,trigger:l,onKill:e=>{this.#g(e);let t=this.#u.indexOf(f);t!==-1&&this.#u.splice(t,1),f.outer&&(g(f.outer,f.trigger,f.savedPosition),f.outer=null,f.inner=null),s?.(e)},onRefreshInit:this.#_(this.#u,f,c.onRefreshInit)}}resolveScrollPosition(e,n){let r=t(e,`resolveScrollPosition`),c=E(n),l=a();if(R(c))return o(l)+z(c,l);if(j(c))return M(c);let u=0;this.#t.forEach(e=>{e.kind===`scene`&&(s(e.trigger,r)>=0||(u+=e.freezeEnd-e.freezeStart))});let d=N(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}){this.#E(o,`createResolvedTrigger`);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&&g(e.outer,e.trigger,e.savedPosition)}),this.#u.length=0)}};export{Y as default};
|