sticky-scroll-trigger 0.2.0 → 0.3.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
|
@@ -9,6 +9,7 @@ The only dependency is GSAP's ScrollTrigger types.
|
|
|
9
9
|
- Pin multiple scenes in sequence, sharing a single container (no gap opens up from adjacent elements scrolling away while one is pinned)
|
|
10
10
|
- Pinning is handled 100% by the browser's `position:sticky`; GSAP only tweens the effect's own properties while the freeze window is active
|
|
11
11
|
- The "section below rises up and covers the section above" effect (overlap scroll) is achieved without changing document height
|
|
12
|
+
- Same-page anchor links, `scrollIntoView` and `:target` land where they should, with no code on your side (see [Same-page links](#same-page-links))
|
|
12
13
|
|
|
13
14
|
See the repository's `demo/index.html` + `demo/src/main.ts` for a live example, and `ARCHITECTURE.md` for why nested sticky was chosen over `pin`, and how it works internally.
|
|
14
15
|
|
|
@@ -132,10 +133,14 @@ Avoid debounce logic that assumes it runs before GSAP's 200ms delay. Use `refres
|
|
|
132
133
|
|
|
133
134
|
## API
|
|
134
135
|
|
|
135
|
-
### `new StickyScrollTrigger(root)`
|
|
136
|
+
### `new StickyScrollTrigger(root, options?)`
|
|
136
137
|
|
|
137
138
|
Creates an instance that treats `root` (a selector string or `HTMLElement`) as the shared container, exposing `createStickyTrigger`, `createOverlapScroll`, `createStickyPin`, `createResolvedTrigger`, `resolveScrollPosition`, `refresh`, and `destroy` as instance methods. Throws if the shared container can't be found. The class itself also exposes a static [`getScrollTop`](#stickyscrolltriggergetscrolltopelement-instances) method, for resolving a position across more than one instance.
|
|
138
139
|
|
|
140
|
+
| option | default | description |
|
|
141
|
+
| --------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
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
|
+
|
|
139
144
|
These are ordinary instance methods, not standalone functions, so always call them on the instance (`sticky.createStickyTrigger(...)`) rather than destructuring them out; a destructured method loses its `this` binding when called.
|
|
140
145
|
|
|
141
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.
|
|
@@ -262,17 +267,23 @@ Using it with `pin: true` is discouraged because the element can jump when pinni
|
|
|
262
267
|
|
|
263
268
|
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).
|
|
264
269
|
|
|
265
|
-
For a target that might belong to a _different_ instance than the one at hand
|
|
270
|
+
For a target that might belong to a _different_ instance than the one at hand, use the static [`getScrollTop`](#stickyscrolltriggergetscrolltopelement-instances) below instead.
|
|
266
271
|
|
|
267
272
|
### `StickyScrollTrigger.getScrollTop(element, instances)`
|
|
268
273
|
|
|
269
274
|
A static method, call it on the class itself (`StickyScrollTrigger.getScrollTop(...)`), not on an instance. Returns the absolute scroll position (px) at which `element`'s own top edge reaches the viewport's top edge (`'top top'`), picking whichever of the given `instances` actually has `element` inside its shared container. Applying the wrong instance's dwell to a target it never delayed would corrupt the result, the same way plain [`resolveScrollPosition`](#resolvescrollpositionelement-position) warns against.
|
|
270
275
|
|
|
276
|
+
Same-page links don't need this: [`scrollMarginTargets`](#same-page-links) already makes them land correctly on their own. Reach for it when you want to drive the scroll yourself (custom easing, a scroll library, a target that isn't a fragment link). If you do, pass `scrollMarginTargets: null` so the correction isn't applied twice.
|
|
277
|
+
|
|
271
278
|
```ts
|
|
272
279
|
import StickyScrollTrigger from "sticky-scroll-trigger";
|
|
273
280
|
|
|
274
|
-
const stickyA = new StickyScrollTrigger(".sectionA"
|
|
275
|
-
|
|
281
|
+
const stickyA = new StickyScrollTrigger(".sectionA", {
|
|
282
|
+
scrollMarginTargets: null,
|
|
283
|
+
});
|
|
284
|
+
const stickyB = new StickyScrollTrigger(".sectionB", {
|
|
285
|
+
scrollMarginTargets: null,
|
|
286
|
+
});
|
|
276
287
|
|
|
277
288
|
document.querySelectorAll('a[href^="#"]').forEach((anchor) => {
|
|
278
289
|
anchor.addEventListener("click", (event) => {
|
|
@@ -305,6 +316,33 @@ Tears down the controller, restores the original DOM layout, and reverts z-order
|
|
|
305
316
|
|
|
306
317
|
It only cleans up DOM/styles managed by this module. You still need to kill active ScrollTrigger instances yourself. After `destroy()`, `createStickyTrigger`/`createOverlapScroll` throw and `refresh()` is a no-op. Calling `destroy()` twice is safe.
|
|
307
318
|
|
|
319
|
+
## Same-page links
|
|
320
|
+
|
|
321
|
+
Pinning decouples an element's position in the document from the scroll position at which it actually reaches the top of the viewport. The browser's own "scroll an element into view" is a single calculation made from the current layout, so on its own it lands short by every preceding scene's dwell. This is true of any pinning technique, GSAP's own `pin` included.
|
|
322
|
+
|
|
323
|
+
`refresh()` declares that difference to the browser by keeping `scroll-margin-top` in sync on every element inside the shared container that matches `scrollMarginTargets` (`'[id]'` by default). Nothing else is needed: plain `<a href="#target">` links, `scrollIntoView()`, `:target` and a `#hash` on load all land correctly as written.
|
|
324
|
+
|
|
325
|
+
```html
|
|
326
|
+
<!-- Just works. No click handler, no scroll maths. -->
|
|
327
|
+
<a href="#chapter3">Chapter 3</a>
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
- 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
|
|
331
|
+
- A fixed header's own offset is a different case: it applies to every scroll, not just ones 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 on `scroll-padding-top` independently of this module's own correction. In practice, don't: real 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 the header offset into `--sst-scroll-margin-top-offset` instead (below), which sidesteps the combination entirely. See the repository's demo (`style.css`'s `html` rule)
|
|
332
|
+
- 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
|
|
333
|
+
|
|
334
|
+
```css
|
|
335
|
+
/* A fixed header's height, folded in here rather than into scroll-padding-top (see above). */
|
|
336
|
+
html {
|
|
337
|
+
--sst-scroll-margin-top-offset: 56px;
|
|
338
|
+
}
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
- The part of the correction that depends on where the jump was started from rides a CSS scroll-driven animation where [supported](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_scroll-driven_animations), so nothing runs on scroll there. Where it isn't (Firefox, as of this writing), a `scroll` listener supplies the same value instead, so the correction is exact from any starting position on every engine either way
|
|
342
|
+
- Known limitation: Chromium's own `scroll-behavior: smooth` doesn't run a native fragment jump as one continuous animation, and can under- or overshoot if `scroll-margin-top` changes at any point before it settles (which a jump started mid-dwell does, on purpose). This isn't specific to how the value is driven (CSS or JS) and has no known workaround; if you need animated same-page scrolling in Chromium, drive it yourself with [`getScrollTop`](#stickyscrolltriggergetscrolltopelement-instances) and `scrollTo({ top, behavior: 'smooth' })` to a precomputed target instead
|
|
343
|
+
- The default `'[id]'` covers everything a fragment link or `:target` can reach. Find-in-page and keyboard focus can scroll to arbitrary elements, which this doesn't cover; widen the selector if that matters to you
|
|
344
|
+
- To take this over yourself, pass `scrollMarginTargets: null` and compute positions with [`getScrollTop`](#stickyscrolltriggergetscrolltopelement-instances) instead. Don't do both: a handler that reads `scroll-margin-top` and also applies `getScrollTop` would count the correction twice
|
|
345
|
+
|
|
308
346
|
## Position syntax
|
|
309
347
|
|
|
310
348
|
`start` (and `end`'s position-clause form) accepts GSAP's standard `"element-side viewport-side"` syntax. With only one clause, the viewport side defaults to `top` (`"center"` is equivalent to `"center top"`, not `"center center"`). You can also pass a function that returns a string, or for the absolute scroll position case below, a number. It's re-evaluated on every `refresh()`, which is handy for reflecting variable values like header height.
|
|
@@ -397,6 +435,7 @@ sticky.createOverlapScroll({
|
|
|
397
435
|
- Using the same element as the `trigger` of two different `createStickyTrigger`/`createOverlapScroll`/`createStickyPin` calls throws
|
|
398
436
|
- 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))
|
|
399
437
|
- Resizes from mobile browsers showing/hiding their address bar are absorbed automatically (see the repository's `ARCHITECTURE.md`, "Two-pass position measurement"), but other causes like `visualViewport` zoom are not handled
|
|
438
|
+
- `refresh()` writes an inline `scroll-margin-top` on every element matching `scrollMarginTargets` inside the shared container, and appends one `<style>` element to `<head>` per instance. Both are undone by `destroy()`. Pass `scrollMarginTargets: null` to opt out (see [Same-page links](#same-page-links))
|
|
400
439
|
- Don't nest one `StickyScrollTrigger` instance's shared container inside another's: once a target sits inside more than one, [`getScrollTop`](#stickyscrolltriggergetscrolltopelement-instances)'s ownership check picks whichever instance is listed first, and neither instance's own dwell alone is actually correct for it
|
|
401
440
|
|
|
402
441
|
## License
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @license
|
|
3
|
-
* StickyScrollTrigger v0.
|
|
3
|
+
* StickyScrollTrigger v0.3.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
|
|
@@ -48,7 +48,7 @@ declare type PositionValue = string | number;
|
|
|
48
48
|
|
|
49
49
|
declare class StickyScrollTrigger {
|
|
50
50
|
#private;
|
|
51
|
-
constructor(root: string | HTMLElement);
|
|
51
|
+
constructor(root: string | HTMLElement, options?: StickyScrollTriggerOptions);
|
|
52
52
|
refresh(): void;
|
|
53
53
|
createStickyTrigger({ trigger: triggerInput, start, end, endTrigger: endTriggerInput, onKill, ...rest }: CreateStickyTriggerOptions): ScrollTrigger.Vars;
|
|
54
54
|
createOverlapScroll({ trigger: triggerInput, cover, start, end, endTrigger: endTriggerInput, onKill, ...rest }: CreateOverlapScrollOptions): ScrollTrigger.Vars;
|
|
@@ -60,4 +60,8 @@ declare class StickyScrollTrigger {
|
|
|
60
60
|
}
|
|
61
61
|
export default StickyScrollTrigger;
|
|
62
62
|
|
|
63
|
+
export declare interface StickyScrollTriggerOptions {
|
|
64
|
+
scrollMarginTargets?: string | null;
|
|
65
|
+
}
|
|
66
|
+
|
|
63
67
|
export { }
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @license
|
|
3
|
-
* StickyScrollTrigger v0.
|
|
3
|
+
* StickyScrollTrigger v0.3.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
|
|
@@ -154,6 +154,108 @@ var StickyScrollTrigger = (function() {
|
|
|
154
154
|
lifted.length = 0;
|
|
155
155
|
};
|
|
156
156
|
};
|
|
157
|
+
const OFFSET_PROPERTY = "--sst-scroll-margin-top-offset";
|
|
158
|
+
let nextInstanceSuffix = 0;
|
|
159
|
+
const supportsScrollDrivenAnimations = typeof CSS !== "undefined" && typeof CSS.supports === "function" && CSS.supports("animation-timeline", "scroll()");
|
|
160
|
+
const buildStylesheet = (instanceId, scenes) => {
|
|
161
|
+
const declarations = [];
|
|
162
|
+
const animations = [];
|
|
163
|
+
const timelines = [];
|
|
164
|
+
const ranges = [];
|
|
165
|
+
scenes.forEach((scene, index) => {
|
|
166
|
+
const property = `--${instanceId}-c${index}`;
|
|
167
|
+
const keyframes = `${instanceId}-k${index}`;
|
|
168
|
+
declarations.push(`@property ${property}{syntax:"<length>";inherits:true;initial-value:0px}`, `@keyframes ${keyframes}{from{${property}:0px}to{${property}:${scene.freezeEnd - scene.freezeStart}px}}`);
|
|
169
|
+
animations.push(`${keyframes} linear both`);
|
|
170
|
+
timelines.push("scroll(root block)");
|
|
171
|
+
ranges.push(`${scene.freezeStart}px ${scene.freezeEnd}px`);
|
|
172
|
+
});
|
|
173
|
+
declarations.push(`@supports (animation-timeline: scroll()) {[data-${instanceId}]{animation:${animations.join(",")};animation-timeline:${timelines.join(",")};animation-range:${ranges.join(",")}}}`);
|
|
174
|
+
return declarations.join("");
|
|
175
|
+
};
|
|
176
|
+
const createScrollMarginSync = (rootElement) => {
|
|
177
|
+
const instanceId = `sst${nextInstanceSuffix++}`;
|
|
178
|
+
const snapshots = new WeakMap();
|
|
179
|
+
let written = new Set();
|
|
180
|
+
let styleElement = null;
|
|
181
|
+
let animationHost = null;
|
|
182
|
+
let jsRamps = [];
|
|
183
|
+
let jsHost = null;
|
|
184
|
+
const applyJsRamp = () => {
|
|
185
|
+
if (!jsHost) return;
|
|
186
|
+
const scroll = window.scrollY;
|
|
187
|
+
jsRamps.forEach((scene, index) => {
|
|
188
|
+
const consumed = Math.min(Math.max(scroll - scene.freezeStart, 0), scene.freezeEnd - scene.freezeStart);
|
|
189
|
+
jsHost.style.setProperty(`--${instanceId}-c${index}`, `${consumed}px`);
|
|
190
|
+
});
|
|
191
|
+
};
|
|
192
|
+
if (!supportsScrollDrivenAnimations) window.addEventListener("scroll", applyJsRamp, { passive: true });
|
|
193
|
+
const restoreTarget = (target) => {
|
|
194
|
+
const snapshot = snapshots.get(target);
|
|
195
|
+
target.style.scrollMarginTop = snapshot ? snapshot.inline : "";
|
|
196
|
+
};
|
|
197
|
+
const clearAnimation = () => {
|
|
198
|
+
styleElement?.remove();
|
|
199
|
+
styleElement = null;
|
|
200
|
+
animationHost?.removeAttribute(`data-${instanceId}`);
|
|
201
|
+
animationHost = null;
|
|
202
|
+
jsRamps = [];
|
|
203
|
+
jsHost = null;
|
|
204
|
+
};
|
|
205
|
+
return {
|
|
206
|
+
sync(scenes, host, targetSelector) {
|
|
207
|
+
const ramps = scenes.filter((scene) => scene.freezeEnd > scene.freezeStart);
|
|
208
|
+
if (targetSelector === null || !ramps.length || !host) {
|
|
209
|
+
written.forEach(restoreTarget);
|
|
210
|
+
written = new Set();
|
|
211
|
+
clearAnimation();
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
if (animationHost !== host) {
|
|
215
|
+
animationHost?.removeAttribute(`data-${instanceId}`);
|
|
216
|
+
animationHost = host;
|
|
217
|
+
animationHost.setAttribute(`data-${instanceId}`, "");
|
|
218
|
+
}
|
|
219
|
+
if (!supportsScrollDrivenAnimations) {
|
|
220
|
+
jsRamps = ramps;
|
|
221
|
+
jsHost = host;
|
|
222
|
+
applyJsRamp();
|
|
223
|
+
}
|
|
224
|
+
if (!styleElement) {
|
|
225
|
+
styleElement = document.createElement("style");
|
|
226
|
+
document.head.appendChild(styleElement);
|
|
227
|
+
}
|
|
228
|
+
styleElement.textContent = buildStylesheet(instanceId, ramps);
|
|
229
|
+
const targets = Array.from(rootElement.querySelectorAll(targetSelector));
|
|
230
|
+
targets.forEach((target) => {
|
|
231
|
+
const existing = snapshots.get(target);
|
|
232
|
+
if (!existing) snapshots.set(target, { inline: target.style.scrollMarginTop });
|
|
233
|
+
else if (written.has(target)) target.style.scrollMarginTop = existing.inline;
|
|
234
|
+
});
|
|
235
|
+
const consumed = ramps.map((_, index) => `var(--${instanceId}-c${index}, 0px)`).join(" + ");
|
|
236
|
+
targets.forEach((target) => {
|
|
237
|
+
const authorPx = parseFloat(getComputedStyle(target).scrollMarginTop) || 0;
|
|
238
|
+
let lag = 0;
|
|
239
|
+
ramps.forEach((scene) => {
|
|
240
|
+
if (compareDocumentOrder(scene.trigger, target) >= 0) return;
|
|
241
|
+
lag += scene.freezeEnd - scene.freezeStart;
|
|
242
|
+
});
|
|
243
|
+
target.style.scrollMarginTop = `calc(${authorPx}px + var(${OFFSET_PROPERTY}, 0px) + ${consumed} - ${lag}px)`;
|
|
244
|
+
});
|
|
245
|
+
const next = new Set(targets);
|
|
246
|
+
written.forEach((target) => {
|
|
247
|
+
if (!next.has(target)) restoreTarget(target);
|
|
248
|
+
});
|
|
249
|
+
written = next;
|
|
250
|
+
},
|
|
251
|
+
restore() {
|
|
252
|
+
if (!supportsScrollDrivenAnimations) window.removeEventListener("scroll", applyJsRamp);
|
|
253
|
+
written.forEach(restoreTarget);
|
|
254
|
+
written = new Set();
|
|
255
|
+
clearAnimation();
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
};
|
|
157
259
|
const isDomOrderStale = (layers) => {
|
|
158
260
|
for (let i = 1; i < layers.length; i += 1) if (compareDocumentOrder(layers[i - 1].trigger, layers[i].trigger) > 0) return true;
|
|
159
261
|
return false;
|
|
@@ -420,8 +522,12 @@ var StickyScrollTrigger = (function() {
|
|
|
420
522
|
#rebuildScheduled = false;
|
|
421
523
|
#scrollTriggerClass = null;
|
|
422
524
|
#pinLayers = [];
|
|
423
|
-
|
|
525
|
+
#scrollMarginSync;
|
|
526
|
+
#scrollMarginTargets;
|
|
527
|
+
constructor(root, options = {}) {
|
|
424
528
|
this.#rootElement = resolveRoot(root);
|
|
529
|
+
this.#scrollMarginSync = createScrollMarginSync(this.#rootElement);
|
|
530
|
+
this.#scrollMarginTargets = options.scrollMarginTargets === void 0 ? "[id]" : options.scrollMarginTargets;
|
|
425
531
|
}
|
|
426
532
|
#unbuild() {
|
|
427
533
|
if (!this.#built) return;
|
|
@@ -543,6 +649,14 @@ var StickyScrollTrigger = (function() {
|
|
|
543
649
|
const viewportHeight = this.#layers.length || this.#pinLayers.length ? measureViewportHeight() : 0;
|
|
544
650
|
this.#refreshScenesAndCovers(viewportHeight);
|
|
545
651
|
this.#refreshPins(viewportHeight);
|
|
652
|
+
this.#syncScrollMargins();
|
|
653
|
+
}
|
|
654
|
+
#syncScrollMargins() {
|
|
655
|
+
this.#scrollMarginSync.sync(this.#layers.filter((layer) => layer.kind === "scene").map(({ trigger, freezeStart, freezeEnd }) => ({
|
|
656
|
+
trigger,
|
|
657
|
+
freezeStart,
|
|
658
|
+
freezeEnd
|
|
659
|
+
})), this.#outermostContainer, this.#scrollMarginTargets);
|
|
546
660
|
}
|
|
547
661
|
#assertTriggerAvailable(list, trigger, message) {
|
|
548
662
|
if (hasDuplicateTrigger(list, trigger)) throw new Error(message);
|
|
@@ -680,6 +794,7 @@ var StickyScrollTrigger = (function() {
|
|
|
680
794
|
destroy() {
|
|
681
795
|
if (this.#destroyed) return;
|
|
682
796
|
this.#destroyed = true;
|
|
797
|
+
this.#scrollMarginSync.restore();
|
|
683
798
|
this.#layers.forEach((layer) => {
|
|
684
799
|
if (layer.kind === "cover") this.#coverRestoreByLayer.get(layer)?.();
|
|
685
800
|
});
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @license
|
|
3
|
-
* StickyScrollTrigger v0.
|
|
3
|
+
* StickyScrollTrigger v0.3.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}},_=e=>{for(let t=1;t<e.length;t+=1)if(s(e[t-1].trigger,e[t].trigger)>0)return!0;return!1},v=(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},y=(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)})},b=e=>typeof e==`function`?e():e,x={top:0,center:.5,bottom:1},S=(e,t,n,r)=>{let i=n===`%`?parseFloat(t)/100*r:parseFloat(t);return e?.startsWith(`-`)?-i:i},C=/^(top|center|bottom|(?:\d+\.?\d*|\.\d+))?(%|px)?(?:([+-]=?)(\d+\.?\d*|\.\d+)(%|px)?)?$/,w=(e,t)=>{let n=C.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 x,l=i===`%`;return{fraction:c?x[r]:l?parseFloat(r)/100:0,offsetPx:(!c&&!l&&r!==void 0?parseFloat(r):0)+(a?S(a,o,s,t):0)}},T=e=>typeof e==`number`||e.trim()!==``&&!Number.isNaN(Number(e)),E=e=>typeof e==`number`?e:Number(e),D=(e,t,n)=>{let[r,i=`top`]=e.trim().split(/\s+/),a=w(r,t),o=w(i,n);return n*o.fraction+o.offsetPx-(t*a.fraction+a.offsetPx)},O=/^\+=(\d+\.?\d*|\.\d+)(%|px)?$/,k=e=>typeof e==`string`&&O.test(e.trim()),A=(e,t)=>{let n=O.exec(String(e).trim());if(!n)throw Error(`StickyScrollTrigger: unsupported end format "${e}"`);let[,r,i]=n;return S(void 0,r,i,t)},j=/^max(?:([+-]=)(\d+\.?\d*|\.\d+)(%|px)?)?$/,M=e=>typeof e==`string`&&j.test(e.trim()),N=(e,t)=>{let n=j.exec(e.trim());if(!n)throw Error(`StickyScrollTrigger: unsupported max-position format "${e}"`);let[,r,i,a]=n;return r?S(r,i,a,t):0},P=(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},F=(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+P(e,s,a,l.end.rawTop,u):t}let r=D(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}},I=(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}=F(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=F(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},L=(e,t,n,a,o,s)=>{if(a===null)return{mode:`auto`};if(k(a))return{mode:`dwell`,distancePx:Math.max(0,A(a,t))};if(T(a))return{mode:`absolute`,value:E(a)};if(M(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:N(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}},R=(e,t,n,i)=>{if(!T(t))return{mode:`clause`,anchorOffset:D(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:E(t)}},z=(e,t,n,r,a)=>{let o=b(n.start),s=n.end===null?null:b(n.end),c=n.trigger.offsetHeight,l=n.endTrigger===n.trigger,u=a.get(n.endTrigger)??null,d=L(e,t,n,s,r,u),f=R(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}},B=(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=[];constructor(t){this.#e=e(t)}#d(){this.#i&&=(y(this.#e,this.#n,this.#o),this.#o=null,!1)}#f(){this.#o=v(this.#e,this.#t),this.#n=[...this.#t],this.#i=!0,this.#a=!1}#p(){this.#c||this.#s||(this.#c=!0,queueMicrotask(()=>{this.#c=!1,!this.#s&&(this.refresh(),typeof this.#l?.refresh==`function`&&this.#l.refresh())}))}#m(e){this.#l=e.constructor}#h(e,t,n){return r=>{this.#m(r),e[0]===t&&this.refresh(),n?.(r)}}#g(){this.#u.forEach(e=>{if(e.outer)return;let t=p(e.trigger);e.outer=t.outer,e.inner=t.inner})}#_(){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})}}#v(e){if(!this.#u.length)return;let t=this.#_();this.#u.forEach(e=>{c(e.trigger),e.inner&&(e.inner.style.height=``)}),this.#u.forEach(t=>{if(!t.inner)return;let n=b(t.end);if(M(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=b(t.top),a=i(t.trigger),o=t.trigger.offsetHeight,s=(T(n)?E(n):i(t.endTrigger)-D(n,t.endTrigger.offsetHeight,e))-a+r+o;l(t.trigger,r),t.inner.style.height=`${Math.max(0,s)}px`}),t()}#y(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)=>z(this.#e,e,t,n,r)),s=0,u=()=>I(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||_(this.#t))&&(this.#d(),this.#f()),this.#g();let e=this.#t.length||this.#u.length?a():0;this.#y(e),this.#v(e)}#b(e,t,n){if(B(e,t))throw Error(n)}#x(e,t,n){if(this.#s)throw Error(`StickyScrollTrigger: cannot register a new layer after destroy() has been called.`);return this.#b(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.#b(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.#m(n);let r=this.#t.indexOf(e);r!==-1&&this.#t.splice(r,1),this.#a=!0,this.#p(),t?.(n)},onRefreshInit:this.#h(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.#x({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.#x(p,e=>{h(),c?.(e)},l),h=g(f);return this.#r.set(p,h),m}createStickyPin({trigger:e,top:n=0,endTrigger:i,end:a=`top top`,onKill:o,...s}){if(this.#s)throw Error(`StickyScrollTrigger: cannot register a new pin after destroy() has been called.`);let c=t(e,`createStickyPin`),l=t(i,`createStickyPin`);this.#b(this.#u,c,`StickyScrollTrigger: ${r(c)} is already registered as a pin trigger.`),this.#b(this.#t,c,`StickyScrollTrigger: ${r(c)} is already registered as a trigger for another layer (createStickyTrigger/createOverlapScroll). Using the same element as both a Scene/Cover trigger and a pin trigger wraps it twice and produces conflicting position:sticky behavior.`);let u={trigger:c,outer:null,inner:null,top:n,endTrigger:l,end:a};return this.#u.push(u),{...s,trigger:c,onKill:e=>{this.#m(e);let t=this.#u.indexOf(u);t!==-1&&this.#u.splice(t,1),u.outer&&(m(u.outer,u.trigger),u.outer=null,u.inner=null),o?.(e)},onRefreshInit:this.#h(this.#u,u,s.onRefreshInit)}}resolveScrollPosition(e,n){let r=t(e,`resolveScrollPosition`),c=b(n),l=a();if(M(c))return o(l)+N(c,l);if(T(c))return E(c);let u=0;this.#t.forEach(e=>{e.kind===`scene`&&(s(e.trigger,r)>=0||(u+=e.freezeEnd-e.freezeStart))});let d=D(c,r.offsetHeight,l),f=this.#_(),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.#t.forEach(e=>{e.kind===`cover`&&this.#r.get(e)?.()}),this.#d(),this.#t.length=0,this.#n=[],this.#u.forEach(e=>{e.outer&&m(e.outer,e.trigger)}),this.#u.length=0)}}})();
|
|
8
|
+
var StickyScrollTrigger=(function(){let e=e=>{let t=typeof e==`string`?document.querySelector(e):e;if(!t){let t=typeof e==`string`?`"${e}"`:String(e);throw Error(`StickyScrollTrigger: root ${t} not found`)}return t},t=(e,t)=>{let n=typeof e==`string`?document.querySelector(e):e;if(!n){let n=typeof e==`string`?`"${e}"`:String(e);throw Error(`${t}: element ${n} not found`)}return n},n=(e,n,r)=>n===void 0?e:t(n,r),r=e=>{let t=e.id?`#${e.id}`:``,n=e.classList.length>0?`.${Array.from(e.classList).join(`.`)}`:``;return`<${e.tagName.toLowerCase()}${t}${n}>`},i=e=>{let t=0,n=e;for(;n;){t+=n.offsetTop;let e=n.offsetParent;e&&(t+=e.clientTop),n=e}return t},a=()=>{let e=document.createElement(`div`);e.style.height=`100vh`,e.style.position=`absolute`,document.body.appendChild(e);let t=e.offsetHeight||window.innerHeight;return e.remove(),t},o=e=>{let t=document.documentElement;return Math.max(0,(t.scrollHeight||document.body.scrollHeight)-e)},s=(e,t)=>{let n=e.compareDocumentPosition(t);return n&Node.DOCUMENT_POSITION_FOLLOWING?-1:n&Node.DOCUMENT_POSITION_PRECEDING?1:0},c=e=>{e.style.position=``,e.style.top=``},l=(e,t)=>{e.style.position=`sticky`,e.style.top=`${t}px`},u=e=>{if(!e.parentNode)throw Error(`StickyScrollTrigger: ${r(e)} is not attached to the document`);let t=document.createElement(`div`),n=document.createElement(`div`),i=document.createElement(`div`);return i.setAttribute(`aria-hidden`,`true`),i.style.pointerEvents=`none`,e.parentNode.insertBefore(t,e),t.appendChild(n),n.appendChild(e),t.appendChild(i),{container:t,wrapper:n,padding:i}},d=e=>{let t=e.parentNode;if(!t)throw Error(`createOverlapScroll: trigger ${r(e)} is not attached to the document`);let n=document.createElement(`div`);for(t.insertBefore(n,t.firstChild);n.nextSibling;){let t=n.nextSibling;if(n.appendChild(t),t===e)break}return n},f=e=>{let t=e.parentNode;if(t){for(;e.firstChild;)t.insertBefore(e.firstChild,e);e.remove()}},p=e=>{if(!e.parentNode)throw Error(`createStickyPin: trigger ${r(e)} is not attached to the document`);let t=document.createElement(`div`),n=document.createElement(`div`);return t.style.height=`0`,t.style.contain=`layout`,e.parentNode.insertBefore(t,e),t.appendChild(n),n.appendChild(e),{outer:t,inner:n}},m=(e,t)=>{let n=e.parentNode;c(t),n&&n.insertBefore(t,e),e.remove()},h=new WeakMap,g=e=>{let t=[];for(let n=e;n;n=n.nextElementSibling){if(!(n instanceof HTMLElement))continue;let e=h.get(n);if(e)e.count+=1;else{let e=getComputedStyle(n),t={count:1,position:n.style.position,zIndex:n.style.zIndex};e.position===`static`&&(n.style.position=`relative`),e.zIndex===`auto`&&(n.style.zIndex=`1`),h.set(n,t)}t.push(n)}return()=>{t.forEach(e=>{let t=h.get(e);t&&(--t.count,!(t.count>0)&&(e.style.position=t.position,e.style.zIndex=t.zIndex,h.delete(e)))}),t.length=0}},_=0,v=typeof CSS<`u`&&typeof CSS.supports==`function`&&CSS.supports(`animation-timeline`,`scroll()`),y=(e,t)=>{let n=[],r=[],i=[],a=[];return t.forEach((t,o)=>{let s=`--${e}-c${o}`,c=`${e}-k${o}`;n.push(`@property ${s}{syntax:"<length>";inherits:true;initial-value:0px}`,`@keyframes ${c}{from{${s}:0px}to{${s}:${t.freezeEnd-t.freezeStart}px}}`),r.push(`${c} linear both`),i.push(`scroll(root block)`),a.push(`${t.freezeStart}px ${t.freezeEnd}px`)}),n.push(`@supports (animation-timeline: scroll()) {[data-${e}]{animation:${r.join(`,`)};animation-timeline:${i.join(`,`)};animation-range:${a.join(`,`)}}}`),n.join(``)},b=e=>{let t=`sst${_++}`,n=new WeakMap,r=new Set,i=null,a=null,o=[],c=null,l=()=>{if(!c)return;let e=window.scrollY;o.forEach((n,r)=>{let i=Math.min(Math.max(e-n.freezeStart,0),n.freezeEnd-n.freezeStart);c.style.setProperty(`--${t}-c${r}`,`${i}px`)})};v||window.addEventListener(`scroll`,l,{passive:!0});let u=e=>{let t=n.get(e);e.style.scrollMarginTop=t?t.inline:``},d=()=>{i?.remove(),i=null,a?.removeAttribute(`data-${t}`),a=null,o=[],c=null};return{sync(f,p,m){let h=f.filter(e=>e.freezeEnd>e.freezeStart);if(m===null||!h.length||!p){r.forEach(u),r=new Set,d();return}a!==p&&(a?.removeAttribute(`data-${t}`),a=p,a.setAttribute(`data-${t}`,``)),v||(o=h,c=p,l()),i||(i=document.createElement(`style`),document.head.appendChild(i)),i.textContent=y(t,h);let g=Array.from(e.querySelectorAll(m));g.forEach(e=>{let t=n.get(e);t?r.has(e)&&(e.style.scrollMarginTop=t.inline):n.set(e,{inline:e.style.scrollMarginTop})});let _=h.map((e,n)=>`var(--${t}-c${n}, 0px)`).join(` + `);g.forEach(e=>{let t=parseFloat(getComputedStyle(e).scrollMarginTop)||0,n=0;h.forEach(t=>{s(t.trigger,e)>=0||(n+=t.freezeEnd-t.freezeStart)}),e.style.scrollMarginTop=`calc(${t}px + var(--sst-scroll-margin-top-offset, 0px) + ${_} - ${n}px)`});let b=new Set(g);r.forEach(e=>{b.has(e)||u(e)}),r=b},restore(){v||window.removeEventListener(`scroll`,l),r.forEach(u),r=new Set,d()}}},x=e=>{for(let t=1;t<e.length;t+=1)if(s(e[t-1].trigger,e[t].trigger)>0)return!0;return!1},S=(e,t)=>{t.sort((e,t)=>s(e.trigger,t.trigger));for(let e of t)e.kind===`cover`&&(e.wrapper=d(e.trigger));let n=e,r=null;for(let e of t){if(e.kind!==`scene`)continue;let t=u(n);e.container=t.container,e.wrapper=t.wrapper,e.padding=t.padding,n=t.container,r=t.container}return r},C=(e,t,n)=>{n&&(n.parentNode?.insertBefore(e,n),n.remove());for(let e=t.length-1;e>=0;--e){let n=t[e];n.kind===`cover`&&n.wrapper&&f(n.wrapper)}t.forEach(e=>{e.wrapper=null,e.kind===`scene`&&(e.container=null,e.padding=null)})},w=e=>typeof e==`function`?e():e,T={top:0,center:.5,bottom:1},E=(e,t,n,r)=>{let i=n===`%`?parseFloat(t)/100*r:parseFloat(t);return e?.startsWith(`-`)?-i:i},D=/^(top|center|bottom|(?:\d+\.?\d*|\.\d+))?(%|px)?(?:([+-]=?)(\d+\.?\d*|\.\d+)(%|px)?)?$/,O=(e,t)=>{let n=D.exec(e);if(!n||!n[1]&&!n[3])throw Error(`StickyScrollTrigger: unsupported position clause "${e}"`);let[,r,i,a,o,s]=n;if(r!==void 0&&a&&!a.includes(`=`)){let t=`${r}${i??``}${a}=${o}${s??``}`;throw Error(`StickyScrollTrigger: unsupported position clause "${e}": a signed offset needs an explicit '=' when it follows a keyword or number (GSAP itself requires this too, to tell "${r}" and the offset apart). Did you mean "${t}"?`)}let c=r!==void 0&&r in T,l=i===`%`;return{fraction:c?T[r]:l?parseFloat(r)/100:0,offsetPx:(!c&&!l&&r!==void 0?parseFloat(r):0)+(a?E(a,o,s,t):0)}},k=e=>typeof e==`number`||e.trim()!==``&&!Number.isNaN(Number(e)),A=e=>typeof e==`number`?e:Number(e),j=(e,t,n)=>{let[r,i=`top`]=e.trim().split(/\s+/),a=O(r,t),o=O(i,n);return n*o.fraction+o.offsetPx-(t*a.fraction+a.offsetPx)},M=/^\+=(\d+\.?\d*|\.\d+)(%|px)?$/,N=e=>typeof e==`string`&&M.test(e.trim()),P=(e,t)=>{let n=M.exec(String(e).trim());if(!n)throw Error(`StickyScrollTrigger: unsupported end format "${e}"`);let[,r,i]=n;return E(void 0,r,i,t)},F=/^max(?:([+-]=)(\d+\.?\d*|\.\d+)(%|px)?)?$/,I=e=>typeof e==`string`&&F.test(e.trim()),L=(e,t)=>{let n=F.exec(e.trim());if(!n)throw Error(`StickyScrollTrigger: unsupported max-position format "${e}"`);let[,r,i,a]=n;return r?E(r,i,a,t):0},R=(e,t,n,r,i)=>{let a=0;return e.forEach((e,o)=>{if(o===i||e.triggerTop>=r)return;let s=t[o]===void 0?n?n.paddings[o]:null:t[o];s!=null&&(a+=s)}),a},z=(e,{viewportHeight:t,structureTop:n,documentMaxScroll:r,measureLiveEndTriggerTop:i},a)=>{let o=[],s=[],c=0;return{plans:e.map((l,u)=>{let d=l.triggerTop+c,f=l.start.mode===`absolute`?l.start.value:d-l.start.anchorOffset,p;switch(l.end.mode){case`auto`:if(l.start.mode!==`clause`)throw Error(`StickyScrollTrigger: internal error: an absolute start reached 'auto' end mode, which resolveStartSpec should have already rejected.`);p=f+Math.max(0,l.start.anchorOffset+(l.coverTop-l.triggerTop));break;case`dwell`:p=f+l.end.distancePx;break;case`absolute`:p=Math.max(f,l.end.value);break;case`max`:p=Math.max(f,r+l.end.offsetPx);break;case`clause`:{let n;if(l.endTriggerIsSelf)n=d;else{let t=l.endTriggerIndex===null?void 0:o[l.endTriggerIndex]===void 0?a?a.naturalTops[l.endTriggerIndex]:void 0:o[l.endTriggerIndex];n=t===void 0?l.end.measureLive?i(u):l.end.rawTop===null?0:l.end.rawTop+R(e,s,a,l.end.rawTop,u):t}let r=j(l.end.clause,l.endTriggerHeight,t);p=Math.max(f,n-r);break}}let m;if(l.kind===`cover`){if(l.start.mode!==`clause`)throw Error(`StickyScrollTrigger: internal error: a cover layer measurement carries an absolute start, which resolveStartSpec should have already rejected.`);m={freezeStart:f,freezeEnd:p,stickyTop:l.start.anchorOffset-(l.triggerTop-l.wrapperTop),paddingHeight:null}}else m={freezeStart:f,freezeEnd:p,stickyTop:n-f,paddingHeight:Math.max(0,p-f)};return m.paddingHeight!==null&&(c+=m.paddingHeight),o[u]=d,s[u]=m.paddingHeight,m}),naturalTops:o}},B=(e,t)=>{let n=e.some((e,t)=>e.end.mode!==`clause`||e.endTriggerIsSelf?!1:e.endTriggerIndex===null?e.end.rawTop!==null:e.endTriggerIndex>=t),r=new Map,i={...t,measureLiveEndTriggerTop:e=>{let n=r.get(e);if(n!==void 0)return n;let i=t.measureLiveEndTriggerTop(e);return r.set(e,i),i}},{plans:a,naturalTops:o}=z(e,i,null);if(n)for(let t=0;t<e.length;t+=1){let n={paddings:a.map(e=>e.paddingHeight),naturalTops:o},r=z(e,i,n),s=r.plans.every((e,t)=>e.paddingHeight===a[t].paddingHeight);if(a=r.plans,o=r.naturalTops,s)break;if(t===e.length-1)throw Error(`StickyScrollTrigger: could not resolve endTrigger positions: some Scene layers' endTrigger references form a circular structural dependency (either two or more triggers reference each other's endTrigger, or an unregistered endTrigger's dwell depends on a layer whose own end depends back on it). Point each endTrigger at a layer that doesn't depend on it.`)}return a.forEach((e,n)=>t.onPlanned(n,e)),a},V=(e,t,n,a,o,s)=>{if(a===null)return{mode:`auto`};if(N(a))return{mode:`dwell`,distancePx:Math.max(0,P(a,t))};if(k(a))return{mode:`absolute`,value:A(a)};if(I(a)){if(n.kind!==`cover`)throw Error(`StickyScrollTrigger: createStickyTrigger's trigger ${r(n.trigger)} has end "${a}", which uses GSAP's 'max' keyword. This isn't supported here (a Scene layer's own dwell padding contributes to the document's max scroll position, so the freeze window would depend on itself and never converge). Use createOverlapScroll instead (cover layers never add padding), or a dwell distance such as '+=500'.`);return{mode:`max`,offsetPx:L(a,t)}}if(n.kind!==`cover`&&s!==null&&s>o)throw Error(`StickyScrollTrigger: createStickyTrigger's trigger ${r(n.trigger)}'s endTrigger (${r(n.endTrigger)}) refers to a layer positioned later in DOM order. This isn't supported (the layer's own dwell padding always precedes and pushes down that later layer's position, so the freeze window would depend on its own dwell and never converge). Point endTrigger at a layer registered earlier in DOM order, or use a dwell distance such as '+=500' instead.`);let c=s===null,l=e.contains(n.endTrigger);if(c&&!l&&n.kind!==`cover`)throw Error(`StickyScrollTrigger: createStickyTrigger's trigger ${r(n.trigger)} has an endTrigger (${r(n.endTrigger)}) outside the shared container (the layer's own dwell would push it away, so the freeze window never converges). Point endTrigger at an element inside the container, or use a dwell distance such as '+=500'.`);return{mode:`clause`,clause:a,rawTop:c&&l?i(n.endTrigger):null,measureLive:c&&!l}},H=(e,t,n,i)=>{if(!k(t))return{mode:`clause`,anchorOffset:j(t,n,i)};if(e.kind===`cover`)throw Error(`StickyScrollTrigger: createOverlapScroll's trigger ${r(e.trigger)} has start "${t}", an absolute scroll position (GSAP treats any bare-number start this way). This isn't supported here: a cover layer's sticky position is always computed relative to its own wrapper, which needs start to be a position clause (e.g. 'bottom bottom'), not an absolute scroll position.`);return{mode:`absolute`,value:A(t)}},U=(e,t,n,r,a)=>{let o=w(n.start),s=n.end===null?null:w(n.end),c=n.trigger.offsetHeight,l=n.endTrigger===n.trigger,u=a.get(n.endTrigger)??null,d=V(e,t,n,s,r,u),f=H(n,o,c,t);return{kind:n.kind,start:f,triggerTop:i(n.trigger),wrapperTop:n.wrapper?i(n.wrapper):0,coverTop:n.kind===`cover`?i(n.cover):0,end:d,endTriggerIsSelf:l,endTriggerIndex:u,endTriggerHeight:d.mode===`clause`?n.endTrigger.offsetHeight:0}},W=(e,t)=>e.some(e=>e.trigger===t);return class{#e;#t=[];#n=[];#r=new WeakMap;#i=!1;#a=!1;#o=null;#s=!1;#c=!1;#l=null;#u=[];#d;#f;constructor(t,n={}){this.#e=e(t),this.#d=b(this.#e),this.#f=n.scrollMarginTargets===void 0?`[id]`:n.scrollMarginTargets}#p(){this.#i&&=(C(this.#e,this.#n,this.#o),this.#o=null,!1)}#m(){this.#o=S(this.#e,this.#t),this.#n=[...this.#t],this.#i=!0,this.#a=!1}#h(){this.#c||this.#s||(this.#c=!0,queueMicrotask(()=>{this.#c=!1,!this.#s&&(this.refresh(),typeof this.#l?.refresh==`function`&&this.#l.refresh())}))}#g(e){this.#l=e.constructor}#_(e,t,n){return r=>{this.#g(r),e[0]===t&&this.refresh(),n?.(r)}}#v(){this.#u.forEach(e=>{if(e.outer)return;let t=p(e.trigger);e.outer=t.outer,e.inner=t.inner})}#y(){let e=this.#t.map(e=>e.wrapper).filter(e=>e!==null).map(e=>({wrapper:e,position:e.style.position,top:e.style.top}));return e.forEach(({wrapper:e})=>c(e)),()=>{e.forEach(({wrapper:e,position:t,top:n})=>{e.style.position=t,e.style.top=n})}}#b(e){if(!this.#u.length)return;let t=this.#y();this.#u.forEach(e=>{c(e.trigger),e.inner&&(e.inner.style.height=``)}),this.#u.forEach(t=>{if(!t.inner)return;let n=w(t.end);if(I(n))throw Error(`StickyScrollTrigger: createStickyPin's end "${n}" uses GSAP's 'max' keyword, which isn't supported here (the pin's own spacer height contributes to the document's max scroll position, so the spacer height would depend on itself and never converge). Use a plain position clause such as 'top top' against endTrigger instead.`);let r=w(t.top),a=i(t.trigger),o=t.trigger.offsetHeight,s=(k(n)?A(n):i(t.endTrigger)-j(n,t.endTrigger.offsetHeight,e))-a+r+o;l(t.trigger,r),t.inner.style.height=`${Math.max(0,s)}px`}),t()}#x(e){if(!this.#t.length)return;let t=[];if(this.#t.forEach(e=>{e.wrapper&&(c(e.wrapper),t.push(e))}),!t.length)return;let n=this.#o?i(this.#o):0,r=new Map;t.forEach((e,t)=>{r.set(e.trigger,t)});let a=t.map((t,n)=>U(this.#e,e,t,n,r)),s=0,u=()=>B(a,{viewportHeight:e,structureTop:n,documentMaxScroll:s,measureLiveEndTriggerTop:e=>i(t[e].endTrigger),onPlanned:(e,n)=>{let r=t[e];r.freezeStart=n.freezeStart,r.freezeEnd=n.freezeEnd,r.wrapper&&(l(r.wrapper,n.stickyTop),r.kind===`scene`&&r.padding&&n.paddingHeight!==null&&(r.padding.style.height=`${n.paddingHeight}px`))}});u();let d=a.some(e=>e.end.mode===`clause`&&e.end.measureLive),f=a.some(e=>e.end.mode===`max`);f&&(s=o(e)),(d||f)&&u()}refresh(){if(this.#s)return;(this.#a||!this.#i||x(this.#t))&&(this.#p(),this.#m()),this.#v();let e=this.#t.length||this.#u.length?a():0;this.#x(e),this.#b(e),this.#S()}#S(){this.#d.sync(this.#t.filter(e=>e.kind===`scene`).map(({trigger:e,freezeStart:t,freezeEnd:n})=>({trigger:e,freezeStart:t,freezeEnd:n})),this.#o,this.#f)}#C(e,t,n){if(W(e,t))throw Error(n)}#w(e,t,n){if(this.#s)throw Error(`StickyScrollTrigger: cannot register a new layer after destroy() has been called.`);return this.#C(this.#t,e.trigger,`StickyScrollTrigger: ${r(e.trigger)} is already registered as a trigger for another layer. Each element can only be used once across createStickyTrigger/createOverlapScroll calls in the same controller instance (reusing it makes endTrigger resolution ambiguous).`),this.#C(this.#u,e.trigger,`StickyScrollTrigger: ${r(e.trigger)} is already registered as a pin trigger (createStickyPin). Using the same element as both a Scene/Cover trigger and a pin trigger wraps it twice and produces conflicting position:sticky behavior.`),this.#t.push(e),this.#a=!0,{...n,trigger:e.trigger,start:()=>e.freezeStart,end:()=>e.freezeEnd||.001,invalidateOnRefresh:n.invalidateOnRefresh??!0,onKill:n=>{this.#g(n);let r=this.#t.indexOf(e);r!==-1&&this.#t.splice(r,1),this.#a=!0,this.#h(),t?.(n)},onRefreshInit:this.#_(this.#t,e,n.onRefreshInit)}}createStickyTrigger({trigger:e,start:r=`0 0`,end:i=`100% 0`,endTrigger:a,onKill:o,...s}){let c=t(e,`createStickyTrigger`),l=n(c,a,`createStickyTrigger`);return this.#w({kind:`scene`,trigger:c,endTrigger:l,container:null,wrapper:null,padding:null,start:r,end:i,freezeStart:0,freezeEnd:0},o,s)}createOverlapScroll({trigger:e,cover:i,start:a=`bottom bottom`,end:o=null,endTrigger:s,onKill:c,...l}){let u=t(e,`createOverlapScroll`),d=n(u,s,`createOverlapScroll`),f=i===void 0?u.nextElementSibling:t(i,`createOverlapScroll`);if(!(f instanceof HTMLElement))throw Error(`createOverlapScroll: cover element (trigger.nextElementSibling) not found for trigger ${r(u)}`);if(f.parentNode!==u.parentNode)throw Error(`createOverlapScroll: cover ${r(f)} must be a sibling of trigger `+r(u));let p={kind:`cover`,trigger:u,endTrigger:d,cover:f,wrapper:null,start:a,end:o,freezeStart:0,freezeEnd:0},m=this.#w(p,e=>{h(),c?.(e)},l),h=g(f);return this.#r.set(p,h),m}createStickyPin({trigger:e,top:n=0,endTrigger:i,end:a=`top top`,onKill:o,...s}){if(this.#s)throw Error(`StickyScrollTrigger: cannot register a new pin after destroy() has been called.`);let c=t(e,`createStickyPin`),l=t(i,`createStickyPin`);this.#C(this.#u,c,`StickyScrollTrigger: ${r(c)} is already registered as a pin trigger.`),this.#C(this.#t,c,`StickyScrollTrigger: ${r(c)} is already registered as a trigger for another layer (createStickyTrigger/createOverlapScroll). Using the same element as both a Scene/Cover trigger and a pin trigger wraps it twice and produces conflicting position:sticky behavior.`);let u={trigger:c,outer:null,inner:null,top:n,endTrigger:l,end:a};return this.#u.push(u),{...s,trigger:c,onKill:e=>{this.#g(e);let t=this.#u.indexOf(u);t!==-1&&this.#u.splice(t,1),u.outer&&(m(u.outer,u.trigger),u.outer=null,u.inner=null),o?.(e)},onRefreshInit:this.#_(this.#u,u,s.onRefreshInit)}}resolveScrollPosition(e,n){let r=t(e,`resolveScrollPosition`),c=w(n),l=a();if(I(c))return o(l)+L(c,l);if(k(c))return A(c);let u=0;this.#t.forEach(e=>{e.kind===`scene`&&(s(e.trigger,r)>=0||(u+=e.freezeEnd-e.freezeStart))});let d=j(c,r.offsetHeight,l),f=this.#y(),p=i(r)+u-d;return f(),p}static getScrollTop(e,n){let r=t(e,`StickyScrollTrigger.getScrollTop`),a=n.find(e=>e.#e.contains(r));return a?a.resolveScrollPosition(r,`top top`):i(r)}createResolvedTrigger({trigger:e,start:r,end:i,endTrigger:a,...o}){let s=t(e,`createResolvedTrigger`),c=n(s,a,`createResolvedTrigger`);return{...o,trigger:s,start:()=>this.resolveScrollPosition(s,r),end:()=>this.resolveScrollPosition(c,i)}}destroy(){this.#s||(this.#s=!0,this.#d.restore(),this.#t.forEach(e=>{e.kind===`cover`&&this.#r.get(e)?.()}),this.#p(),this.#t.length=0,this.#n=[],this.#u.forEach(e=>{e.outer&&m(e.outer,e.trigger)}),this.#u.length=0)}}})();
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @license
|
|
3
|
-
* StickyScrollTrigger v0.
|
|
3
|
+
* StickyScrollTrigger v0.3.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
|
|
@@ -153,6 +153,108 @@ const liftAboveStickyWrapper = (cover) => {
|
|
|
153
153
|
lifted.length = 0;
|
|
154
154
|
};
|
|
155
155
|
};
|
|
156
|
+
const OFFSET_PROPERTY = "--sst-scroll-margin-top-offset";
|
|
157
|
+
let nextInstanceSuffix = 0;
|
|
158
|
+
const supportsScrollDrivenAnimations = typeof CSS !== "undefined" && typeof CSS.supports === "function" && CSS.supports("animation-timeline", "scroll()");
|
|
159
|
+
const buildStylesheet = (instanceId, scenes) => {
|
|
160
|
+
const declarations = [];
|
|
161
|
+
const animations = [];
|
|
162
|
+
const timelines = [];
|
|
163
|
+
const ranges = [];
|
|
164
|
+
scenes.forEach((scene, index) => {
|
|
165
|
+
const property = `--${instanceId}-c${index}`;
|
|
166
|
+
const keyframes = `${instanceId}-k${index}`;
|
|
167
|
+
declarations.push(`@property ${property}{syntax:"<length>";inherits:true;initial-value:0px}`, `@keyframes ${keyframes}{from{${property}:0px}to{${property}:${scene.freezeEnd - scene.freezeStart}px}}`);
|
|
168
|
+
animations.push(`${keyframes} linear both`);
|
|
169
|
+
timelines.push("scroll(root block)");
|
|
170
|
+
ranges.push(`${scene.freezeStart}px ${scene.freezeEnd}px`);
|
|
171
|
+
});
|
|
172
|
+
declarations.push(`@supports (animation-timeline: scroll()) {[data-${instanceId}]{animation:${animations.join(",")};animation-timeline:${timelines.join(",")};animation-range:${ranges.join(",")}}}`);
|
|
173
|
+
return declarations.join("");
|
|
174
|
+
};
|
|
175
|
+
const createScrollMarginSync = (rootElement) => {
|
|
176
|
+
const instanceId = `sst${nextInstanceSuffix++}`;
|
|
177
|
+
const snapshots = new WeakMap();
|
|
178
|
+
let written = new Set();
|
|
179
|
+
let styleElement = null;
|
|
180
|
+
let animationHost = null;
|
|
181
|
+
let jsRamps = [];
|
|
182
|
+
let jsHost = null;
|
|
183
|
+
const applyJsRamp = () => {
|
|
184
|
+
if (!jsHost) return;
|
|
185
|
+
const scroll = window.scrollY;
|
|
186
|
+
jsRamps.forEach((scene, index) => {
|
|
187
|
+
const consumed = Math.min(Math.max(scroll - scene.freezeStart, 0), scene.freezeEnd - scene.freezeStart);
|
|
188
|
+
jsHost.style.setProperty(`--${instanceId}-c${index}`, `${consumed}px`);
|
|
189
|
+
});
|
|
190
|
+
};
|
|
191
|
+
if (!supportsScrollDrivenAnimations) window.addEventListener("scroll", applyJsRamp, { passive: true });
|
|
192
|
+
const restoreTarget = (target) => {
|
|
193
|
+
const snapshot = snapshots.get(target);
|
|
194
|
+
target.style.scrollMarginTop = snapshot ? snapshot.inline : "";
|
|
195
|
+
};
|
|
196
|
+
const clearAnimation = () => {
|
|
197
|
+
styleElement?.remove();
|
|
198
|
+
styleElement = null;
|
|
199
|
+
animationHost?.removeAttribute(`data-${instanceId}`);
|
|
200
|
+
animationHost = null;
|
|
201
|
+
jsRamps = [];
|
|
202
|
+
jsHost = null;
|
|
203
|
+
};
|
|
204
|
+
return {
|
|
205
|
+
sync(scenes, host, targetSelector) {
|
|
206
|
+
const ramps = scenes.filter((scene) => scene.freezeEnd > scene.freezeStart);
|
|
207
|
+
if (targetSelector === null || !ramps.length || !host) {
|
|
208
|
+
written.forEach(restoreTarget);
|
|
209
|
+
written = new Set();
|
|
210
|
+
clearAnimation();
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
if (animationHost !== host) {
|
|
214
|
+
animationHost?.removeAttribute(`data-${instanceId}`);
|
|
215
|
+
animationHost = host;
|
|
216
|
+
animationHost.setAttribute(`data-${instanceId}`, "");
|
|
217
|
+
}
|
|
218
|
+
if (!supportsScrollDrivenAnimations) {
|
|
219
|
+
jsRamps = ramps;
|
|
220
|
+
jsHost = host;
|
|
221
|
+
applyJsRamp();
|
|
222
|
+
}
|
|
223
|
+
if (!styleElement) {
|
|
224
|
+
styleElement = document.createElement("style");
|
|
225
|
+
document.head.appendChild(styleElement);
|
|
226
|
+
}
|
|
227
|
+
styleElement.textContent = buildStylesheet(instanceId, ramps);
|
|
228
|
+
const targets = Array.from(rootElement.querySelectorAll(targetSelector));
|
|
229
|
+
targets.forEach((target) => {
|
|
230
|
+
const existing = snapshots.get(target);
|
|
231
|
+
if (!existing) snapshots.set(target, { inline: target.style.scrollMarginTop });
|
|
232
|
+
else if (written.has(target)) target.style.scrollMarginTop = existing.inline;
|
|
233
|
+
});
|
|
234
|
+
const consumed = ramps.map((_, index) => `var(--${instanceId}-c${index}, 0px)`).join(" + ");
|
|
235
|
+
targets.forEach((target) => {
|
|
236
|
+
const authorPx = parseFloat(getComputedStyle(target).scrollMarginTop) || 0;
|
|
237
|
+
let lag = 0;
|
|
238
|
+
ramps.forEach((scene) => {
|
|
239
|
+
if (compareDocumentOrder(scene.trigger, target) >= 0) return;
|
|
240
|
+
lag += scene.freezeEnd - scene.freezeStart;
|
|
241
|
+
});
|
|
242
|
+
target.style.scrollMarginTop = `calc(${authorPx}px + var(${OFFSET_PROPERTY}, 0px) + ${consumed} - ${lag}px)`;
|
|
243
|
+
});
|
|
244
|
+
const next = new Set(targets);
|
|
245
|
+
written.forEach((target) => {
|
|
246
|
+
if (!next.has(target)) restoreTarget(target);
|
|
247
|
+
});
|
|
248
|
+
written = next;
|
|
249
|
+
},
|
|
250
|
+
restore() {
|
|
251
|
+
if (!supportsScrollDrivenAnimations) window.removeEventListener("scroll", applyJsRamp);
|
|
252
|
+
written.forEach(restoreTarget);
|
|
253
|
+
written = new Set();
|
|
254
|
+
clearAnimation();
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
};
|
|
156
258
|
const isDomOrderStale = (layers) => {
|
|
157
259
|
for (let i = 1; i < layers.length; i += 1) if (compareDocumentOrder(layers[i - 1].trigger, layers[i].trigger) > 0) return true;
|
|
158
260
|
return false;
|
|
@@ -419,8 +521,12 @@ var StickyScrollTrigger = class {
|
|
|
419
521
|
#rebuildScheduled = false;
|
|
420
522
|
#scrollTriggerClass = null;
|
|
421
523
|
#pinLayers = [];
|
|
422
|
-
|
|
524
|
+
#scrollMarginSync;
|
|
525
|
+
#scrollMarginTargets;
|
|
526
|
+
constructor(root, options = {}) {
|
|
423
527
|
this.#rootElement = resolveRoot(root);
|
|
528
|
+
this.#scrollMarginSync = createScrollMarginSync(this.#rootElement);
|
|
529
|
+
this.#scrollMarginTargets = options.scrollMarginTargets === void 0 ? "[id]" : options.scrollMarginTargets;
|
|
424
530
|
}
|
|
425
531
|
#unbuild() {
|
|
426
532
|
if (!this.#built) return;
|
|
@@ -542,6 +648,14 @@ var StickyScrollTrigger = class {
|
|
|
542
648
|
const viewportHeight = this.#layers.length || this.#pinLayers.length ? measureViewportHeight() : 0;
|
|
543
649
|
this.#refreshScenesAndCovers(viewportHeight);
|
|
544
650
|
this.#refreshPins(viewportHeight);
|
|
651
|
+
this.#syncScrollMargins();
|
|
652
|
+
}
|
|
653
|
+
#syncScrollMargins() {
|
|
654
|
+
this.#scrollMarginSync.sync(this.#layers.filter((layer) => layer.kind === "scene").map(({ trigger, freezeStart, freezeEnd }) => ({
|
|
655
|
+
trigger,
|
|
656
|
+
freezeStart,
|
|
657
|
+
freezeEnd
|
|
658
|
+
})), this.#outermostContainer, this.#scrollMarginTargets);
|
|
545
659
|
}
|
|
546
660
|
#assertTriggerAvailable(list, trigger, message) {
|
|
547
661
|
if (hasDuplicateTrigger(list, trigger)) throw new Error(message);
|
|
@@ -679,6 +793,7 @@ var StickyScrollTrigger = class {
|
|
|
679
793
|
destroy() {
|
|
680
794
|
if (this.#destroyed) return;
|
|
681
795
|
this.#destroyed = true;
|
|
796
|
+
this.#scrollMarginSync.restore();
|
|
682
797
|
this.#layers.forEach((layer) => {
|
|
683
798
|
if (layer.kind === "cover") this.#coverRestoreByLayer.get(layer)?.();
|
|
684
799
|
});
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @license
|
|
3
|
-
* StickyScrollTrigger v0.
|
|
3
|
+
* StickyScrollTrigger v0.3.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}},_=e=>{for(let t=1;t<e.length;t+=1)if(s(e[t-1].trigger,e[t].trigger)>0)return!0;return!1},v=(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},y=(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)})},b=e=>typeof e==`function`?e():e,x={top:0,center:.5,bottom:1},S=(e,t,n,r)=>{let i=n===`%`?parseFloat(t)/100*r:parseFloat(t);return e?.startsWith(`-`)?-i:i},C=/^(top|center|bottom|(?:\d+\.?\d*|\.\d+))?(%|px)?(?:([+-]=?)(\d+\.?\d*|\.\d+)(%|px)?)?$/,w=(e,t)=>{let n=C.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 x,l=i===`%`;return{fraction:c?x[r]:l?parseFloat(r)/100:0,offsetPx:(!c&&!l&&r!==void 0?parseFloat(r):0)+(a?S(a,o,s,t):0)}},T=e=>typeof e==`number`||e.trim()!==``&&!Number.isNaN(Number(e)),E=e=>typeof e==`number`?e:Number(e),D=(e,t,n)=>{let[r,i=`top`]=e.trim().split(/\s+/),a=w(r,t),o=w(i,n);return n*o.fraction+o.offsetPx-(t*a.fraction+a.offsetPx)},O=/^\+=(\d+\.?\d*|\.\d+)(%|px)?$/,k=e=>typeof e==`string`&&O.test(e.trim()),A=(e,t)=>{let n=O.exec(String(e).trim());if(!n)throw Error(`StickyScrollTrigger: unsupported end format "${e}"`);let[,r,i]=n;return S(void 0,r,i,t)},j=/^max(?:([+-]=)(\d+\.?\d*|\.\d+)(%|px)?)?$/,M=e=>typeof e==`string`&&j.test(e.trim()),N=(e,t)=>{let n=j.exec(e.trim());if(!n)throw Error(`StickyScrollTrigger: unsupported max-position format "${e}"`);let[,r,i,a]=n;return r?S(r,i,a,t):0},P=(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},F=(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+P(e,s,a,l.end.rawTop,u):t}let r=D(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}},I=(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}=F(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=F(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},L=(e,t,n,a,o,s)=>{if(a===null)return{mode:`auto`};if(k(a))return{mode:`dwell`,distancePx:Math.max(0,A(a,t))};if(T(a))return{mode:`absolute`,value:E(a)};if(M(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:N(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}},R=(e,t,n,i)=>{if(!T(t))return{mode:`clause`,anchorOffset:D(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:E(t)}},z=(e,t,n,r,a)=>{let o=b(n.start),s=n.end===null?null:b(n.end),c=n.trigger.offsetHeight,l=n.endTrigger===n.trigger,u=a.get(n.endTrigger)??null,d=L(e,t,n,s,r,u),f=R(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}},B=(e,t)=>e.some(e=>e.trigger===t);var V=class{#e;#t=[];#n=[];#r=new WeakMap;#i=!1;#a=!1;#o=null;#s=!1;#c=!1;#l=null;#u=[];constructor(t){this.#e=e(t)}#d(){this.#i&&=(y(this.#e,this.#n,this.#o),this.#o=null,!1)}#f(){this.#o=v(this.#e,this.#t),this.#n=[...this.#t],this.#i=!0,this.#a=!1}#p(){this.#c||this.#s||(this.#c=!0,queueMicrotask(()=>{this.#c=!1,!this.#s&&(this.refresh(),typeof this.#l?.refresh==`function`&&this.#l.refresh())}))}#m(e){this.#l=e.constructor}#h(e,t,n){return r=>{this.#m(r),e[0]===t&&this.refresh(),n?.(r)}}#g(){this.#u.forEach(e=>{if(e.outer)return;let t=p(e.trigger);e.outer=t.outer,e.inner=t.inner})}#_(){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})}}#v(e){if(!this.#u.length)return;let t=this.#_();this.#u.forEach(e=>{c(e.trigger),e.inner&&(e.inner.style.height=``)}),this.#u.forEach(t=>{if(!t.inner)return;let n=b(t.end);if(M(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=b(t.top),a=i(t.trigger),o=t.trigger.offsetHeight,s=(T(n)?E(n):i(t.endTrigger)-D(n,t.endTrigger.offsetHeight,e))-a+r+o;l(t.trigger,r),t.inner.style.height=`${Math.max(0,s)}px`}),t()}#y(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)=>z(this.#e,e,t,n,r)),s=0,u=()=>I(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||_(this.#t))&&(this.#d(),this.#f()),this.#g();let e=this.#t.length||this.#u.length?a():0;this.#y(e),this.#v(e)}#b(e,t,n){if(B(e,t))throw Error(n)}#x(e,t,n){if(this.#s)throw Error(`StickyScrollTrigger: cannot register a new layer after destroy() has been called.`);return this.#b(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.#b(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.#m(n);let r=this.#t.indexOf(e);r!==-1&&this.#t.splice(r,1),this.#a=!0,this.#p(),t?.(n)},onRefreshInit:this.#h(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.#x({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.#x(p,e=>{h(),c?.(e)},l),h=g(f);return this.#r.set(p,h),m}createStickyPin({trigger:e,top:n=0,endTrigger:i,end:a=`top top`,onKill:o,...s}){if(this.#s)throw Error(`StickyScrollTrigger: cannot register a new pin after destroy() has been called.`);let c=t(e,`createStickyPin`),l=t(i,`createStickyPin`);this.#b(this.#u,c,`StickyScrollTrigger: ${r(c)} is already registered as a pin trigger.`),this.#b(this.#t,c,`StickyScrollTrigger: ${r(c)} is already registered as a trigger for another layer (createStickyTrigger/createOverlapScroll). Using the same element as both a Scene/Cover trigger and a pin trigger wraps it twice and produces conflicting position:sticky behavior.`);let u={trigger:c,outer:null,inner:null,top:n,endTrigger:l,end:a};return this.#u.push(u),{...s,trigger:c,onKill:e=>{this.#m(e);let t=this.#u.indexOf(u);t!==-1&&this.#u.splice(t,1),u.outer&&(m(u.outer,u.trigger),u.outer=null,u.inner=null),o?.(e)},onRefreshInit:this.#h(this.#u,u,s.onRefreshInit)}}resolveScrollPosition(e,n){let r=t(e,`resolveScrollPosition`),c=b(n),l=a();if(M(c))return o(l)+N(c,l);if(T(c))return E(c);let u=0;this.#t.forEach(e=>{e.kind===`scene`&&(s(e.trigger,r)>=0||(u+=e.freezeEnd-e.freezeStart))});let d=D(c,r.offsetHeight,l),f=this.#_(),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.#t.forEach(e=>{e.kind===`cover`&&this.#r.get(e)?.()}),this.#d(),this.#t.length=0,this.#n=[],this.#u.forEach(e=>{e.outer&&m(e.outer,e.trigger)}),this.#u.length=0)}};export{V as default};
|
|
8
|
+
const e=e=>{let t=typeof e==`string`?document.querySelector(e):e;if(!t){let t=typeof e==`string`?`"${e}"`:String(e);throw Error(`StickyScrollTrigger: root ${t} not found`)}return t},t=(e,t)=>{let n=typeof e==`string`?document.querySelector(e):e;if(!n){let n=typeof e==`string`?`"${e}"`:String(e);throw Error(`${t}: element ${n} not found`)}return n},n=(e,n,r)=>n===void 0?e:t(n,r),r=e=>{let t=e.id?`#${e.id}`:``,n=e.classList.length>0?`.${Array.from(e.classList).join(`.`)}`:``;return`<${e.tagName.toLowerCase()}${t}${n}>`},i=e=>{let t=0,n=e;for(;n;){t+=n.offsetTop;let e=n.offsetParent;e&&(t+=e.clientTop),n=e}return t},a=()=>{let e=document.createElement(`div`);e.style.height=`100vh`,e.style.position=`absolute`,document.body.appendChild(e);let t=e.offsetHeight||window.innerHeight;return e.remove(),t},o=e=>{let t=document.documentElement;return Math.max(0,(t.scrollHeight||document.body.scrollHeight)-e)},s=(e,t)=>{let n=e.compareDocumentPosition(t);return n&Node.DOCUMENT_POSITION_FOLLOWING?-1:n&Node.DOCUMENT_POSITION_PRECEDING?1:0},c=e=>{e.style.position=``,e.style.top=``},l=(e,t)=>{e.style.position=`sticky`,e.style.top=`${t}px`},u=e=>{if(!e.parentNode)throw Error(`StickyScrollTrigger: ${r(e)} is not attached to the document`);let t=document.createElement(`div`),n=document.createElement(`div`),i=document.createElement(`div`);return i.setAttribute(`aria-hidden`,`true`),i.style.pointerEvents=`none`,e.parentNode.insertBefore(t,e),t.appendChild(n),n.appendChild(e),t.appendChild(i),{container:t,wrapper:n,padding:i}},d=e=>{let t=e.parentNode;if(!t)throw Error(`createOverlapScroll: trigger ${r(e)} is not attached to the document`);let n=document.createElement(`div`);for(t.insertBefore(n,t.firstChild);n.nextSibling;){let t=n.nextSibling;if(n.appendChild(t),t===e)break}return n},f=e=>{let t=e.parentNode;if(t){for(;e.firstChild;)t.insertBefore(e.firstChild,e);e.remove()}},p=e=>{if(!e.parentNode)throw Error(`createStickyPin: trigger ${r(e)} is not attached to the document`);let t=document.createElement(`div`),n=document.createElement(`div`);return t.style.height=`0`,t.style.contain=`layout`,e.parentNode.insertBefore(t,e),t.appendChild(n),n.appendChild(e),{outer:t,inner:n}},m=(e,t)=>{let n=e.parentNode;c(t),n&&n.insertBefore(t,e),e.remove()},h=new WeakMap,g=e=>{let t=[];for(let n=e;n;n=n.nextElementSibling){if(!(n instanceof HTMLElement))continue;let e=h.get(n);if(e)e.count+=1;else{let e=getComputedStyle(n),t={count:1,position:n.style.position,zIndex:n.style.zIndex};e.position===`static`&&(n.style.position=`relative`),e.zIndex===`auto`&&(n.style.zIndex=`1`),h.set(n,t)}t.push(n)}return()=>{t.forEach(e=>{let t=h.get(e);t&&(--t.count,!(t.count>0)&&(e.style.position=t.position,e.style.zIndex=t.zIndex,h.delete(e)))}),t.length=0}};let _=0;const v=typeof CSS<`u`&&typeof CSS.supports==`function`&&CSS.supports(`animation-timeline`,`scroll()`),y=(e,t)=>{let n=[],r=[],i=[],a=[];return t.forEach((t,o)=>{let s=`--${e}-c${o}`,c=`${e}-k${o}`;n.push(`@property ${s}{syntax:"<length>";inherits:true;initial-value:0px}`,`@keyframes ${c}{from{${s}:0px}to{${s}:${t.freezeEnd-t.freezeStart}px}}`),r.push(`${c} linear both`),i.push(`scroll(root block)`),a.push(`${t.freezeStart}px ${t.freezeEnd}px`)}),n.push(`@supports (animation-timeline: scroll()) {[data-${e}]{animation:${r.join(`,`)};animation-timeline:${i.join(`,`)};animation-range:${a.join(`,`)}}}`),n.join(``)},b=e=>{let t=`sst${_++}`,n=new WeakMap,r=new Set,i=null,a=null,o=[],c=null,l=()=>{if(!c)return;let e=window.scrollY;o.forEach((n,r)=>{let i=Math.min(Math.max(e-n.freezeStart,0),n.freezeEnd-n.freezeStart);c.style.setProperty(`--${t}-c${r}`,`${i}px`)})};v||window.addEventListener(`scroll`,l,{passive:!0});let u=e=>{let t=n.get(e);e.style.scrollMarginTop=t?t.inline:``},d=()=>{i?.remove(),i=null,a?.removeAttribute(`data-${t}`),a=null,o=[],c=null};return{sync(f,p,m){let h=f.filter(e=>e.freezeEnd>e.freezeStart);if(m===null||!h.length||!p){r.forEach(u),r=new Set,d();return}a!==p&&(a?.removeAttribute(`data-${t}`),a=p,a.setAttribute(`data-${t}`,``)),v||(o=h,c=p,l()),i||(i=document.createElement(`style`),document.head.appendChild(i)),i.textContent=y(t,h);let g=Array.from(e.querySelectorAll(m));g.forEach(e=>{let t=n.get(e);t?r.has(e)&&(e.style.scrollMarginTop=t.inline):n.set(e,{inline:e.style.scrollMarginTop})});let _=h.map((e,n)=>`var(--${t}-c${n}, 0px)`).join(` + `);g.forEach(e=>{let t=parseFloat(getComputedStyle(e).scrollMarginTop)||0,n=0;h.forEach(t=>{s(t.trigger,e)>=0||(n+=t.freezeEnd-t.freezeStart)}),e.style.scrollMarginTop=`calc(${t}px + var(--sst-scroll-margin-top-offset, 0px) + ${_} - ${n}px)`});let b=new Set(g);r.forEach(e=>{b.has(e)||u(e)}),r=b},restore(){v||window.removeEventListener(`scroll`,l),r.forEach(u),r=new Set,d()}}},x=e=>{for(let t=1;t<e.length;t+=1)if(s(e[t-1].trigger,e[t].trigger)>0)return!0;return!1},S=(e,t)=>{t.sort((e,t)=>s(e.trigger,t.trigger));for(let e of t)e.kind===`cover`&&(e.wrapper=d(e.trigger));let n=e,r=null;for(let e of t){if(e.kind!==`scene`)continue;let t=u(n);e.container=t.container,e.wrapper=t.wrapper,e.padding=t.padding,n=t.container,r=t.container}return r},C=(e,t,n)=>{n&&(n.parentNode?.insertBefore(e,n),n.remove());for(let e=t.length-1;e>=0;--e){let n=t[e];n.kind===`cover`&&n.wrapper&&f(n.wrapper)}t.forEach(e=>{e.wrapper=null,e.kind===`scene`&&(e.container=null,e.padding=null)})},w=e=>typeof e==`function`?e():e,T={top:0,center:.5,bottom:1},E=(e,t,n,r)=>{let i=n===`%`?parseFloat(t)/100*r:parseFloat(t);return e?.startsWith(`-`)?-i:i},D=/^(top|center|bottom|(?:\d+\.?\d*|\.\d+))?(%|px)?(?:([+-]=?)(\d+\.?\d*|\.\d+)(%|px)?)?$/,O=(e,t)=>{let n=D.exec(e);if(!n||!n[1]&&!n[3])throw Error(`StickyScrollTrigger: unsupported position clause "${e}"`);let[,r,i,a,o,s]=n;if(r!==void 0&&a&&!a.includes(`=`)){let t=`${r}${i??``}${a}=${o}${s??``}`;throw Error(`StickyScrollTrigger: unsupported position clause "${e}": a signed offset needs an explicit '=' when it follows a keyword or number (GSAP itself requires this too, to tell "${r}" and the offset apart). Did you mean "${t}"?`)}let c=r!==void 0&&r in T,l=i===`%`;return{fraction:c?T[r]:l?parseFloat(r)/100:0,offsetPx:(!c&&!l&&r!==void 0?parseFloat(r):0)+(a?E(a,o,s,t):0)}},k=e=>typeof e==`number`||e.trim()!==``&&!Number.isNaN(Number(e)),A=e=>typeof e==`number`?e:Number(e),j=(e,t,n)=>{let[r,i=`top`]=e.trim().split(/\s+/),a=O(r,t),o=O(i,n);return n*o.fraction+o.offsetPx-(t*a.fraction+a.offsetPx)},M=/^\+=(\d+\.?\d*|\.\d+)(%|px)?$/,N=e=>typeof e==`string`&&M.test(e.trim()),P=(e,t)=>{let n=M.exec(String(e).trim());if(!n)throw Error(`StickyScrollTrigger: unsupported end format "${e}"`);let[,r,i]=n;return E(void 0,r,i,t)},F=/^max(?:([+-]=)(\d+\.?\d*|\.\d+)(%|px)?)?$/,I=e=>typeof e==`string`&&F.test(e.trim()),L=(e,t)=>{let n=F.exec(e.trim());if(!n)throw Error(`StickyScrollTrigger: unsupported max-position format "${e}"`);let[,r,i,a]=n;return r?E(r,i,a,t):0},R=(e,t,n,r,i)=>{let a=0;return e.forEach((e,o)=>{if(o===i||e.triggerTop>=r)return;let s=t[o]===void 0?n?n.paddings[o]:null:t[o];s!=null&&(a+=s)}),a},z=(e,{viewportHeight:t,structureTop:n,documentMaxScroll:r,measureLiveEndTriggerTop:i},a)=>{let o=[],s=[],c=0;return{plans:e.map((l,u)=>{let d=l.triggerTop+c,f=l.start.mode===`absolute`?l.start.value:d-l.start.anchorOffset,p;switch(l.end.mode){case`auto`:if(l.start.mode!==`clause`)throw Error(`StickyScrollTrigger: internal error: an absolute start reached 'auto' end mode, which resolveStartSpec should have already rejected.`);p=f+Math.max(0,l.start.anchorOffset+(l.coverTop-l.triggerTop));break;case`dwell`:p=f+l.end.distancePx;break;case`absolute`:p=Math.max(f,l.end.value);break;case`max`:p=Math.max(f,r+l.end.offsetPx);break;case`clause`:{let n;if(l.endTriggerIsSelf)n=d;else{let t=l.endTriggerIndex===null?void 0:o[l.endTriggerIndex]===void 0?a?a.naturalTops[l.endTriggerIndex]:void 0:o[l.endTriggerIndex];n=t===void 0?l.end.measureLive?i(u):l.end.rawTop===null?0:l.end.rawTop+R(e,s,a,l.end.rawTop,u):t}let r=j(l.end.clause,l.endTriggerHeight,t);p=Math.max(f,n-r);break}}let m;if(l.kind===`cover`){if(l.start.mode!==`clause`)throw Error(`StickyScrollTrigger: internal error: a cover layer measurement carries an absolute start, which resolveStartSpec should have already rejected.`);m={freezeStart:f,freezeEnd:p,stickyTop:l.start.anchorOffset-(l.triggerTop-l.wrapperTop),paddingHeight:null}}else m={freezeStart:f,freezeEnd:p,stickyTop:n-f,paddingHeight:Math.max(0,p-f)};return m.paddingHeight!==null&&(c+=m.paddingHeight),o[u]=d,s[u]=m.paddingHeight,m}),naturalTops:o}},B=(e,t)=>{let n=e.some((e,t)=>e.end.mode!==`clause`||e.endTriggerIsSelf?!1:e.endTriggerIndex===null?e.end.rawTop!==null:e.endTriggerIndex>=t),r=new Map,i={...t,measureLiveEndTriggerTop:e=>{let n=r.get(e);if(n!==void 0)return n;let i=t.measureLiveEndTriggerTop(e);return r.set(e,i),i}},{plans:a,naturalTops:o}=z(e,i,null);if(n)for(let t=0;t<e.length;t+=1){let n={paddings:a.map(e=>e.paddingHeight),naturalTops:o},r=z(e,i,n),s=r.plans.every((e,t)=>e.paddingHeight===a[t].paddingHeight);if(a=r.plans,o=r.naturalTops,s)break;if(t===e.length-1)throw Error(`StickyScrollTrigger: could not resolve endTrigger positions: some Scene layers' endTrigger references form a circular structural dependency (either two or more triggers reference each other's endTrigger, or an unregistered endTrigger's dwell depends on a layer whose own end depends back on it). Point each endTrigger at a layer that doesn't depend on it.`)}return a.forEach((e,n)=>t.onPlanned(n,e)),a},V=(e,t,n,a,o,s)=>{if(a===null)return{mode:`auto`};if(N(a))return{mode:`dwell`,distancePx:Math.max(0,P(a,t))};if(k(a))return{mode:`absolute`,value:A(a)};if(I(a)){if(n.kind!==`cover`)throw Error(`StickyScrollTrigger: createStickyTrigger's trigger ${r(n.trigger)} has end "${a}", which uses GSAP's 'max' keyword. This isn't supported here (a Scene layer's own dwell padding contributes to the document's max scroll position, so the freeze window would depend on itself and never converge). Use createOverlapScroll instead (cover layers never add padding), or a dwell distance such as '+=500'.`);return{mode:`max`,offsetPx:L(a,t)}}if(n.kind!==`cover`&&s!==null&&s>o)throw Error(`StickyScrollTrigger: createStickyTrigger's trigger ${r(n.trigger)}'s endTrigger (${r(n.endTrigger)}) refers to a layer positioned later in DOM order. This isn't supported (the layer's own dwell padding always precedes and pushes down that later layer's position, so the freeze window would depend on its own dwell and never converge). Point endTrigger at a layer registered earlier in DOM order, or use a dwell distance such as '+=500' instead.`);let c=s===null,l=e.contains(n.endTrigger);if(c&&!l&&n.kind!==`cover`)throw Error(`StickyScrollTrigger: createStickyTrigger's trigger ${r(n.trigger)} has an endTrigger (${r(n.endTrigger)}) outside the shared container (the layer's own dwell would push it away, so the freeze window never converges). Point endTrigger at an element inside the container, or use a dwell distance such as '+=500'.`);return{mode:`clause`,clause:a,rawTop:c&&l?i(n.endTrigger):null,measureLive:c&&!l}},H=(e,t,n,i)=>{if(!k(t))return{mode:`clause`,anchorOffset:j(t,n,i)};if(e.kind===`cover`)throw Error(`StickyScrollTrigger: createOverlapScroll's trigger ${r(e.trigger)} has start "${t}", an absolute scroll position (GSAP treats any bare-number start this way). This isn't supported here: a cover layer's sticky position is always computed relative to its own wrapper, which needs start to be a position clause (e.g. 'bottom bottom'), not an absolute scroll position.`);return{mode:`absolute`,value:A(t)}},U=(e,t,n,r,a)=>{let o=w(n.start),s=n.end===null?null:w(n.end),c=n.trigger.offsetHeight,l=n.endTrigger===n.trigger,u=a.get(n.endTrigger)??null,d=V(e,t,n,s,r,u),f=H(n,o,c,t);return{kind:n.kind,start:f,triggerTop:i(n.trigger),wrapperTop:n.wrapper?i(n.wrapper):0,coverTop:n.kind===`cover`?i(n.cover):0,end:d,endTriggerIsSelf:l,endTriggerIndex:u,endTriggerHeight:d.mode===`clause`?n.endTrigger.offsetHeight:0}},W=(e,t)=>e.some(e=>e.trigger===t);var G=class{#e;#t=[];#n=[];#r=new WeakMap;#i=!1;#a=!1;#o=null;#s=!1;#c=!1;#l=null;#u=[];#d;#f;constructor(t,n={}){this.#e=e(t),this.#d=b(this.#e),this.#f=n.scrollMarginTargets===void 0?`[id]`:n.scrollMarginTargets}#p(){this.#i&&=(C(this.#e,this.#n,this.#o),this.#o=null,!1)}#m(){this.#o=S(this.#e,this.#t),this.#n=[...this.#t],this.#i=!0,this.#a=!1}#h(){this.#c||this.#s||(this.#c=!0,queueMicrotask(()=>{this.#c=!1,!this.#s&&(this.refresh(),typeof this.#l?.refresh==`function`&&this.#l.refresh())}))}#g(e){this.#l=e.constructor}#_(e,t,n){return r=>{this.#g(r),e[0]===t&&this.refresh(),n?.(r)}}#v(){this.#u.forEach(e=>{if(e.outer)return;let t=p(e.trigger);e.outer=t.outer,e.inner=t.inner})}#y(){let e=this.#t.map(e=>e.wrapper).filter(e=>e!==null).map(e=>({wrapper:e,position:e.style.position,top:e.style.top}));return e.forEach(({wrapper:e})=>c(e)),()=>{e.forEach(({wrapper:e,position:t,top:n})=>{e.style.position=t,e.style.top=n})}}#b(e){if(!this.#u.length)return;let t=this.#y();this.#u.forEach(e=>{c(e.trigger),e.inner&&(e.inner.style.height=``)}),this.#u.forEach(t=>{if(!t.inner)return;let n=w(t.end);if(I(n))throw Error(`StickyScrollTrigger: createStickyPin's end "${n}" uses GSAP's 'max' keyword, which isn't supported here (the pin's own spacer height contributes to the document's max scroll position, so the spacer height would depend on itself and never converge). Use a plain position clause such as 'top top' against endTrigger instead.`);let r=w(t.top),a=i(t.trigger),o=t.trigger.offsetHeight,s=(k(n)?A(n):i(t.endTrigger)-j(n,t.endTrigger.offsetHeight,e))-a+r+o;l(t.trigger,r),t.inner.style.height=`${Math.max(0,s)}px`}),t()}#x(e){if(!this.#t.length)return;let t=[];if(this.#t.forEach(e=>{e.wrapper&&(c(e.wrapper),t.push(e))}),!t.length)return;let n=this.#o?i(this.#o):0,r=new Map;t.forEach((e,t)=>{r.set(e.trigger,t)});let a=t.map((t,n)=>U(this.#e,e,t,n,r)),s=0,u=()=>B(a,{viewportHeight:e,structureTop:n,documentMaxScroll:s,measureLiveEndTriggerTop:e=>i(t[e].endTrigger),onPlanned:(e,n)=>{let r=t[e];r.freezeStart=n.freezeStart,r.freezeEnd=n.freezeEnd,r.wrapper&&(l(r.wrapper,n.stickyTop),r.kind===`scene`&&r.padding&&n.paddingHeight!==null&&(r.padding.style.height=`${n.paddingHeight}px`))}});u();let d=a.some(e=>e.end.mode===`clause`&&e.end.measureLive),f=a.some(e=>e.end.mode===`max`);f&&(s=o(e)),(d||f)&&u()}refresh(){if(this.#s)return;(this.#a||!this.#i||x(this.#t))&&(this.#p(),this.#m()),this.#v();let e=this.#t.length||this.#u.length?a():0;this.#x(e),this.#b(e),this.#S()}#S(){this.#d.sync(this.#t.filter(e=>e.kind===`scene`).map(({trigger:e,freezeStart:t,freezeEnd:n})=>({trigger:e,freezeStart:t,freezeEnd:n})),this.#o,this.#f)}#C(e,t,n){if(W(e,t))throw Error(n)}#w(e,t,n){if(this.#s)throw Error(`StickyScrollTrigger: cannot register a new layer after destroy() has been called.`);return this.#C(this.#t,e.trigger,`StickyScrollTrigger: ${r(e.trigger)} is already registered as a trigger for another layer. Each element can only be used once across createStickyTrigger/createOverlapScroll calls in the same controller instance (reusing it makes endTrigger resolution ambiguous).`),this.#C(this.#u,e.trigger,`StickyScrollTrigger: ${r(e.trigger)} is already registered as a pin trigger (createStickyPin). Using the same element as both a Scene/Cover trigger and a pin trigger wraps it twice and produces conflicting position:sticky behavior.`),this.#t.push(e),this.#a=!0,{...n,trigger:e.trigger,start:()=>e.freezeStart,end:()=>e.freezeEnd||.001,invalidateOnRefresh:n.invalidateOnRefresh??!0,onKill:n=>{this.#g(n);let r=this.#t.indexOf(e);r!==-1&&this.#t.splice(r,1),this.#a=!0,this.#h(),t?.(n)},onRefreshInit:this.#_(this.#t,e,n.onRefreshInit)}}createStickyTrigger({trigger:e,start:r=`0 0`,end:i=`100% 0`,endTrigger:a,onKill:o,...s}){let c=t(e,`createStickyTrigger`),l=n(c,a,`createStickyTrigger`);return this.#w({kind:`scene`,trigger:c,endTrigger:l,container:null,wrapper:null,padding:null,start:r,end:i,freezeStart:0,freezeEnd:0},o,s)}createOverlapScroll({trigger:e,cover:i,start:a=`bottom bottom`,end:o=null,endTrigger:s,onKill:c,...l}){let u=t(e,`createOverlapScroll`),d=n(u,s,`createOverlapScroll`),f=i===void 0?u.nextElementSibling:t(i,`createOverlapScroll`);if(!(f instanceof HTMLElement))throw Error(`createOverlapScroll: cover element (trigger.nextElementSibling) not found for trigger ${r(u)}`);if(f.parentNode!==u.parentNode)throw Error(`createOverlapScroll: cover ${r(f)} must be a sibling of trigger `+r(u));let p={kind:`cover`,trigger:u,endTrigger:d,cover:f,wrapper:null,start:a,end:o,freezeStart:0,freezeEnd:0},m=this.#w(p,e=>{h(),c?.(e)},l),h=g(f);return this.#r.set(p,h),m}createStickyPin({trigger:e,top:n=0,endTrigger:i,end:a=`top top`,onKill:o,...s}){if(this.#s)throw Error(`StickyScrollTrigger: cannot register a new pin after destroy() has been called.`);let c=t(e,`createStickyPin`),l=t(i,`createStickyPin`);this.#C(this.#u,c,`StickyScrollTrigger: ${r(c)} is already registered as a pin trigger.`),this.#C(this.#t,c,`StickyScrollTrigger: ${r(c)} is already registered as a trigger for another layer (createStickyTrigger/createOverlapScroll). Using the same element as both a Scene/Cover trigger and a pin trigger wraps it twice and produces conflicting position:sticky behavior.`);let u={trigger:c,outer:null,inner:null,top:n,endTrigger:l,end:a};return this.#u.push(u),{...s,trigger:c,onKill:e=>{this.#g(e);let t=this.#u.indexOf(u);t!==-1&&this.#u.splice(t,1),u.outer&&(m(u.outer,u.trigger),u.outer=null,u.inner=null),o?.(e)},onRefreshInit:this.#_(this.#u,u,s.onRefreshInit)}}resolveScrollPosition(e,n){let r=t(e,`resolveScrollPosition`),c=w(n),l=a();if(I(c))return o(l)+L(c,l);if(k(c))return A(c);let u=0;this.#t.forEach(e=>{e.kind===`scene`&&(s(e.trigger,r)>=0||(u+=e.freezeEnd-e.freezeStart))});let d=j(c,r.offsetHeight,l),f=this.#y(),p=i(r)+u-d;return f(),p}static getScrollTop(e,n){let r=t(e,`StickyScrollTrigger.getScrollTop`),a=n.find(e=>e.#e.contains(r));return a?a.resolveScrollPosition(r,`top top`):i(r)}createResolvedTrigger({trigger:e,start:r,end:i,endTrigger:a,...o}){let s=t(e,`createResolvedTrigger`),c=n(s,a,`createResolvedTrigger`);return{...o,trigger:s,start:()=>this.resolveScrollPosition(s,r),end:()=>this.resolveScrollPosition(c,i)}}destroy(){this.#s||(this.#s=!0,this.#d.restore(),this.#t.forEach(e=>{e.kind===`cover`&&this.#r.get(e)?.()}),this.#p(),this.#t.length=0,this.#n=[],this.#u.forEach(e=>{e.outer&&m(e.outer,e.trigger)}),this.#u.length=0)}};export{G as default};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sticky-scroll-trigger",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A ScrollTrigger helper that uses native CSS position:sticky instead of GSAP pinning",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Kuninori Ogino",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"format:check": "eslint . && prettier --check \"**/*.md\" \"**/*.{json,css}\"",
|
|
44
44
|
"test": "vitest run",
|
|
45
45
|
"test:watch": "vitest",
|
|
46
|
-
"playwright:install": "playwright install chromium webkit",
|
|
46
|
+
"playwright:install": "playwright install chromium webkit firefox",
|
|
47
47
|
"test:e2e": "playwright test --config e2e/playwright.config.ts",
|
|
48
48
|
"prepare": "husky",
|
|
49
49
|
"prepublishOnly": "npm run build:lib"
|
|
@@ -54,15 +54,15 @@
|
|
|
54
54
|
"@playwright/test": "^1.62.1",
|
|
55
55
|
"@stylistic/eslint-plugin": "^5.10.0",
|
|
56
56
|
"@types/node": "^24.13.3",
|
|
57
|
-
"eslint": "^10.8.
|
|
57
|
+
"eslint": "^10.8.1",
|
|
58
58
|
"gsap": "^3.15.0",
|
|
59
59
|
"husky": "^9.1.7",
|
|
60
60
|
"jsdom": "^30.0.1",
|
|
61
61
|
"lint-staged": "^17.3.0",
|
|
62
62
|
"prettier": "^3.9.6",
|
|
63
63
|
"typescript": "^6.0.3",
|
|
64
|
-
"typescript-eslint": "^8.
|
|
65
|
-
"vite": "^8.2.
|
|
64
|
+
"typescript-eslint": "^8.66.0",
|
|
65
|
+
"vite": "^8.2.1",
|
|
66
66
|
"vite-plugin-dts": "^5.0.3",
|
|
67
67
|
"vitest": "^4.1.10"
|
|
68
68
|
},
|