sticky-scroll-trigger 0.10.0 → 0.11.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
|
@@ -40,7 +40,7 @@ npm install gsap sticky-scroll-trigger
|
|
|
40
40
|
For plain `<script>` usage without a bundler, an IIFE build is also published, exposing `window.StickyScrollTrigger`:
|
|
41
41
|
|
|
42
42
|
```html
|
|
43
|
-
<script src="https://cdn.jsdelivr.net/npm/sticky-scroll-trigger@0.
|
|
43
|
+
<script src="https://cdn.jsdelivr.net/npm/sticky-scroll-trigger@0.11.0/dist/StickyScrollTrigger.global.min.js"></script>
|
|
44
44
|
```
|
|
45
45
|
|
|
46
46
|
Consumers should only rely on the exports of `sticky-scroll-trigger` (or `window.StickyScrollTrigger` for the global build). The internal modules under `src/` are not part of the public API.
|
|
@@ -282,7 +282,7 @@ sticky.refresh();
|
|
|
282
282
|
|
|
283
283
|
Here `start` means only where the element sits while pinned, not when pinning begins, as it does for the other methods. Pinning begins the moment `trigger` reaches that position naturally, which is CSS's decision rather than a separate number to set, so a pin's position and the scroll position it engages at are one setting.
|
|
284
284
|
|
|
285
|
-
That's why `start` throws on an [absolute scroll position](#absolute-scroll-position) (`start: 20`, which GSAP reads as scroll position 20) instead of silently treating it as a px distance. `top` is its own option for the same reason: with the bare-number slot reserved for GSAP's meaning, `'top 20px'` would otherwise be a pin's only way to say "20px below the top of the viewport". `end` does take an absolute scroll position (`end: 3000` releases at scroll 3000), since a release point is a real thing to ask for on the scroll axis.
|
|
285
|
+
That's why `start` throws on an [absolute scroll position](#absolute-scroll-position) (`start: 20`, which GSAP reads as scroll position 20) instead of silently treating it as a px distance. `top` is its own option for the same reason: with the bare-number slot reserved for GSAP's meaning, `'top 20px'` would otherwise be a pin's only way to say "20px below the top of the viewport". `end` does take an absolute scroll position (`end: 3000` releases at scroll 3000), since a release point is a real thing to ask for on the scroll axis. If scroll 3000 falls inside a Scene layer's freeze, a pin inside the shared container holds until that freeze ends. Pinning also starts only after the dwell of every scene that freezes first, so the pin can hold for less scroll than GSAP's, or not at all, as with a Scene layer's [absolute `end`](#absolute-scroll-position).
|
|
286
286
|
|
|
287
287
|
Clauses resolve exactly as they do elsewhere, including the one-token forms that name the element's own side: `'top 20px'` puts the element's top 20px below the viewport's top edge, while `'20px'` puts it 20px above.
|
|
288
288
|
|
|
@@ -460,6 +460,10 @@ GSAP treats a value as an absolute scroll position rather than a clause whenever
|
|
|
460
460
|
|
|
461
461
|
`start: '500'` (or `start: 500`) freezes starting at absolute scroll position 500px, ignoring the trigger's natural position and any preceding dwell. `end: '500'` ends the freeze window at 500px the same way, clamped to `start` if that would put `end` before it, matching GSAP's own `end = Math.max(start, ...)`.
|
|
462
462
|
|
|
463
|
+
When an absolute `start` falls inside another Scene layer's freeze window, the reported window (what GSAP gets, and so `self.start`/`self.end` and callbacks) keeps the written `start` and its length, as GSAP's own pins overlap. The page itself can't freeze twice at once, so it freezes only for the part of that window the other scene hasn't already frozen; positions (`resolveScrollPosition`, same-page links, cover layers and pins) follow the page.
|
|
464
|
+
|
|
465
|
+
An absolute `end` can give a Scene layer's window a different length than GSAP's pin. A position-clause `start` waits out the dwell of every scene that freezes first, including one enclosing `trigger`, one inside it, or one later in the DOM, while GSAP's pin starts after the spacers already placed above it. The wait doesn't change the length of a dwell or position-clause `end`, which is measured from that start, but an absolute one stays at the written value, so the window is shorter by however much later it starts than GSAP's, down to zero once the start passes the end. GSAP's `pinnedContainer` also delays the start for an enclosing scene, but moves an absolute `end` along with it. A scene above `trigger` that freezes later doesn't delay the start, which can make the window longer, as GSAP's pins created in another order would give. An absolute `start` doesn't wait, so with an absolute `end` the window keeps the written length.
|
|
466
|
+
|
|
463
467
|
`'500 top'` (two tokens) and `'500px'` (a suffix) don't qualify; they resolve as offsets via the bare-number row in the table above, same as GSAP.
|
|
464
468
|
|
|
465
469
|
> [`createOverlapScroll`](#createoverlapscrolloptions)'s `start` doesn't support absolute scroll positions: a cover layer's sticky position is always computed relative to its own wrapper (see the repository's `ARCHITECTURE.md`), which has no equivalent for one. It throws instead; use a position clause.
|
|
@@ -505,7 +509,7 @@ These rules assume the elements holding a scene's `trigger` grow with its spacer
|
|
|
505
509
|
|
|
506
510
|
Counting an enclosing scene departs from plain GSAP pins, which measure a point inside a pinned element as if the pin weren't there, so whichever of `start` and `end` sits inside it lands early by the pin's duration unless `pinnedContainer` names that element. This module finds the nesting itself and counts an enclosing scene the way `pinnedContainer` does, but decides at each point rather than once at `start`, and for every enclosing scene rather than one. So it also counts a scene that freezes after `start` but before the point `end` names arrives, which `pinnedContainer` leaves out.
|
|
507
511
|
|
|
508
|
-
Some sets of Scene layers
|
|
512
|
+
Some sets of Scene layers never settle on a layout where every `end` agrees with what it counts, such as two nested scenes whose `endTrigger`s each lie past the other's `trigger`. Others settle on one only because two ends each count the other's dwell, which GSAP's pins never do in any creation order: a pair whose ends count a share of each other would grow toward `1 / (1 - share)` times their own length. Others still settle on one no creation order gives: GSAP sizes a pin's spacer when that pin refreshes, so a scene refreshed first holds in the spacers added inside it later, yet one `end` can count a spacer through a scene's `trigger` while another counts that scene's dwell, which needs it refreshed first. The first two resolve the way GSAP's pins would if created in dependency order: each layer after the layers its `end` counts and after the scenes whose `trigger` encloses its own, in DOM order among layers that depend on each other. The third resolves the way they would if created in DOM order, close to the top-to-bottom order GSAP recommends creating them in. Either way, each `end` then counts only the layers before it, and a scene enclosed by an earlier one no longer pushes points outside that scene. If that order doesn't settle either, absolute starts and ends are read as each pin sees them when refreshed in that order.
|
|
509
513
|
|
|
510
514
|
A `createOverlapScroll` cover layer's `end` instead counts a scene's dwell exactly when that scene freezes before the point arrives, for any `endTrigger` inside the container, registered or not: its window describes the rise on screen, and a cover has no pin spacer to match.
|
|
511
515
|
|
|
@@ -541,7 +545,7 @@ sticky.createOverlapScroll({
|
|
|
541
545
|
- GSAP's `clamp()` position wrapper isn't accepted, and a function-valued `start`/`end` is called with no arguments rather than with the `ScrollTrigger` instance GSAP passes (see [Position syntax](#position-syntax))
|
|
542
546
|
- A `createStickyTrigger` Scene layer can't point `endTrigger` at a registered layer later in DOM order; `createOverlapScroll`'s cover layer can (see [End syntax](#end-syntax))
|
|
543
547
|
- If a Scene layer's `end` uses a position clause, don't set `endTrigger` to an element that gets pushed down by that same scene's own dwell; the value won't converge
|
|
544
|
-
- If Scene layers' `endTrigger`s depend on each other in a way that never settles, the layers resolve in
|
|
548
|
+
- If Scene layers' `endTrigger`s depend on each other in a way that never settles, or only settles by counting each other's dwell or in a way no creation order gives, the layers resolve the way GSAP's pins would in one creation order (see [End syntax](#end-syntax)). `refresh()` throws only if that order doesn't settle either
|
|
545
549
|
- `end: 'max'` throws on `createStickyTrigger` and `createStickyPin` for the same reason (their own padding/spacer would depend on itself); use `createOverlapScroll` instead
|
|
546
550
|
- Using the same element as the `trigger` of two different `createStickyTrigger`/`createOverlapScroll`/`createStickyPin` calls throws
|
|
547
551
|
- You must call `refresh()` once manually after registration. Window resize/load recomputation is automatically wired to GSAP's own `refreshInit`, but for layout changes that don't involve those (e.g. content height changes), call `ScrollTrigger.refresh()` yourself (see [Calling refresh](#calling-refresh))
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @license
|
|
3
|
-
* StickyScrollTrigger v0.
|
|
3
|
+
* StickyScrollTrigger v0.11.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
|
|
@@ -99,7 +99,7 @@ var StickyScrollTrigger = (function() {
|
|
|
99
99
|
el.style.position = saved.position;
|
|
100
100
|
el.style.top = saved.top;
|
|
101
101
|
};
|
|
102
|
-
const wrapScene = (inner) => {
|
|
102
|
+
const wrapScene = (inner, holdTrailingMargin) => {
|
|
103
103
|
if (!inner.parentNode) throw new Error(`StickyScrollTrigger: ${describeElement(inner)} is not attached to the document`);
|
|
104
104
|
const container = document.createElement("div");
|
|
105
105
|
const wrapper = document.createElement("div");
|
|
@@ -110,6 +110,11 @@ var StickyScrollTrigger = (function() {
|
|
|
110
110
|
container.appendChild(wrapper);
|
|
111
111
|
wrapper.appendChild(inner);
|
|
112
112
|
container.appendChild(padding);
|
|
113
|
+
if (holdTrailingMargin) {
|
|
114
|
+
const sentinel = document.createElement("div");
|
|
115
|
+
sentinel.style.display = "flow-root";
|
|
116
|
+
wrapper.appendChild(sentinel);
|
|
117
|
+
}
|
|
113
118
|
return {
|
|
114
119
|
container,
|
|
115
120
|
wrapper,
|
|
@@ -309,16 +314,16 @@ var StickyScrollTrigger = (function() {
|
|
|
309
314
|
};
|
|
310
315
|
const isHeldIn = (measurements, index, enclosedBy, anchorElement, order) => measurements[index].triggerEnclosedBy.some((m) => m !== index && measurements[m].kind === "scene" && order.isBefore(m, index) && !(enclosedBy.includes(m) && m !== anchorElement));
|
|
311
316
|
const nestedIn = (measurements, index) => measurements.flatMap(({ triggerEnclosedBy }, k) => k !== index && triggerEnclosedBy.includes(index) ? [k] : []);
|
|
312
|
-
const anchorShare = (measurements,
|
|
317
|
+
const anchorShare = (measurements, world, index, freezeStart, box, reachedAt, enclosedBy, anchorElement, order, ownIndex) => {
|
|
313
318
|
const measurement = measurements[index];
|
|
314
|
-
if (enclosedBy.includes(index)) return (measurement.start.mode === "absolute" ?
|
|
319
|
+
if (enclosedBy.includes(index)) return (measurement.start.mode === "absolute" ? world.unpaddedStart(index, ownIndex, freezeStart) : measurement.triggerTop - measurement.start.anchorOffset) < reachedAt - .05 ? 1 : 0;
|
|
315
320
|
if (order && isHeldIn(measurements, index, enclosedBy, anchorElement, order)) return 0;
|
|
316
321
|
if (box.nests.includes(index)) return box.fraction;
|
|
317
322
|
return measurement.triggerTop + measurement.triggerHeight <= box.top + .05 ? 1 : 0;
|
|
318
323
|
};
|
|
319
|
-
const gapsBeforeEndAnchor = (measurements, windows, box, reachedAt, enclosedBy, ownIndex, anchorElement, order, counted) => windows.reduce((total, { index, freezeStart, freezeEnd }) => {
|
|
324
|
+
const gapsBeforeEndAnchor = (measurements, world, windows, box, reachedAt, enclosedBy, ownIndex, anchorElement, order, counted) => windows.reduce((total, { index, freezeStart, freezeEnd }) => {
|
|
320
325
|
if (index === ownIndex || order && order.isBefore(ownIndex, index)) return total;
|
|
321
|
-
const weight = anchorShare(measurements,
|
|
326
|
+
const weight = anchorShare(measurements, world, index, freezeStart, box, reachedAt, enclosedBy, anchorElement, order, ownIndex);
|
|
322
327
|
if (weight === 0) return total;
|
|
323
328
|
counted?.add(index);
|
|
324
329
|
return total + weight * (freezeEnd - freezeStart);
|
|
@@ -353,11 +358,11 @@ var StickyScrollTrigger = (function() {
|
|
|
353
358
|
}
|
|
354
359
|
throw new Error("StickyScrollTrigger: internal error: clauseFreezeStart was given a layer without a clause start.");
|
|
355
360
|
};
|
|
356
|
-
const runPass = (measurements, { viewportHeight, measureLiveEndTriggerTop }, previous, order, record) => {
|
|
361
|
+
const runPass = (measurements, { viewportHeight, measureLiveEndTriggerTop }, previous, order, world, record, previousOnly = false) => {
|
|
357
362
|
const fresh = [];
|
|
358
363
|
const knownWindows = () => measurements.flatMap((measurement, k) => {
|
|
359
364
|
if (measurement.kind !== "scene") return [];
|
|
360
|
-
const plan = fresh[k];
|
|
365
|
+
const plan = previousOnly ? null : fresh[k];
|
|
361
366
|
if (plan) return [{
|
|
362
367
|
index: k,
|
|
363
368
|
freezeStart: plan.freezeStart,
|
|
@@ -371,7 +376,7 @@ var StickyScrollTrigger = (function() {
|
|
|
371
376
|
freezeEnd: freezeStart + paddingHeight
|
|
372
377
|
}];
|
|
373
378
|
});
|
|
374
|
-
const paddingOf = (k) => fresh[k]?.paddingHeight ?? previous?.paddings[k] ?? 0;
|
|
379
|
+
const paddingOf = (k) => (previousOnly ? null : fresh[k]?.paddingHeight) ?? previous?.paddings[k] ?? 0;
|
|
375
380
|
const recounts = [];
|
|
376
381
|
const plans = measurements.map((measurement, index) => {
|
|
377
382
|
fresh[index] = null;
|
|
@@ -383,15 +388,17 @@ var StickyScrollTrigger = (function() {
|
|
|
383
388
|
};
|
|
384
389
|
recounts.push(recount);
|
|
385
390
|
const { start } = measurement;
|
|
386
|
-
const freezeStart = start.mode === "absolute" ? start.value : clauseFreezeStart(measurements, index, paddingOf);
|
|
391
|
+
const freezeStart = start.mode === "absolute" ? start.value : previous?.freezeStarts[index] ?? clauseFreezeStart(measurements, index, paddingOf);
|
|
387
392
|
let freezeEnd;
|
|
388
393
|
switch (measurement.end.mode) {
|
|
389
394
|
case "dwell":
|
|
390
395
|
freezeEnd = freezeStart + measurement.end.distancePx;
|
|
391
396
|
break;
|
|
392
|
-
case "absolute":
|
|
393
|
-
|
|
397
|
+
case "absolute": {
|
|
398
|
+
const length = world.absoluteEndLength(index);
|
|
399
|
+
freezeEnd = length === null ? Math.max(freezeStart, measurement.end.value) : freezeStart + length;
|
|
394
400
|
break;
|
|
401
|
+
}
|
|
395
402
|
case "auto":
|
|
396
403
|
case "max": throw new Error(`StickyScrollTrigger: internal error: a Scene layer reached '${measurement.end.mode}' end mode, which only a cover layer can have.`);
|
|
397
404
|
case "clause": {
|
|
@@ -427,7 +434,7 @@ var StickyScrollTrigger = (function() {
|
|
|
427
434
|
fraction: resolveElementFraction(measurement.end.clause),
|
|
428
435
|
nests: measurement.endTriggerNests
|
|
429
436
|
};
|
|
430
|
-
const gaps = (w, counted) => gapsBeforeEndAnchor(measurements, w, box, rawTop - anchorOffsetEnd, measurement.endTriggerEnclosedBy, index, measurement.endTriggerIndex, order, counted);
|
|
437
|
+
const gaps = (w, counted) => gapsBeforeEndAnchor(measurements, world, w, box, rawTop - anchorOffsetEnd, measurement.endTriggerEnclosedBy, index, measurement.endTriggerIndex, order, counted);
|
|
431
438
|
endTop = rawTop + gaps(knownWindows());
|
|
432
439
|
recount.runs.push((w) => gaps(w, recount.counted));
|
|
433
440
|
}
|
|
@@ -435,7 +442,7 @@ var StickyScrollTrigger = (function() {
|
|
|
435
442
|
let layoutStart;
|
|
436
443
|
if (start.mode === "absolute") layoutStart = start.value;
|
|
437
444
|
else {
|
|
438
|
-
const gaps = (w, counted) => gapsBeforeEndAnchor(measurements, w, {
|
|
445
|
+
const gaps = (w, counted) => gapsBeforeEndAnchor(measurements, world, w, {
|
|
439
446
|
top: measurement.triggerTop,
|
|
440
447
|
fraction: start.elementFraction,
|
|
441
448
|
nests: nestedIn(measurements, index)
|
|
@@ -447,12 +454,10 @@ var StickyScrollTrigger = (function() {
|
|
|
447
454
|
break;
|
|
448
455
|
}
|
|
449
456
|
}
|
|
450
|
-
const paddingHeight = Math.max(0, freezeEnd - freezeStart);
|
|
451
457
|
const plan = {
|
|
452
458
|
freezeStart,
|
|
453
459
|
freezeEnd,
|
|
454
|
-
|
|
455
|
-
paddingHeight
|
|
460
|
+
paddingHeight: Math.max(0, freezeEnd - freezeStart)
|
|
456
461
|
};
|
|
457
462
|
fresh[index] = plan;
|
|
458
463
|
return plan;
|
|
@@ -496,6 +501,8 @@ var StickyScrollTrigger = (function() {
|
|
|
496
501
|
return {
|
|
497
502
|
freezeStart,
|
|
498
503
|
freezeEnd: Math.max(freezeStart, freezeEnd),
|
|
504
|
+
engagedStart: freezeStart,
|
|
505
|
+
engagedEnd: Math.max(freezeStart, freezeEnd),
|
|
499
506
|
stickyTop: measurement.start.anchorOffset - (measurement.triggerTop - measurement.wrapperTop),
|
|
500
507
|
paddingHeight: null
|
|
501
508
|
};
|
|
@@ -550,8 +557,67 @@ var StickyScrollTrigger = (function() {
|
|
|
550
557
|
});
|
|
551
558
|
return { isBefore: (a, b) => rank[a] < rank[b] };
|
|
552
559
|
};
|
|
560
|
+
const hasCycle = (scenes, edges) => {
|
|
561
|
+
const component = componentsOf(edges);
|
|
562
|
+
return new Set(scenes.map((k) => component[k])).size < scenes.length;
|
|
563
|
+
};
|
|
564
|
+
const refreshOrderEdges = (measurements, lengths, edges) => {
|
|
565
|
+
const before = measurements.map(() => []);
|
|
566
|
+
edges.forEach((movers, i) => {
|
|
567
|
+
const measurement = measurements[i];
|
|
568
|
+
const enclosingAnchor = (measurement.end.mode === "clause" && !measurement.endTriggerIsSelf ? measurement.endTriggerEnclosedBy : measurement.triggerEnclosedBy).filter((m) => m !== measurement.endTriggerIndex);
|
|
569
|
+
movers.forEach((k) => {
|
|
570
|
+
if (lengths[k] <= .05) return;
|
|
571
|
+
before[i].push(k);
|
|
572
|
+
measurements[k].triggerEnclosedBy.forEach((m) => {
|
|
573
|
+
if (m !== k && m !== i && measurements[m].kind === "scene" && !enclosingAnchor.includes(m)) before[m].push(k);
|
|
574
|
+
});
|
|
575
|
+
});
|
|
576
|
+
});
|
|
577
|
+
return before;
|
|
578
|
+
};
|
|
579
|
+
const placeWindows = (measurements, scenes, lengths, atWrittenEnd) => {
|
|
580
|
+
const rest = new Set(scenes);
|
|
581
|
+
const reported = [];
|
|
582
|
+
const engaged = [];
|
|
583
|
+
let consumed = 0;
|
|
584
|
+
let frozenUntil = -Infinity;
|
|
585
|
+
while (rest.size) {
|
|
586
|
+
let next = -1;
|
|
587
|
+
let opensAt = 0;
|
|
588
|
+
let nextIsAbsolute = false;
|
|
589
|
+
let nextReach = 0;
|
|
590
|
+
for (const k of rest) {
|
|
591
|
+
const { start, triggerTop } = measurements[k];
|
|
592
|
+
const isAbsolute = start.mode === "absolute";
|
|
593
|
+
const reach = start.mode === "clause" ? triggerTop - start.anchorOffset : 0;
|
|
594
|
+
const at = start.mode === "absolute" ? start.value : reach + consumed;
|
|
595
|
+
const tied = Math.abs(at - opensAt) <= TIE_TOLERANCE_PX;
|
|
596
|
+
if (next === -1 || at < opensAt - .05 || tied && (isAbsolute ? !nextIsAbsolute || at < opensAt : !nextIsAbsolute && reach < nextReach - .05)) {
|
|
597
|
+
next = k;
|
|
598
|
+
opensAt = at;
|
|
599
|
+
nextIsAbsolute = isAbsolute;
|
|
600
|
+
nextReach = reach;
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
const { end } = measurements[next];
|
|
604
|
+
const reportedEnd = end.mode === "absolute" && atWrittenEnd(next) ? Math.max(opensAt, end.value) : opensAt + lengths[next];
|
|
605
|
+
const engagedStart = Math.max(opensAt, frozenUntil);
|
|
606
|
+
const engagedEnd = Math.max(engagedStart, reportedEnd);
|
|
607
|
+
rest.delete(next);
|
|
608
|
+
reported[next] = [opensAt, reportedEnd];
|
|
609
|
+
engaged[next] = [engagedStart, engagedEnd];
|
|
610
|
+
consumed += engagedEnd - engagedStart;
|
|
611
|
+
frozenUntil = Math.max(frozenUntil, engagedEnd);
|
|
612
|
+
}
|
|
613
|
+
return {
|
|
614
|
+
reported,
|
|
615
|
+
engaged
|
|
616
|
+
};
|
|
617
|
+
};
|
|
553
618
|
const planLayers = (measurements, deps) => {
|
|
554
|
-
const
|
|
619
|
+
const scenes = measurements.flatMap(({ kind }, k) => kind === "scene" ? [k] : []);
|
|
620
|
+
const needsConvergence = scenes.length > 1;
|
|
555
621
|
const liveTopCache = new Map();
|
|
556
622
|
const passDeps = {
|
|
557
623
|
...deps,
|
|
@@ -563,47 +629,153 @@ var StickyScrollTrigger = (function() {
|
|
|
563
629
|
return value;
|
|
564
630
|
}
|
|
565
631
|
};
|
|
566
|
-
const stateOf = (passPlans) => ({
|
|
567
|
-
paddings: passPlans.map((plan) => plan?.paddingHeight ?? null),
|
|
568
|
-
freezeStarts: passPlans.map((plan) => plan?.freezeStart ?? null)
|
|
569
|
-
});
|
|
570
632
|
const sameValues = (a, b) => a.every((value, i) => value === b[i] || value !== null && b[i] !== null && Math.abs(value - b[i]) <= .05);
|
|
571
633
|
const sameState = (a, b) => sameValues(a.paddings, b.paddings) && sameValues(a.freezeStarts, b.freezeStarts);
|
|
572
|
-
const
|
|
573
|
-
const
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
634
|
+
const lengthsOf = (windows) => windows.map((window) => window ? Math.max(0, window.freezeEnd - window.freezeStart) : 0);
|
|
635
|
+
const stateAt = (lengths, reported) => ({
|
|
636
|
+
paddings: measurements.map(({ kind }, k) => kind === "scene" ? lengths[k] : null),
|
|
637
|
+
freezeStarts: measurements.map(({ kind }, k) => kind === "scene" ? reported[k][0] : null)
|
|
638
|
+
});
|
|
639
|
+
const mix = (states, weights) => {
|
|
640
|
+
const of = (pick) => pick(states[0]).map((value, k) => value === null ? null : states.reduce((sum, state, j) => sum + weights[j] * pick(state)[k], 0));
|
|
641
|
+
return {
|
|
642
|
+
paddings: of((state) => state.paddings),
|
|
643
|
+
freezeStarts: of((state) => state.freezeStarts)
|
|
644
|
+
};
|
|
645
|
+
};
|
|
646
|
+
let world = null;
|
|
647
|
+
const place = (lengths, order, reading) => {
|
|
648
|
+
const placed = placeWindows(measurements, scenes, lengths, () => reading.endsAsRefreshed);
|
|
649
|
+
world = {
|
|
650
|
+
lengths,
|
|
651
|
+
order,
|
|
652
|
+
reported: placed.reported,
|
|
653
|
+
refreshTime: []
|
|
654
|
+
};
|
|
655
|
+
return placed;
|
|
656
|
+
};
|
|
657
|
+
const refreshTimeOf = (own) => {
|
|
658
|
+
if (world === null) return null;
|
|
659
|
+
const { lengths, order, refreshTime } = world;
|
|
660
|
+
const absent = (k) => k === own || order !== null && !order.isBefore(k, own);
|
|
661
|
+
refreshTime[own] ??= placeWindows(measurements, scenes, lengths.map((length, k) => absent(k) ? 0 : length), (k) => !absent(k)).reported;
|
|
662
|
+
return refreshTime[own];
|
|
663
|
+
};
|
|
664
|
+
const passWorld = (order, reading) => ({
|
|
665
|
+
unpaddedStart: (index, ownIndex, freezeStart) => {
|
|
666
|
+
const windows = (reading.startsAsRefreshed ? refreshTimeOf(ownIndex) : null) ?? world?.reported ?? [];
|
|
667
|
+
const spans = scenes.flatMap((k) => {
|
|
668
|
+
if (k === index || k === ownIndex || !windows[k]) return [];
|
|
669
|
+
if (order && !order.isBefore(k, ownIndex)) return [];
|
|
670
|
+
const [from, to] = windows[k];
|
|
671
|
+
return Math.min(to, freezeStart) > from ? [[from, Math.min(to, freezeStart)]] : [];
|
|
672
|
+
}).sort((a, b) => a[0] - b[0]);
|
|
673
|
+
let frozen = 0;
|
|
674
|
+
let frozenUntil = -Infinity;
|
|
675
|
+
for (const [from, to] of spans) {
|
|
676
|
+
const lo = Math.max(from, frozenUntil);
|
|
677
|
+
if (to > lo) frozen += to - lo;
|
|
678
|
+
frozenUntil = Math.max(frozenUntil, to);
|
|
679
|
+
}
|
|
680
|
+
return freezeStart - frozen;
|
|
681
|
+
},
|
|
682
|
+
absoluteEndLength: (index) => {
|
|
683
|
+
const { end } = measurements[index];
|
|
684
|
+
if (!reading.endsAsRefreshed || end.mode !== "absolute" || world === null) return null;
|
|
685
|
+
const windows = world.order === null ? world.reported : refreshTimeOf(index);
|
|
686
|
+
return Math.max(0, end.value - windows[index][0]);
|
|
687
|
+
}
|
|
688
|
+
});
|
|
689
|
+
const solve = (order, reading, cap, record = null, cycle = 2, damping = 1) => {
|
|
690
|
+
const reads = passWorld(order, reading);
|
|
691
|
+
const fed = [];
|
|
692
|
+
let previous = null;
|
|
693
|
+
for (let pass = 0; pass < cap; pass += 1) {
|
|
694
|
+
const lengths = lengthsOf(runPass(measurements, passDeps, previous, order, reads, record));
|
|
695
|
+
const placed = place(lengths, order, reading);
|
|
696
|
+
const next = stateAt(lengths, placed.reported);
|
|
697
|
+
if (!needsConvergence || previous !== null && sameState(next, previous)) return {
|
|
698
|
+
lengths,
|
|
699
|
+
...placed
|
|
700
|
+
};
|
|
701
|
+
let feed = next;
|
|
702
|
+
if (previous !== null) {
|
|
703
|
+
fed.push(previous);
|
|
704
|
+
let back = -1;
|
|
705
|
+
for (let j = 0; j < cycle - 1 && back === -1; j += 1) if (fed.length > j + 1 && sameState(next, fed[fed.length - j - 2])) back = j;
|
|
706
|
+
if (back !== -1) {
|
|
707
|
+
const states = [...fed.slice(fed.length - back - 1), next];
|
|
708
|
+
feed = mix(states, states.map(() => 1 / states.length));
|
|
709
|
+
} else if (damping < 1) feed = mix([next, previous], [damping, 1 - damping]);
|
|
710
|
+
}
|
|
711
|
+
previous = feed;
|
|
589
712
|
}
|
|
590
713
|
return null;
|
|
591
714
|
};
|
|
715
|
+
const activeEdges = (lengths, reading) => {
|
|
716
|
+
const reads = passWorld(null, reading);
|
|
717
|
+
const respond = (from) => {
|
|
718
|
+
const { reported } = place(from, null, reading);
|
|
719
|
+
return lengthsOf(runPass(measurements, passDeps, stateAt(from, reported), null, reads, null, true));
|
|
720
|
+
};
|
|
721
|
+
const base = respond(lengths);
|
|
722
|
+
const edges = measurements.map(() => []);
|
|
723
|
+
scenes.forEach((k) => {
|
|
724
|
+
const nudged = lengths.slice();
|
|
725
|
+
nudged[k] += 2 ** -7;
|
|
726
|
+
respond(nudged).forEach((length, i) => {
|
|
727
|
+
if (i !== k && Math.abs(length - base[i]) > 1e-6) edges[i].push(k);
|
|
728
|
+
});
|
|
729
|
+
});
|
|
730
|
+
return edges;
|
|
731
|
+
};
|
|
732
|
+
const unsettled = (solved) => solved === null || solved.reported.some((window) => window.some((point) => !(Math.abs(point) <= 2 ** 48)));
|
|
592
733
|
const counted = measurements.map(() => new Set());
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
734
|
+
const everySpacer = {
|
|
735
|
+
endsAsRefreshed: true,
|
|
736
|
+
startsAsRefreshed: false
|
|
737
|
+
};
|
|
738
|
+
let solved = solve(null, everySpacer, Math.max(64, 2 * measurements.length + 2), (index, layers) => layers.forEach((k) => counted[index].add(k)));
|
|
739
|
+
const fallback = () => {
|
|
740
|
+
if (unsettled(solved)) return fallbackOrder(measurements, counted);
|
|
741
|
+
if (!needsConvergence) return null;
|
|
742
|
+
const edges = activeEdges(solved.lengths, everySpacer);
|
|
743
|
+
if (hasCycle(scenes, edges)) return fallbackOrder(measurements, counted);
|
|
744
|
+
return hasCycle(scenes, refreshOrderEdges(measurements, solved.lengths, edges)) ? { isBefore: (a, b) => a < b } : null;
|
|
745
|
+
};
|
|
746
|
+
const order = fallback();
|
|
747
|
+
if (order) {
|
|
748
|
+
const asReported = {
|
|
749
|
+
endsAsRefreshed: false,
|
|
750
|
+
startsAsRefreshed: false
|
|
751
|
+
};
|
|
752
|
+
solved = solve(order, asReported, 2 * measurements.length + 2);
|
|
753
|
+
if (unsettled(solved)) solved = solve(order, asReported, Math.max(64, 2 * measurements.length + 2), null, 4, .5);
|
|
754
|
+
if (unsettled(solved)) solved = solve(order, {
|
|
755
|
+
endsAsRefreshed: true,
|
|
756
|
+
startsAsRefreshed: true
|
|
757
|
+
}, 2 * measurements.length + 2);
|
|
758
|
+
}
|
|
759
|
+
if (unsettled(solved)) throw new Error("StickyScrollTrigger: could not resolve endTrigger positions: some endTrigger references form a circular structural dependency that never settles. Point each endTrigger at an element that doesn't depend on it.");
|
|
760
|
+
const { reported, engaged } = solved;
|
|
761
|
+
const scenePlans = measurements.map(({ kind }, index) => {
|
|
762
|
+
if (kind !== "scene") return null;
|
|
763
|
+
const [engagedStart, engagedEnd] = engaged[index];
|
|
764
|
+
const outerDwell = scenes.reduce((total, k) => k > index && engaged[k][1] <= engagedStart + .05 ? total + (engaged[k][1] - engaged[k][0]) : total, 0);
|
|
600
765
|
return {
|
|
601
|
-
|
|
602
|
-
|
|
766
|
+
freezeStart: reported[index][0],
|
|
767
|
+
freezeEnd: reported[index][1],
|
|
768
|
+
engagedStart,
|
|
769
|
+
engagedEnd,
|
|
770
|
+
stickyTop: deps.structureTop - (engagedStart - outerDwell),
|
|
771
|
+
paddingHeight: engagedEnd - engagedStart
|
|
603
772
|
};
|
|
604
773
|
});
|
|
605
|
-
const windows =
|
|
606
|
-
|
|
774
|
+
const windows = scenes.map((k) => ({
|
|
775
|
+
freezeStart: engaged[k][0],
|
|
776
|
+
freezeEnd: engaged[k][1]
|
|
777
|
+
}));
|
|
778
|
+
const finalPlans = scenePlans.map((plan, index) => plan ?? planCover(measurements, index, windows, passDeps));
|
|
607
779
|
finalPlans.forEach((plan, index) => deps.onPlanned(index, plan));
|
|
608
780
|
return finalPlans;
|
|
609
781
|
};
|
|
@@ -731,7 +903,7 @@ var StickyScrollTrigger = (function() {
|
|
|
731
903
|
let outermost = null;
|
|
732
904
|
for (const layer of layers) {
|
|
733
905
|
if (layer.kind !== "scene") continue;
|
|
734
|
-
const wrapped = wrapScene(inner);
|
|
906
|
+
const wrapped = wrapScene(inner, inner === rootElement);
|
|
735
907
|
layer.container = wrapped.container;
|
|
736
908
|
layer.wrapper = wrapped.wrapper;
|
|
737
909
|
layer.padding = wrapped.padding;
|
|
@@ -1036,7 +1208,7 @@ var StickyScrollTrigger = (function() {
|
|
|
1036
1208
|
const topPx = resolveAnchorTop(classifiedStart.value, triggerHeight, viewportHeight);
|
|
1037
1209
|
const toScroll = (element, reachedAt) => this.#addDwellBeforeReach(element, reachedAt);
|
|
1038
1210
|
const releaseScroll = resolvePinReleaseScroll(layer.endTrigger, classifiedEnd, toScroll(layer.trigger, triggerTop - topPx), viewportHeight, toScroll);
|
|
1039
|
-
const height = releaseScroll - (this.#rootElement.contains(layer.trigger) ? dwellConsumedAt(releaseScroll, this.#
|
|
1211
|
+
const height = releaseScroll - (this.#rootElement.contains(layer.trigger) ? dwellConsumedAt(releaseScroll, this.#engagedWindows()) : 0) - triggerTop + topPx + triggerHeight + triggerMarginBottom;
|
|
1040
1212
|
const room = documentHeight > 0 ? documentHeight - triggerTop : Infinity;
|
|
1041
1213
|
applyStickyPosition(layer.trigger, topPx);
|
|
1042
1214
|
layer.inner.style.height = `${Math.max(0, Math.min(height, room))}px`;
|
|
@@ -1074,6 +1246,8 @@ var StickyScrollTrigger = (function() {
|
|
|
1074
1246
|
const layer = active[index];
|
|
1075
1247
|
layer.freezeStart = plan.freezeStart;
|
|
1076
1248
|
layer.freezeEnd = plan.freezeEnd;
|
|
1249
|
+
layer.engagedStart = plan.engagedStart;
|
|
1250
|
+
layer.engagedEnd = plan.engagedEnd;
|
|
1077
1251
|
if (!layer.wrapper) return;
|
|
1078
1252
|
applyStickyPosition(layer.wrapper, plan.stickyTop);
|
|
1079
1253
|
if (layer.kind === "scene" && layer.padding && plan.paddingHeight !== null) layer.padding.style.height = `${plan.paddingHeight}px`;
|
|
@@ -1116,10 +1290,7 @@ var StickyScrollTrigger = (function() {
|
|
|
1116
1290
|
});
|
|
1117
1291
|
}
|
|
1118
1292
|
#syncScrollMargins() {
|
|
1119
|
-
this.#scrollMarginSync.sync(this.#
|
|
1120
|
-
freezeStart,
|
|
1121
|
-
freezeEnd
|
|
1122
|
-
})), this.#outermostContainer, (targets) => {
|
|
1293
|
+
this.#scrollMarginSync.sync(this.#engagedWindows(), this.#outermostContainer, (targets) => {
|
|
1123
1294
|
if (!targets.length) return [];
|
|
1124
1295
|
const restoreStickyState = this.#resetStickyState();
|
|
1125
1296
|
const tops = targets.map((target) => documentTop(target));
|
|
@@ -1182,7 +1353,9 @@ var StickyScrollTrigger = (function() {
|
|
|
1182
1353
|
start,
|
|
1183
1354
|
end,
|
|
1184
1355
|
freezeStart: 0,
|
|
1185
|
-
freezeEnd: 0
|
|
1356
|
+
freezeEnd: 0,
|
|
1357
|
+
engagedStart: 0,
|
|
1358
|
+
engagedEnd: 0
|
|
1186
1359
|
}, onKill, rest);
|
|
1187
1360
|
}
|
|
1188
1361
|
createOverlapScroll({ trigger: triggerInput, cover, start = "bottom bottom", end = null, endTrigger: endTriggerInput, onKill, ...rest }) {
|
|
@@ -1202,7 +1375,9 @@ var StickyScrollTrigger = (function() {
|
|
|
1202
1375
|
start,
|
|
1203
1376
|
end,
|
|
1204
1377
|
freezeStart: 0,
|
|
1205
|
-
freezeEnd: 0
|
|
1378
|
+
freezeEnd: 0,
|
|
1379
|
+
engagedStart: 0,
|
|
1380
|
+
engagedEnd: 0
|
|
1206
1381
|
};
|
|
1207
1382
|
const vars = this.#registerLayer(layer, (self) => {
|
|
1208
1383
|
this.#coverRestoreByLayer.get(layer)?.();
|
|
@@ -1264,10 +1439,13 @@ var StickyScrollTrigger = (function() {
|
|
|
1264
1439
|
}
|
|
1265
1440
|
#addDwellBeforeReach(element, reachedAt) {
|
|
1266
1441
|
if (!this.#rootElement.contains(element)) return reachedAt;
|
|
1267
|
-
return reachedAt + dwellBeforeReach(reachedAt, this.#
|
|
1442
|
+
return reachedAt + dwellBeforeReach(reachedAt, this.#engagedWindows());
|
|
1268
1443
|
}
|
|
1269
|
-
#
|
|
1270
|
-
return this.#layers.
|
|
1444
|
+
#engagedWindows() {
|
|
1445
|
+
return this.#layers.flatMap((layer) => layer.kind === "scene" ? [{
|
|
1446
|
+
freezeStart: layer.engagedStart,
|
|
1447
|
+
freezeEnd: layer.engagedEnd
|
|
1448
|
+
}] : []);
|
|
1271
1449
|
}
|
|
1272
1450
|
static getScrollTop(elementInput, instances) {
|
|
1273
1451
|
const element = resolveElement(elementInput, "StickyScrollTrigger.getScrollTop");
|