sticky-scroll-trigger 0.11.0 → 0.13.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 +67 -11
- package/dist/StickyScrollTrigger.d.ts +4 -1
- package/dist/StickyScrollTrigger.global.js +876 -143
- package/dist/StickyScrollTrigger.global.min.js +2 -2
- package/dist/StickyScrollTrigger.js +876 -143
- package/dist/StickyScrollTrigger.min.js +2 -2
- package/package.json +12 -12
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @license
|
|
3
|
-
* StickyScrollTrigger v0.
|
|
3
|
+
* StickyScrollTrigger v0.13.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
|
|
@@ -98,6 +98,11 @@ const restoreInlinePosition = (el, saved) => {
|
|
|
98
98
|
el.style.position = saved.position;
|
|
99
99
|
el.style.top = saved.top;
|
|
100
100
|
};
|
|
101
|
+
const stripStickyPositions = (elements) => {
|
|
102
|
+
const saved = elements.map(captureInlinePosition);
|
|
103
|
+
elements.forEach(resetStickyPosition);
|
|
104
|
+
return () => elements.forEach((el, i) => restoreInlinePosition(el, saved[i]));
|
|
105
|
+
};
|
|
101
106
|
const wrapScene = (inner, holdTrailingMargin) => {
|
|
102
107
|
if (!inner.parentNode) throw new Error(`StickyScrollTrigger: ${describeElement(inner)} is not attached to the document`);
|
|
103
108
|
const container = document.createElement("div");
|
|
@@ -200,6 +205,438 @@ const liftAboveStickyWrapper = (cover) => {
|
|
|
200
205
|
lifted.length = 0;
|
|
201
206
|
};
|
|
202
207
|
};
|
|
208
|
+
const AUTO_MIN_DEPTH = 7;
|
|
209
|
+
const MAX_CHAIN_IN_BAND = 4;
|
|
210
|
+
const BUFFER_FLOOR_PX = 325;
|
|
211
|
+
const BUFFER_STEP_PX = 25;
|
|
212
|
+
const isWebKit = () => typeof CSS !== "undefined" && typeof CSS.supports === "function" && CSS.supports("font", "-apple-system-body") || typeof navigator !== "undefined" && navigator.vendor === "Apple Computer, Inc." && !/\bjsdom\//.test(navigator.userAgent);
|
|
213
|
+
const mergeIntervals = (intervals) => {
|
|
214
|
+
const sorted = intervals.filter(([a, b]) => b > a).sort((x, y) => x[0] - y[0]);
|
|
215
|
+
const merged = [];
|
|
216
|
+
sorted.forEach(([a, b]) => {
|
|
217
|
+
const last = merged.at(-1);
|
|
218
|
+
if (last && a <= last[1]) last[1] = Math.max(last[1], b);
|
|
219
|
+
else merged.push([a, b]);
|
|
220
|
+
});
|
|
221
|
+
return merged;
|
|
222
|
+
};
|
|
223
|
+
const invertStuckUnion = (union, y, inclusive) => {
|
|
224
|
+
let scroll = y;
|
|
225
|
+
for (const [a, b] of union) if (inclusive ? a <= scroll : a < scroll) scroll += b - a;
|
|
226
|
+
else break;
|
|
227
|
+
return scroll;
|
|
228
|
+
};
|
|
229
|
+
const isTransformed = (style) => [
|
|
230
|
+
style.transform,
|
|
231
|
+
style.translate,
|
|
232
|
+
style.scale,
|
|
233
|
+
style.rotate
|
|
234
|
+
].some((value) => value !== void 0 && value !== "" && value !== "none");
|
|
235
|
+
const hasTransform = (el, memo) => {
|
|
236
|
+
if (!el) return false;
|
|
237
|
+
const known = memo.get(el);
|
|
238
|
+
if (known !== void 0) return known;
|
|
239
|
+
const result = isTransformed(getComputedStyle(el)) || hasTransform(el.parentElement, memo);
|
|
240
|
+
memo.set(el, result);
|
|
241
|
+
return result;
|
|
242
|
+
};
|
|
243
|
+
const scrolls = (style) => ![style.overflowX, style.overflowY].every((v) => v === "visible" || v === "clip");
|
|
244
|
+
const bodyScrolls = () => scrolls(getComputedStyle(document.documentElement)) && scrolls(getComputedStyle(document.body));
|
|
245
|
+
const isInsideScroller = (el, memo) => {
|
|
246
|
+
const node = el.parentElement;
|
|
247
|
+
if (!node || node === document.body || node === document.documentElement) return false;
|
|
248
|
+
const known = memo.get(node);
|
|
249
|
+
if (known !== void 0) return known;
|
|
250
|
+
const style = getComputedStyle(node);
|
|
251
|
+
const result = style.position === "fixed" || scrolls(style) || isInsideScroller(node, memo);
|
|
252
|
+
memo.set(node, result);
|
|
253
|
+
return result;
|
|
254
|
+
};
|
|
255
|
+
const ownedDepth = (owned, demoted) => {
|
|
256
|
+
const levels = new Map();
|
|
257
|
+
const levelsUpFrom = (node) => {
|
|
258
|
+
if (!node) return 0;
|
|
259
|
+
const known = levels.get(node);
|
|
260
|
+
if (known !== void 0) return known;
|
|
261
|
+
const level = (demoted.has(node) || getComputedStyle(node).position === "sticky" ? 1 : 0) + levelsUpFrom(node.parentElement);
|
|
262
|
+
levels.set(node, level);
|
|
263
|
+
return level;
|
|
264
|
+
};
|
|
265
|
+
const scrollerMemo = new Map();
|
|
266
|
+
let depth = 0;
|
|
267
|
+
for (const el of owned) {
|
|
268
|
+
if (!el.isConnected || isInsideScroller(el, scrollerMemo)) continue;
|
|
269
|
+
depth = Math.max(depth, 1 + levelsUpFrom(el.parentElement));
|
|
270
|
+
}
|
|
271
|
+
return depth;
|
|
272
|
+
};
|
|
273
|
+
const SIMPLE_TOP = /^-?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?(px|%)$/i;
|
|
274
|
+
const clients = new Set();
|
|
275
|
+
let records = [];
|
|
276
|
+
let buffer = 0;
|
|
277
|
+
let listening = false;
|
|
278
|
+
let scrollBeforeGsapRefresh = null;
|
|
279
|
+
let gsapPhase = "idle";
|
|
280
|
+
let adoptedMidRefresh = false;
|
|
281
|
+
let scannedThisTask = null;
|
|
282
|
+
const gsapClasses = new Set();
|
|
283
|
+
const gsapRefreshing = () => Array.from(gsapClasses).some((cls) => cls.isRefreshing === true);
|
|
284
|
+
const stateAt = (record, scroll) => {
|
|
285
|
+
if (!record.demotable) return "in";
|
|
286
|
+
if (scroll < record.lo - buffer) return "before";
|
|
287
|
+
return scroll > record.hi + buffer && !record.holdsAfter ? "after" : "in";
|
|
288
|
+
};
|
|
289
|
+
const promote = (record, afterMeasure = false) => {
|
|
290
|
+
const { style } = record.el;
|
|
291
|
+
const demoted = record.state !== "in";
|
|
292
|
+
const positionOurs = afterMeasure && style.position === "static" || demoted && style.position === "relative";
|
|
293
|
+
if (positionOurs) style.position = record.position;
|
|
294
|
+
if (demoted && positionOurs && style.top === record.demotedTop) style.top = record.top;
|
|
295
|
+
if (demoted && record.zIndexAuto && style.zIndex === "0") style.zIndex = record.zIndex;
|
|
296
|
+
if (demoted && record.willChangeAuto && style.willChange === "opacity") style.willChange = record.willChange;
|
|
297
|
+
record.state = "in";
|
|
298
|
+
};
|
|
299
|
+
const applyState = (record, state) => {
|
|
300
|
+
if (record.state === state) return;
|
|
301
|
+
if (state === "in") {
|
|
302
|
+
promote(record);
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
const { style } = record.el;
|
|
306
|
+
style.position = "relative";
|
|
307
|
+
style.top = `${state === "before" ? 0 : record.travel}px`;
|
|
308
|
+
record.demotedTop = style.top;
|
|
309
|
+
if (record.zIndexAuto) style.zIndex = "0";
|
|
310
|
+
if (record.willChangeAuto) style.willChange = "opacity";
|
|
311
|
+
record.state = state;
|
|
312
|
+
};
|
|
313
|
+
const update = (scroll) => {
|
|
314
|
+
records.forEach((record) => applyState(record, stateAt(record, scroll)));
|
|
315
|
+
};
|
|
316
|
+
const onScroll = () => update(window.scrollY);
|
|
317
|
+
const listen = (on) => {
|
|
318
|
+
if (on === listening) return;
|
|
319
|
+
listening = on;
|
|
320
|
+
if (on) window.addEventListener("scroll", onScroll, { passive: true });
|
|
321
|
+
else window.removeEventListener("scroll", onScroll);
|
|
322
|
+
};
|
|
323
|
+
const clearTable = () => {
|
|
324
|
+
records.forEach((record) => promote(record));
|
|
325
|
+
records = [];
|
|
326
|
+
listen(false);
|
|
327
|
+
};
|
|
328
|
+
const restoreAfterGsapMeasure = () => {
|
|
329
|
+
const scroll = scrollBeforeGsapRefresh;
|
|
330
|
+
if (gsapPhase !== "measuring" || scroll === null) return;
|
|
331
|
+
gsapPhase = "restored";
|
|
332
|
+
if (records.length) update(scroll);
|
|
333
|
+
};
|
|
334
|
+
const endGsapRefresh = () => {
|
|
335
|
+
gsapPhase = "idle";
|
|
336
|
+
scrollBeforeGsapRefresh = null;
|
|
337
|
+
adoptedMidRefresh = false;
|
|
338
|
+
};
|
|
339
|
+
const rebuildSafely = () => {
|
|
340
|
+
try {
|
|
341
|
+
rebuildDormancy(null);
|
|
342
|
+
} catch (error) {
|
|
343
|
+
clearTable();
|
|
344
|
+
queueMicrotask(() => {
|
|
345
|
+
throw error;
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
const onGsapRefreshDone = () => {
|
|
350
|
+
const wasRefreshing = gsapPhase !== "idle";
|
|
351
|
+
endGsapRefresh();
|
|
352
|
+
if (wasRefreshing && liveClients().length) rebuildSafely();
|
|
353
|
+
};
|
|
354
|
+
const onGsapRefreshInit = () => {
|
|
355
|
+
if (gsapPhase === "idle") {
|
|
356
|
+
gsapPhase = "measuring";
|
|
357
|
+
scrollBeforeGsapRefresh = window.scrollY;
|
|
358
|
+
queueMicrotask(onGsapRefreshDone);
|
|
359
|
+
if (records.length) update(0);
|
|
360
|
+
}
|
|
361
|
+
return { render: restoreAfterGsapMeasure };
|
|
362
|
+
};
|
|
363
|
+
const adoptGsapClass = (cls) => {
|
|
364
|
+
if (gsapClasses.has(cls)) return;
|
|
365
|
+
if (cls.isRefreshing === true) adoptedMidRefresh = true;
|
|
366
|
+
gsapClasses.add(cls);
|
|
367
|
+
cls.addEventListener?.("refresh", onGsapRefreshDone);
|
|
368
|
+
cls.addEventListener?.("refreshInit", onGsapRefreshInit);
|
|
369
|
+
};
|
|
370
|
+
const dropGsapClassesLater = () => {
|
|
371
|
+
if (!gsapClasses.size) return;
|
|
372
|
+
queueMicrotask(() => {
|
|
373
|
+
if (liveClients().length) return;
|
|
374
|
+
gsapClasses.forEach((cls) => {
|
|
375
|
+
cls.removeEventListener?.("refresh", onGsapRefreshDone);
|
|
376
|
+
cls.removeEventListener?.("refreshInit", onGsapRefreshInit);
|
|
377
|
+
});
|
|
378
|
+
gsapClasses.clear();
|
|
379
|
+
});
|
|
380
|
+
};
|
|
381
|
+
const scanStickyElements = (owned) => {
|
|
382
|
+
const scrollerMemo = new Map();
|
|
383
|
+
const isWindowSticky = (el) => el.isConnected && getComputedStyle(el).position === "sticky" && !isInsideScroller(el, scrollerMemo);
|
|
384
|
+
if (scannedThisTask) return Array.from(new Set([
|
|
385
|
+
...scannedThisTask,
|
|
386
|
+
...records.map((r) => r.el),
|
|
387
|
+
...owned
|
|
388
|
+
])).filter(isWindowSticky);
|
|
389
|
+
const found = Array.from(document.querySelectorAll("*")).filter(isWindowSticky);
|
|
390
|
+
scannedThisTask = found;
|
|
391
|
+
queueMicrotask(() => {
|
|
392
|
+
scannedThisTask = null;
|
|
393
|
+
});
|
|
394
|
+
return found;
|
|
395
|
+
};
|
|
396
|
+
const maxChainInBand = (b, maxScroll) => {
|
|
397
|
+
const bands = records.flatMap((r) => {
|
|
398
|
+
const band = r.demotable ? [Math.max(0, r.lo - b), Math.min(maxScroll, r.hi + (r.holdsAfter ? Infinity : b))] : [0, maxScroll];
|
|
399
|
+
return band[1] >= band[0] ? [{
|
|
400
|
+
record: r,
|
|
401
|
+
start: band[0],
|
|
402
|
+
end: band[1]
|
|
403
|
+
}] : [];
|
|
404
|
+
});
|
|
405
|
+
let max = 0;
|
|
406
|
+
new Set([0, ...bands.filter(({ record }) => record.demotable).map(({ start }) => start)]).forEach((scroll) => {
|
|
407
|
+
const inBand = new Set(bands.filter(({ start, end }) => start <= scroll && scroll <= end).map(({ record }) => record));
|
|
408
|
+
inBand.forEach((r) => {
|
|
409
|
+
max = Math.max(max, 1 + r.ancestors.filter((a) => inBand.has(a)).length);
|
|
410
|
+
});
|
|
411
|
+
});
|
|
412
|
+
return max;
|
|
413
|
+
};
|
|
414
|
+
const chooseBuffer = (viewportHeight, maxScroll) => {
|
|
415
|
+
let low = Math.ceil(BUFFER_FLOOR_PX / BUFFER_STEP_PX);
|
|
416
|
+
let high = Math.floor(viewportHeight / BUFFER_STEP_PX);
|
|
417
|
+
if (high < low || maxChainInBand(low * BUFFER_STEP_PX, maxScroll) > MAX_CHAIN_IN_BAND) return BUFFER_FLOOR_PX;
|
|
418
|
+
while (low < high) {
|
|
419
|
+
const mid = Math.ceil((low + high) / 2);
|
|
420
|
+
if (maxChainInBand(mid * BUFFER_STEP_PX, maxScroll) <= MAX_CHAIN_IN_BAND) low = mid;
|
|
421
|
+
else high = mid - 1;
|
|
422
|
+
}
|
|
423
|
+
return low * BUFFER_STEP_PX;
|
|
424
|
+
};
|
|
425
|
+
const liveClients = () => {
|
|
426
|
+
const live = [];
|
|
427
|
+
clients.forEach((ref) => {
|
|
428
|
+
const client = ref.deref();
|
|
429
|
+
if (!client) {
|
|
430
|
+
clients.delete(ref);
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
const owned = client.ownedElements();
|
|
434
|
+
if (owned.some((el) => el.isConnected)) live.push({
|
|
435
|
+
client,
|
|
436
|
+
owned
|
|
437
|
+
});
|
|
438
|
+
});
|
|
439
|
+
return live;
|
|
440
|
+
};
|
|
441
|
+
const measureStatic = (all, owned, unmodeled) => {
|
|
442
|
+
const scroll = window.scrollY;
|
|
443
|
+
const viewportHeight = document.documentElement.clientHeight || window.innerHeight;
|
|
444
|
+
const transformMemo = new Map();
|
|
445
|
+
const documentHeight = measureDocumentHeight();
|
|
446
|
+
all.forEach((record) => {
|
|
447
|
+
const { el } = record;
|
|
448
|
+
const rect = el.getBoundingClientRect();
|
|
449
|
+
const computed = getComputedStyle(el);
|
|
450
|
+
const parent = el.parentElement;
|
|
451
|
+
let containerBottom = rect.bottom + scroll;
|
|
452
|
+
if (parent) {
|
|
453
|
+
const parentStyle = getComputedStyle(parent);
|
|
454
|
+
containerBottom = parent.getBoundingClientRect().bottom + scroll - parseFloat(parentStyle.borderBottomWidth) - parseFloat(parentStyle.paddingBottom);
|
|
455
|
+
if (parentStyle.display === "contents" || parentStyle.display === "inline") unmodeled.add(record);
|
|
456
|
+
}
|
|
457
|
+
record.naturalTop = rect.top + scroll;
|
|
458
|
+
record.travel = Math.max(0, containerBottom - (record.naturalTop + rect.height + parseFloat(computed.marginBottom)));
|
|
459
|
+
record.overflowBottom = record.naturalTop + el.clientTop + el.scrollHeight;
|
|
460
|
+
if (computed.position !== "static" || hasTransform(el, transformMemo)) unmodeled.add(record);
|
|
461
|
+
if (computed.bottom !== "auto" || computed.left !== "auto" || computed.right !== "auto") unmodeled.add(record);
|
|
462
|
+
if (owned.has(el) && (!computed.top.endsWith("px") || Math.abs(parseFloat(computed.top) - parseFloat(el.style.top)) > .01)) unmodeled.add(record);
|
|
463
|
+
if (!owned.has(el) && computed.top === "auto") {
|
|
464
|
+
record.stickyTop = 0;
|
|
465
|
+
record.travel = 0;
|
|
466
|
+
} else if (!owned.has(el)) {
|
|
467
|
+
const unit = SIMPLE_TOP.exec(computed.top)?.[1];
|
|
468
|
+
if (!unit) unmodeled.add(record);
|
|
469
|
+
else if (unit === "%") record.stickyTop = parseFloat(computed.top) * viewportHeight / 100;
|
|
470
|
+
else record.stickyTop = parseFloat(computed.top);
|
|
471
|
+
}
|
|
472
|
+
});
|
|
473
|
+
return {
|
|
474
|
+
scroll,
|
|
475
|
+
viewportHeight,
|
|
476
|
+
documentHeight
|
|
477
|
+
};
|
|
478
|
+
};
|
|
479
|
+
const teardown = () => {
|
|
480
|
+
clearTable();
|
|
481
|
+
endGsapRefresh();
|
|
482
|
+
dropGsapClassesLater();
|
|
483
|
+
};
|
|
484
|
+
const rebuildDormancy = (scrollTriggerClass) => {
|
|
485
|
+
const live = liveClients();
|
|
486
|
+
if (!live.length) {
|
|
487
|
+
teardown();
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
if (scrollTriggerClass) adoptGsapClass(scrollTriggerClass);
|
|
491
|
+
if (!gsapRefreshing()) endGsapRefresh();
|
|
492
|
+
if (!gsapClasses.size || bodyScrolls()) {
|
|
493
|
+
clearTable();
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
const owned = new Map();
|
|
497
|
+
live.forEach(({ client, owned: elements }) => {
|
|
498
|
+
elements.forEach((el) => owned.set(el, client));
|
|
499
|
+
});
|
|
500
|
+
const autoOnly = live.every(({ client }) => client.mode === "auto");
|
|
501
|
+
const demoted = new Set(records.filter((r) => r.state !== "in").map((r) => r.el));
|
|
502
|
+
if (autoOnly && ownedDepth(owned.keys(), demoted) < AUTO_MIN_DEPTH) {
|
|
503
|
+
clearTable();
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
const sticky = scanStickyElements(owned.keys());
|
|
507
|
+
const stickySet = new Set(sticky);
|
|
508
|
+
const leftover = records.filter((r) => r.state !== "in" && !stickySet.has(r.el));
|
|
509
|
+
leftover.filter((r) => !r.el.isConnected).forEach((r) => promote(r));
|
|
510
|
+
const kept = leftover.filter((r) => r.el.isConnected);
|
|
511
|
+
const previous = new Map(records.map((r) => [r.el, r]));
|
|
512
|
+
const found = [...sticky.map((el) => {
|
|
513
|
+
const computed = getComputedStyle(el);
|
|
514
|
+
const before = previous.get(el);
|
|
515
|
+
if (before && before.state !== "in") return {
|
|
516
|
+
...before,
|
|
517
|
+
demotable: false,
|
|
518
|
+
position: el.style.position,
|
|
519
|
+
top: el.style.top,
|
|
520
|
+
stickyTop: parseFloat(el.style.top) || 0,
|
|
521
|
+
ancestors: [],
|
|
522
|
+
state: "before"
|
|
523
|
+
};
|
|
524
|
+
return {
|
|
525
|
+
el,
|
|
526
|
+
demotable: false,
|
|
527
|
+
position: el.style.position,
|
|
528
|
+
top: el.style.top,
|
|
529
|
+
zIndex: el.style.zIndex,
|
|
530
|
+
willChange: el.style.willChange,
|
|
531
|
+
zIndexAuto: computed.zIndex === "auto",
|
|
532
|
+
willChangeAuto: computed.willChange === "auto",
|
|
533
|
+
stickyTop: parseFloat(el.style.top) || 0,
|
|
534
|
+
naturalTop: 0,
|
|
535
|
+
travel: 0,
|
|
536
|
+
ancestors: [],
|
|
537
|
+
lo: 0,
|
|
538
|
+
hi: 0,
|
|
539
|
+
overflowBottom: 0,
|
|
540
|
+
holdsAfter: false,
|
|
541
|
+
state: "in",
|
|
542
|
+
demotedTop: ""
|
|
543
|
+
};
|
|
544
|
+
}), ...kept];
|
|
545
|
+
found.sort((a, b) => compareDocumentOrder(a.el, b.el));
|
|
546
|
+
found.forEach((record, index) => {
|
|
547
|
+
record.ancestors = found.slice(0, index).filter((a) => a.el.contains(record.el));
|
|
548
|
+
});
|
|
549
|
+
const holdsOwned = new Set();
|
|
550
|
+
found.forEach((record) => {
|
|
551
|
+
if (owned.has(record.el)) record.ancestors.forEach((a) => holdsOwned.add(a));
|
|
552
|
+
});
|
|
553
|
+
const all = found.filter((r) => owned.has(r.el) || holdsOwned.has(r) || r.state !== "in" || r.ancestors.some((a) => owned.has(a.el)));
|
|
554
|
+
const inTable = new Set(all);
|
|
555
|
+
all.forEach((record) => {
|
|
556
|
+
record.ancestors = record.ancestors.filter((a) => inTable.has(a));
|
|
557
|
+
});
|
|
558
|
+
const depths = new Map();
|
|
559
|
+
all.forEach((record) => {
|
|
560
|
+
const client = owned.get(record.el);
|
|
561
|
+
if (client) depths.set(client, Math.max(depths.get(client) ?? 0, record.ancestors.length + 1));
|
|
562
|
+
});
|
|
563
|
+
const active = new Set(live.map(({ client }) => client).filter((client) => client.mode === "always" || (depths.get(client) ?? 0) >= AUTO_MIN_DEPTH));
|
|
564
|
+
all.forEach((record) => {
|
|
565
|
+
const client = owned.get(record.el);
|
|
566
|
+
record.demotable = client !== void 0 && active.has(client);
|
|
567
|
+
});
|
|
568
|
+
records = all;
|
|
569
|
+
if (!all.some((r) => r.demotable)) {
|
|
570
|
+
clearTable();
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
const toMeasure = all.filter((r) => owned.has(r.el) || holdsOwned.has(r) || r.state !== "in");
|
|
574
|
+
const authorStyle = new Map(toMeasure.filter((r) => !owned.has(r.el) && r.state === "in").map(({ el }) => [el, el.getAttribute("style")]));
|
|
575
|
+
const restorePositions = () => toMeasure.forEach((record) => {
|
|
576
|
+
if (!authorStyle.has(record.el)) {
|
|
577
|
+
promote(record, true);
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
const saved = authorStyle.get(record.el);
|
|
581
|
+
if (saved === null || saved === void 0) record.el.removeAttribute("style");
|
|
582
|
+
else record.el.setAttribute("style", saved);
|
|
583
|
+
});
|
|
584
|
+
toMeasure.forEach(({ el }) => {
|
|
585
|
+
el.style.position = "static";
|
|
586
|
+
});
|
|
587
|
+
const unmodeled = new Set();
|
|
588
|
+
let measured;
|
|
589
|
+
try {
|
|
590
|
+
measured = measureStatic(toMeasure, owned, unmodeled);
|
|
591
|
+
} finally {
|
|
592
|
+
restorePositions();
|
|
593
|
+
}
|
|
594
|
+
const { scroll, viewportHeight, documentHeight } = measured;
|
|
595
|
+
const maxScroll = Math.max(0, documentHeight - viewportHeight);
|
|
596
|
+
all.forEach((record) => {
|
|
597
|
+
const union = mergeIntervals(record.ancestors.map((a) => [a.lo, a.hi]));
|
|
598
|
+
const reach = record.naturalTop - record.stickyTop;
|
|
599
|
+
record.lo = invertStuckUnion(union, reach, true);
|
|
600
|
+
record.hi = invertStuckUnion(union, reach + record.travel, false);
|
|
601
|
+
if (unmodeled.has(record) || record.ancestors.some((a) => unmodeled.has(a))) record.demotable = false;
|
|
602
|
+
const carried = record.ancestors.filter((a) => a.demotable && !a.holdsAfter).reduce((sum, a) => sum + a.travel, 0);
|
|
603
|
+
record.holdsAfter = record.overflowBottom + record.travel + carried > documentHeight + 1;
|
|
604
|
+
});
|
|
605
|
+
buffer = chooseBuffer(viewportHeight, maxScroll);
|
|
606
|
+
update(gsapPhase === "measuring" ? 0 : (gsapPhase === "restored" ? scrollBeforeGsapRefresh : null) ?? scroll);
|
|
607
|
+
listen(true);
|
|
608
|
+
};
|
|
609
|
+
const takeGsapRefreshRestore = () => adoptedMidRefresh && gsapRefreshing() ? onGsapRefreshInit().render : null;
|
|
610
|
+
const clearDormancy = clearTable;
|
|
611
|
+
const releaseDormant = (elements) => {
|
|
612
|
+
const released = new Set(elements);
|
|
613
|
+
records = records.filter((record) => {
|
|
614
|
+
if (!released.has(record.el)) return true;
|
|
615
|
+
promote(record);
|
|
616
|
+
return false;
|
|
617
|
+
});
|
|
618
|
+
};
|
|
619
|
+
const registerDormancyClient = (client) => {
|
|
620
|
+
clients.add(new WeakRef(client));
|
|
621
|
+
};
|
|
622
|
+
let rebuildQueued = false;
|
|
623
|
+
const queueRebuild = () => {
|
|
624
|
+
if (rebuildQueued) return;
|
|
625
|
+
rebuildQueued = true;
|
|
626
|
+
queueMicrotask(() => {
|
|
627
|
+
rebuildQueued = false;
|
|
628
|
+
rebuildSafely();
|
|
629
|
+
});
|
|
630
|
+
};
|
|
631
|
+
const unregisterDormancyClient = (client) => {
|
|
632
|
+
clients.forEach((ref) => {
|
|
633
|
+
if (ref.deref() !== client) return;
|
|
634
|
+
clients.delete(ref);
|
|
635
|
+
releaseDormant(client.ownedElements());
|
|
636
|
+
});
|
|
637
|
+
if (liveClients().length) queueRebuild();
|
|
638
|
+
else teardown();
|
|
639
|
+
};
|
|
203
640
|
const resolveMaybeFn = (value) => typeof value === "function" ? value() : value;
|
|
204
641
|
const KEYWORD_FRACTIONS = {
|
|
205
642
|
top: 0,
|
|
@@ -240,19 +677,20 @@ const assertNotClamped = (trimmed) => {
|
|
|
240
677
|
const inner = trimmed.slice(6).replace(/\)$/, "").trim();
|
|
241
678
|
throw new Error(`StickyScrollTrigger: unsupported position clause "${trimmed}": GSAP's clamp() wrapper isn't supported here.` + (inner ? ` Did you mean "${inner}"?` : ""));
|
|
242
679
|
};
|
|
243
|
-
const
|
|
680
|
+
const splitClause = (position) => {
|
|
244
681
|
const trimmed = position.trim();
|
|
245
682
|
assertNotClamped(trimmed);
|
|
246
|
-
|
|
247
|
-
return elementHeight * elementClause.fraction + elementClause.offsetPx;
|
|
683
|
+
return trimmed.split(/\s+/);
|
|
248
684
|
};
|
|
685
|
+
const anchorWithin = (token, size) => {
|
|
686
|
+
const { fraction, offsetPx } = parseClauseToken(token, size);
|
|
687
|
+
return size * fraction + offsetPx;
|
|
688
|
+
};
|
|
689
|
+
const resolveElementAnchor = (position, elementHeight) => anchorWithin(splitClause(position)[0], elementHeight);
|
|
249
690
|
const resolveElementFraction = (position) => resolveElementAnchor(position, 1) - resolveElementAnchor(position, 0);
|
|
250
691
|
const resolveAnchorTop = (position, elementHeight, viewportHeight) => {
|
|
251
|
-
const
|
|
252
|
-
|
|
253
|
-
const viewportToken = trimmed.split(/\s+/)[1] ?? "top";
|
|
254
|
-
const viewportClause = parseClauseToken(viewportToken, viewportHeight);
|
|
255
|
-
return viewportHeight * viewportClause.fraction + viewportClause.offsetPx - resolveElementAnchor(trimmed, elementHeight);
|
|
692
|
+
const [elementToken, viewportToken = "top"] = splitClause(position);
|
|
693
|
+
return anchorWithin(viewportToken, viewportHeight) - anchorWithin(elementToken, elementHeight);
|
|
256
694
|
};
|
|
257
695
|
const DWELL_RELATIVE_RE = /^\+=(\d+(?:\.\d*)?|\.\d+)(%|px)?$/;
|
|
258
696
|
const isDwellFormat = (resolved) => typeof resolved === "string" && DWELL_RELATIVE_RE.test(resolved.trim());
|
|
@@ -262,9 +700,7 @@ const prefixSpacedRelativeEnd = (start, endResolved) => {
|
|
|
262
700
|
if (!trimmedEnd.startsWith("+=") || !/\s/.test(trimmedEnd)) return endResolved;
|
|
263
701
|
const startResolved = resolveMaybeFn(start);
|
|
264
702
|
if (typeof startResolved !== "string") return endResolved;
|
|
265
|
-
const
|
|
266
|
-
assertNotClamped(trimmedStart);
|
|
267
|
-
const [elementToken] = trimmedStart.split(/\s+/);
|
|
703
|
+
const [elementToken] = splitClause(startResolved);
|
|
268
704
|
try {
|
|
269
705
|
parseClauseToken(elementToken, 0);
|
|
270
706
|
} catch (error) {
|
|
@@ -313,16 +749,18 @@ const classifyPosition = (resolved) => {
|
|
|
313
749
|
};
|
|
314
750
|
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));
|
|
315
751
|
const nestedIn = (measurements, index) => measurements.flatMap(({ triggerEnclosedBy }, k) => k !== index && triggerEnclosedBy.includes(index) ? [k] : []);
|
|
316
|
-
const anchorShare = (measurements, world,
|
|
752
|
+
const anchorShare = ({ measurements, world, order, ownIndex }, index, freezeStart, box, reachedAt) => {
|
|
753
|
+
const { enclosedBy, anchorElement } = box;
|
|
317
754
|
const measurement = measurements[index];
|
|
318
755
|
if (enclosedBy.includes(index)) return (measurement.start.mode === "absolute" ? world.unpaddedStart(index, ownIndex, freezeStart) : measurement.triggerTop - measurement.start.anchorOffset) < reachedAt - .05 ? 1 : 0;
|
|
319
756
|
if (order && isHeldIn(measurements, index, enclosedBy, anchorElement, order)) return 0;
|
|
320
757
|
if (box.nests.includes(index)) return box.fraction;
|
|
321
758
|
return measurement.triggerTop + measurement.triggerHeight <= box.top + .05 ? 1 : 0;
|
|
322
759
|
};
|
|
323
|
-
const gapsBeforeEndAnchor = (
|
|
760
|
+
const gapsBeforeEndAnchor = (context, windows, box, reachedAt, counted) => windows.reduce((total, { index, freezeStart, freezeEnd }) => {
|
|
761
|
+
const { ownIndex, order } = context;
|
|
324
762
|
if (index === ownIndex || order && order.isBefore(ownIndex, index)) return total;
|
|
325
|
-
const weight = anchorShare(
|
|
763
|
+
const weight = anchorShare(context, index, freezeStart, box, reachedAt);
|
|
326
764
|
if (weight === 0) return total;
|
|
327
765
|
counted?.add(index);
|
|
328
766
|
return total + weight * (freezeEnd - freezeStart);
|
|
@@ -386,8 +824,64 @@ const runPass = (measurements, { viewportHeight, measureLiveEndTriggerTop }, pre
|
|
|
386
824
|
runs: []
|
|
387
825
|
};
|
|
388
826
|
recounts.push(recount);
|
|
827
|
+
const context = {
|
|
828
|
+
measurements,
|
|
829
|
+
world,
|
|
830
|
+
order,
|
|
831
|
+
ownIndex: index
|
|
832
|
+
};
|
|
833
|
+
const gapsOf = (box, reachedAt) => {
|
|
834
|
+
const gaps = (w, counted) => gapsBeforeEndAnchor(context, w, box, reachedAt, counted);
|
|
835
|
+
recount.runs.push((w) => gaps(w, recount.counted));
|
|
836
|
+
return gaps(knownWindows());
|
|
837
|
+
};
|
|
389
838
|
const { start } = measurement;
|
|
390
839
|
const freezeStart = start.mode === "absolute" ? start.value : previous?.freezeStarts[index] ?? clauseFreezeStart(measurements, index, paddingOf);
|
|
840
|
+
const heldBack = (k) => order !== null && (order.isBefore(index, k) || isHeldIn(measurements, k, measurement.triggerEnclosedBy, index, order));
|
|
841
|
+
const selfClauseEnd = (end, anchorOffsetEnd) => {
|
|
842
|
+
const nests = nestedIn(measurements, index);
|
|
843
|
+
const weight = resolveElementFraction(end.clause) - (start.mode === "clause" ? start.elementFraction : 0);
|
|
844
|
+
const dwellInside = measurements.reduce((total, other, k) => {
|
|
845
|
+
if (weight === 0 || other.kind !== "scene" || !nests.includes(k) || heldBack(k)) return total;
|
|
846
|
+
recount.counted.add(k);
|
|
847
|
+
return total + weight * paddingOf(k);
|
|
848
|
+
}, 0);
|
|
849
|
+
if (start.mode === "clause") return freezeStart + Math.max(0, start.anchorOffset - anchorOffsetEnd + dwellInside);
|
|
850
|
+
const dwellAbove = measurements.reduce((total, other, k) => {
|
|
851
|
+
if (k === index || other.kind !== "scene" || nests.includes(k) || other.triggerTop + other.triggerHeight > measurement.triggerTop + .05 || heldBack(k)) return total;
|
|
852
|
+
recount.counted.add(k);
|
|
853
|
+
return total + paddingOf(k);
|
|
854
|
+
}, 0);
|
|
855
|
+
return Math.max(freezeStart, measurement.triggerTop - anchorOffsetEnd + dwellAbove + dwellInside);
|
|
856
|
+
};
|
|
857
|
+
const elementClauseEnd = (end, anchorOffsetEnd) => {
|
|
858
|
+
let endTop;
|
|
859
|
+
if (end.measureLive) endTop = measureLiveEndTriggerTop(index);
|
|
860
|
+
else {
|
|
861
|
+
const rawTop = measurement.endTrigger.index === null ? end.rawTop : measurements[measurement.endTrigger.index].triggerTop;
|
|
862
|
+
if (rawTop === null) endTop = 0;
|
|
863
|
+
else {
|
|
864
|
+
const box = {
|
|
865
|
+
top: rawTop,
|
|
866
|
+
fraction: resolveElementFraction(end.clause),
|
|
867
|
+
nests: measurement.endTrigger.nests,
|
|
868
|
+
enclosedBy: measurement.endTrigger.enclosedBy,
|
|
869
|
+
anchorElement: measurement.endTrigger.index
|
|
870
|
+
};
|
|
871
|
+
endTop = rawTop + gapsOf(box, rawTop - anchorOffsetEnd);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
let layoutStart;
|
|
875
|
+
if (start.mode === "absolute") layoutStart = start.value;
|
|
876
|
+
else layoutStart = measurement.triggerTop - start.anchorOffset + gapsOf({
|
|
877
|
+
top: measurement.triggerTop,
|
|
878
|
+
fraction: start.elementFraction,
|
|
879
|
+
nests: nestedIn(measurements, index),
|
|
880
|
+
enclosedBy: measurement.triggerEnclosedBy,
|
|
881
|
+
anchorElement: index
|
|
882
|
+
}, measurement.triggerTop - start.anchorOffset);
|
|
883
|
+
return freezeStart + Math.max(0, endTop - anchorOffsetEnd - layoutStart);
|
|
884
|
+
};
|
|
391
885
|
let freezeEnd;
|
|
392
886
|
switch (measurement.end.mode) {
|
|
393
887
|
case "dwell":
|
|
@@ -401,55 +895,8 @@ const runPass = (measurements, { viewportHeight, measureLiveEndTriggerTop }, pre
|
|
|
401
895
|
case "auto":
|
|
402
896
|
case "max": throw new Error(`StickyScrollTrigger: internal error: a Scene layer reached '${measurement.end.mode}' end mode, which only a cover layer can have.`);
|
|
403
897
|
case "clause": {
|
|
404
|
-
const anchorOffsetEnd = resolveAnchorTop(measurement.end.clause, measurement.
|
|
405
|
-
|
|
406
|
-
const nests = nestedIn(measurements, index);
|
|
407
|
-
const weight = resolveElementFraction(measurement.end.clause) - (start.mode === "clause" ? start.elementFraction : 0);
|
|
408
|
-
const dwellInside = measurements.reduce((total, other, k) => {
|
|
409
|
-
if (weight === 0 || other.kind !== "scene" || !nests.includes(k) || order && (order.isBefore(index, k) || isHeldIn(measurements, k, measurement.triggerEnclosedBy, index, order))) return total;
|
|
410
|
-
recount.counted.add(k);
|
|
411
|
-
return total + weight * paddingOf(k);
|
|
412
|
-
}, 0);
|
|
413
|
-
if (start.mode === "clause") {
|
|
414
|
-
freezeEnd = freezeStart + Math.max(0, start.anchorOffset - anchorOffsetEnd + dwellInside);
|
|
415
|
-
break;
|
|
416
|
-
}
|
|
417
|
-
const dwellAbove = measurements.reduce((total, other, k) => {
|
|
418
|
-
if (k === index || other.kind !== "scene" || nests.includes(k) || other.triggerTop + other.triggerHeight > measurement.triggerTop + .05 || order && (order.isBefore(index, k) || isHeldIn(measurements, k, measurement.triggerEnclosedBy, index, order))) return total;
|
|
419
|
-
recount.counted.add(k);
|
|
420
|
-
return total + paddingOf(k);
|
|
421
|
-
}, 0);
|
|
422
|
-
freezeEnd = Math.max(freezeStart, measurement.triggerTop - anchorOffsetEnd + dwellAbove + dwellInside);
|
|
423
|
-
break;
|
|
424
|
-
}
|
|
425
|
-
let endTop;
|
|
426
|
-
if (measurement.end.measureLive) endTop = measureLiveEndTriggerTop(index);
|
|
427
|
-
else {
|
|
428
|
-
const rawTop = measurement.endTriggerIndex === null ? measurement.end.rawTop : measurements[measurement.endTriggerIndex].triggerTop;
|
|
429
|
-
if (rawTop === null) endTop = 0;
|
|
430
|
-
else {
|
|
431
|
-
const box = {
|
|
432
|
-
top: rawTop,
|
|
433
|
-
fraction: resolveElementFraction(measurement.end.clause),
|
|
434
|
-
nests: measurement.endTriggerNests
|
|
435
|
-
};
|
|
436
|
-
const gaps = (w, counted) => gapsBeforeEndAnchor(measurements, world, w, box, rawTop - anchorOffsetEnd, measurement.endTriggerEnclosedBy, index, measurement.endTriggerIndex, order, counted);
|
|
437
|
-
endTop = rawTop + gaps(knownWindows());
|
|
438
|
-
recount.runs.push((w) => gaps(w, recount.counted));
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
let layoutStart;
|
|
442
|
-
if (start.mode === "absolute") layoutStart = start.value;
|
|
443
|
-
else {
|
|
444
|
-
const gaps = (w, counted) => gapsBeforeEndAnchor(measurements, world, w, {
|
|
445
|
-
top: measurement.triggerTop,
|
|
446
|
-
fraction: start.elementFraction,
|
|
447
|
-
nests: nestedIn(measurements, index)
|
|
448
|
-
}, measurement.triggerTop - start.anchorOffset, measurement.triggerEnclosedBy, index, index, order, counted);
|
|
449
|
-
layoutStart = measurement.triggerTop - start.anchorOffset + gaps(knownWindows());
|
|
450
|
-
recount.runs.push((w) => gaps(w, recount.counted));
|
|
451
|
-
}
|
|
452
|
-
freezeEnd = freezeStart + Math.max(0, endTop - anchorOffsetEnd - layoutStart);
|
|
898
|
+
const anchorOffsetEnd = resolveAnchorTop(measurement.end.clause, measurement.endTrigger.height, viewportHeight);
|
|
899
|
+
freezeEnd = measurement.endTrigger.isSelf ? selfClauseEnd(measurement.end, anchorOffsetEnd) : elementClauseEnd(measurement.end, anchorOffsetEnd);
|
|
453
900
|
break;
|
|
454
901
|
}
|
|
455
902
|
}
|
|
@@ -491,9 +938,9 @@ const planCover = (measurements, index, windows, { viewportHeight, documentMaxSc
|
|
|
491
938
|
freezeEnd = documentMaxScroll + measurement.end.offsetPx;
|
|
492
939
|
break;
|
|
493
940
|
case "clause": {
|
|
494
|
-
const anchorOffsetEnd = resolveAnchorTop(measurement.end.clause, measurement.
|
|
941
|
+
const anchorOffsetEnd = resolveAnchorTop(measurement.end.clause, measurement.endTrigger.height, viewportHeight);
|
|
495
942
|
if (measurement.end.measureLive) freezeEnd = measureLiveEndTriggerTop(index) - anchorOffsetEnd;
|
|
496
|
-
else freezeEnd = reached((measurement.
|
|
943
|
+
else freezeEnd = reached((measurement.endTrigger.isSelf ? measurement.triggerTop : measurement.endTrigger.index === null ? measurement.end.rawTop ?? 0 : measurements[measurement.endTrigger.index].triggerTop) - anchorOffsetEnd);
|
|
497
944
|
break;
|
|
498
945
|
}
|
|
499
946
|
}
|
|
@@ -564,7 +1011,7 @@ const refreshOrderEdges = (measurements, lengths, edges) => {
|
|
|
564
1011
|
const before = measurements.map(() => []);
|
|
565
1012
|
edges.forEach((movers, i) => {
|
|
566
1013
|
const measurement = measurements[i];
|
|
567
|
-
const enclosingAnchor = (measurement.end.mode === "clause" && !measurement.
|
|
1014
|
+
const enclosingAnchor = (measurement.end.mode === "clause" && !measurement.endTrigger.isSelf ? measurement.endTrigger.enclosedBy : measurement.triggerEnclosedBy).filter((m) => m !== measurement.endTrigger.index);
|
|
568
1015
|
movers.forEach((k) => {
|
|
569
1016
|
if (lengths[k] <= .05) return;
|
|
570
1017
|
before[i].push(k);
|
|
@@ -821,7 +1268,7 @@ const createScrollMarginSync = (rootElement, targetSelector) => {
|
|
|
821
1268
|
const snapshot = snapshots.get(target);
|
|
822
1269
|
target.style.scrollMarginTop = snapshot ? snapshot.inline : "";
|
|
823
1270
|
};
|
|
824
|
-
const
|
|
1271
|
+
const stopRamps = () => {
|
|
825
1272
|
if (sheet) {
|
|
826
1273
|
document.adoptedStyleSheets = document.adoptedStyleSheets.filter((one) => one !== sheet);
|
|
827
1274
|
sheet = null;
|
|
@@ -831,31 +1278,36 @@ const createScrollMarginSync = (rootElement, targetSelector) => {
|
|
|
831
1278
|
jsRamps = [];
|
|
832
1279
|
jsHost = null;
|
|
833
1280
|
};
|
|
1281
|
+
const startRamps = (ramps, host) => {
|
|
1282
|
+
if (animationHost !== host) {
|
|
1283
|
+
animationHost?.removeAttribute(`data-${instanceId}`);
|
|
1284
|
+
animationHost = host;
|
|
1285
|
+
animationHost.setAttribute(`data-${instanceId}`, "");
|
|
1286
|
+
}
|
|
1287
|
+
if (usesJsRamp) {
|
|
1288
|
+
jsRamps = ramps;
|
|
1289
|
+
jsHost = host;
|
|
1290
|
+
return;
|
|
1291
|
+
}
|
|
1292
|
+
if (!sheet) {
|
|
1293
|
+
sheet = new CSSStyleSheet();
|
|
1294
|
+
document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet];
|
|
1295
|
+
}
|
|
1296
|
+
sheet.replaceSync(buildStylesheet(instanceId, ramps));
|
|
1297
|
+
};
|
|
1298
|
+
const release = () => {
|
|
1299
|
+
written.forEach(restoreTarget);
|
|
1300
|
+
written = new Set();
|
|
1301
|
+
stopRamps();
|
|
1302
|
+
};
|
|
834
1303
|
return {
|
|
835
1304
|
sync(scenes, host, measureNaturalTops) {
|
|
836
1305
|
const ramps = scenes.filter((scene) => scene.freezeEnd > scene.freezeStart).sort((a, b) => a.freezeStart - b.freezeStart);
|
|
837
1306
|
if (targetSelector === null || !ramps.length || !host) {
|
|
838
|
-
|
|
839
|
-
written = new Set();
|
|
840
|
-
clearAnimation();
|
|
1307
|
+
release();
|
|
841
1308
|
return;
|
|
842
1309
|
}
|
|
843
|
-
|
|
844
|
-
animationHost?.removeAttribute(`data-${instanceId}`);
|
|
845
|
-
animationHost = host;
|
|
846
|
-
animationHost.setAttribute(`data-${instanceId}`, "");
|
|
847
|
-
}
|
|
848
|
-
if (usesJsRamp) {
|
|
849
|
-
jsRamps = ramps;
|
|
850
|
-
jsHost = host;
|
|
851
|
-
applyJsRamp();
|
|
852
|
-
} else {
|
|
853
|
-
if (!sheet) {
|
|
854
|
-
sheet = new CSSStyleSheet();
|
|
855
|
-
document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet];
|
|
856
|
-
}
|
|
857
|
-
sheet.replaceSync(buildStylesheet(instanceId, ramps));
|
|
858
|
-
}
|
|
1310
|
+
startRamps(ramps, host);
|
|
859
1311
|
const targets = Array.from(rootElement.querySelectorAll(targetSelector));
|
|
860
1312
|
const naturalTops = measureNaturalTops(targets);
|
|
861
1313
|
targets.forEach((target) => {
|
|
@@ -880,11 +1332,12 @@ const createScrollMarginSync = (rootElement, targetSelector) => {
|
|
|
880
1332
|
});
|
|
881
1333
|
written = next;
|
|
882
1334
|
},
|
|
1335
|
+
applyRamps() {
|
|
1336
|
+
applyJsRamp();
|
|
1337
|
+
},
|
|
883
1338
|
restore() {
|
|
884
1339
|
if (usesJsRamp) window.removeEventListener("scroll", applyJsRamp);
|
|
885
|
-
|
|
886
|
-
written = new Set();
|
|
887
|
-
clearAnimation();
|
|
1340
|
+
release();
|
|
888
1341
|
}
|
|
889
1342
|
};
|
|
890
1343
|
};
|
|
@@ -911,6 +1364,11 @@ const buildStructure = (rootElement, layers) => {
|
|
|
911
1364
|
}
|
|
912
1365
|
return outermost;
|
|
913
1366
|
};
|
|
1367
|
+
const unbuildCover = (layer) => {
|
|
1368
|
+
if (!layer.wrapper) return;
|
|
1369
|
+
unwrapCover(layer.wrapper);
|
|
1370
|
+
layer.wrapper = null;
|
|
1371
|
+
};
|
|
914
1372
|
const unbuildStructure = (rootElement, layers, outermostContainer) => {
|
|
915
1373
|
if (outermostContainer) {
|
|
916
1374
|
outermostContainer.parentNode?.insertBefore(rootElement, outermostContainer);
|
|
@@ -918,15 +1376,13 @@ const unbuildStructure = (rootElement, layers, outermostContainer) => {
|
|
|
918
1376
|
}
|
|
919
1377
|
for (let i = layers.length - 1; i >= 0; i -= 1) {
|
|
920
1378
|
const layer = layers[i];
|
|
921
|
-
if (layer.kind === "cover"
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
layer.wrapper = null;
|
|
925
|
-
if (layer.kind === "scene") {
|
|
1379
|
+
if (layer.kind === "cover") unbuildCover(layer);
|
|
1380
|
+
else {
|
|
1381
|
+
layer.wrapper = null;
|
|
926
1382
|
layer.container = null;
|
|
927
1383
|
layer.padding = null;
|
|
928
1384
|
}
|
|
929
|
-
}
|
|
1385
|
+
}
|
|
930
1386
|
};
|
|
931
1387
|
const resolveEndSpec = (rootElement, viewportHeight, layer, endResolved, ownIndex, endTriggerIndex) => {
|
|
932
1388
|
if (endResolved === null) return { mode: "auto" };
|
|
@@ -983,23 +1439,27 @@ const measureLayer = (rootElement, viewportHeight, layer, ownIndex, indexByTrigg
|
|
|
983
1439
|
const endResolved = layer.end === null ? null : prefixSpacedRelativeEnd(startResolved, resolveMaybeFn(layer.end));
|
|
984
1440
|
const elementHeight = measureUsedHeight(layer.trigger);
|
|
985
1441
|
const endTriggerIsSelf = layer.endTrigger === layer.trigger;
|
|
1442
|
+
const indicesWhere = (test) => [...indexByTrigger].filter(([trigger]) => test(trigger)).map(([, index]) => index);
|
|
986
1443
|
const endTriggerIndex = indexByTrigger.get(layer.endTrigger) ?? null;
|
|
987
1444
|
const end = resolveEndSpec(rootElement, viewportHeight, layer, endResolved, ownIndex, endTriggerIndex);
|
|
1445
|
+
const endTriggerInContainer = end.mode === "clause" && !end.measureLive && !endTriggerIsSelf;
|
|
988
1446
|
const start = resolveStartSpec(layer, startResolved, elementHeight, viewportHeight);
|
|
989
1447
|
return {
|
|
990
1448
|
kind: layer.kind,
|
|
991
1449
|
start,
|
|
992
1450
|
triggerTop: documentTop(layer.trigger),
|
|
993
1451
|
triggerHeight: elementHeight,
|
|
994
|
-
triggerEnclosedBy:
|
|
1452
|
+
triggerEnclosedBy: indicesWhere((trigger) => trigger.contains(layer.trigger)),
|
|
995
1453
|
wrapperTop: layer.wrapper ? documentTop(layer.wrapper) : 0,
|
|
996
1454
|
coverTop: layer.kind === "cover" ? documentTop(layer.cover) : 0,
|
|
997
1455
|
end,
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1456
|
+
endTrigger: {
|
|
1457
|
+
isSelf: endTriggerIsSelf,
|
|
1458
|
+
index: endTriggerIndex,
|
|
1459
|
+
height: end.mode === "clause" ? measureUsedHeight(layer.endTrigger) : 0,
|
|
1460
|
+
enclosedBy: endTriggerInContainer ? indicesWhere((trigger) => trigger.contains(layer.endTrigger)) : [],
|
|
1461
|
+
nests: endTriggerInContainer ? indicesWhere((other) => other !== layer.endTrigger && layer.endTrigger.contains(other)) : []
|
|
1462
|
+
}
|
|
1003
1463
|
};
|
|
1004
1464
|
};
|
|
1005
1465
|
const EXCLUDED_VAR_KEYS = [
|
|
@@ -1032,20 +1492,51 @@ const resolvePinReleaseScroll = (endTrigger, classifiedEnd, engageScroll, viewpo
|
|
|
1032
1492
|
case "clause": return toScroll(endTrigger, documentTop(endTrigger) - resolveAnchorTop(classifiedEnd.value, measureUsedHeight(endTrigger), viewportHeight));
|
|
1033
1493
|
}
|
|
1034
1494
|
};
|
|
1495
|
+
const removeItem = (list, item) => {
|
|
1496
|
+
const index = list.indexOf(item);
|
|
1497
|
+
if (index !== -1) list.splice(index, 1);
|
|
1498
|
+
};
|
|
1035
1499
|
const hasDuplicateTrigger = (list, trigger) => list.some((existing) => existing.trigger === trigger);
|
|
1500
|
+
const pendingDisarms = new Set();
|
|
1501
|
+
const pendingKeeps = new Set();
|
|
1502
|
+
let unbuildCount = 0;
|
|
1503
|
+
const REST_POLL_MS = 100;
|
|
1504
|
+
const runEach = (...steps) => {
|
|
1505
|
+
let failure = null;
|
|
1506
|
+
steps.forEach((step) => {
|
|
1507
|
+
try {
|
|
1508
|
+
step();
|
|
1509
|
+
} catch (error) {
|
|
1510
|
+
if (!failure) failure = { error };
|
|
1511
|
+
else queueMicrotask(() => {
|
|
1512
|
+
throw error;
|
|
1513
|
+
});
|
|
1514
|
+
}
|
|
1515
|
+
});
|
|
1516
|
+
if (failure) throw failure.error;
|
|
1517
|
+
};
|
|
1036
1518
|
var StickyScrollTrigger = class {
|
|
1037
1519
|
#rootElement;
|
|
1038
1520
|
#layers = [];
|
|
1039
1521
|
#builtLayers = [];
|
|
1522
|
+
#measuredStructure = null;
|
|
1040
1523
|
#coverRestoreByLayer = new WeakMap();
|
|
1041
1524
|
#built = false;
|
|
1042
1525
|
#dirty = false;
|
|
1043
1526
|
#outermostContainer = null;
|
|
1044
1527
|
#destroyed = false;
|
|
1045
1528
|
#rebuildScheduled = false;
|
|
1529
|
+
#keepContentPending = false;
|
|
1530
|
+
#deferredRun = null;
|
|
1531
|
+
#restTimer = null;
|
|
1532
|
+
#pendingKeep = null;
|
|
1046
1533
|
#scrollTriggerClass = null;
|
|
1047
1534
|
#pinLayers = [];
|
|
1048
|
-
#scrollMarginSync;
|
|
1535
|
+
#scrollMarginSync = null;
|
|
1536
|
+
#scrollMarginTargets;
|
|
1537
|
+
#armed = false;
|
|
1538
|
+
#dispatchObserver = null;
|
|
1539
|
+
#dormancyClient = null;
|
|
1049
1540
|
constructor(root, options = {}) {
|
|
1050
1541
|
this.#rootElement = resolveRoot(root);
|
|
1051
1542
|
const scrollMarginTargets = options.scrollMarginTargets === void 0 ? "[id]" : options.scrollMarginTargets;
|
|
@@ -1054,11 +1545,119 @@ var StickyScrollTrigger = class {
|
|
|
1054
1545
|
} catch {
|
|
1055
1546
|
throw new Error(`StickyScrollTrigger: scrollMarginTargets "${scrollMarginTargets}" is not a valid CSS selector.`);
|
|
1056
1547
|
}
|
|
1057
|
-
|
|
1548
|
+
const optimization = options.nestedStickyOptimization === void 0 ? "auto" : options.nestedStickyOptimization;
|
|
1549
|
+
if (![
|
|
1550
|
+
"auto",
|
|
1551
|
+
"always",
|
|
1552
|
+
"off"
|
|
1553
|
+
].includes(optimization)) throw new Error(`StickyScrollTrigger: nestedStickyOptimization must be 'auto', 'always' or 'off', got ${String(optimization)}.`);
|
|
1554
|
+
this.#scrollMarginTargets = scrollMarginTargets;
|
|
1555
|
+
if (optimization === "always" || optimization === "auto" && isWebKit()) this.#dormancyClient = {
|
|
1556
|
+
mode: optimization,
|
|
1557
|
+
ownedElements: () => this.#ownedStickyElements()
|
|
1558
|
+
};
|
|
1559
|
+
}
|
|
1560
|
+
#arm() {
|
|
1561
|
+
if (pendingDisarms.delete(this)) this.#keepContentPending = false;
|
|
1562
|
+
if (this.#armed) return;
|
|
1563
|
+
this.#armed = true;
|
|
1564
|
+
pendingKeeps.forEach((keep) => {
|
|
1565
|
+
if (keep.owner.#overlaps(this)) keep.cancelled = true;
|
|
1566
|
+
});
|
|
1567
|
+
this.#scrollMarginSync = createScrollMarginSync(this.#rootElement, this.#scrollMarginTargets);
|
|
1568
|
+
if (this.#dormancyClient) registerDormancyClient(this.#dormancyClient);
|
|
1569
|
+
}
|
|
1570
|
+
#disarmIfEmpty() {
|
|
1571
|
+
if (this.#armed && !this.#destroyed && !this.#layers.length && !this.#pinLayers.length) {
|
|
1572
|
+
if (pendingDisarms.has(this)) return;
|
|
1573
|
+
if (!this.#scrollTriggerClass?.isRefreshing && this.#scrolling()) {
|
|
1574
|
+
pendingDisarms.add(this);
|
|
1575
|
+
queueMicrotask(() => this.#settleWhenAtRest());
|
|
1576
|
+
return;
|
|
1577
|
+
}
|
|
1578
|
+
this.#disarmKeepingContent(true, true);
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
#flushOverlappingDisarms() {
|
|
1582
|
+
if (this.#destroyed) return;
|
|
1583
|
+
pendingDisarms.forEach((other) => {
|
|
1584
|
+
if (other !== this && other.#overlaps(this)) other.#flushPendingDisarmForRevert();
|
|
1585
|
+
});
|
|
1586
|
+
}
|
|
1587
|
+
#scrolling() {
|
|
1588
|
+
const cls = this.#scrollTriggerClass;
|
|
1589
|
+
return Boolean(cls?.isScrolling?.() || cls?.isUpdating === true);
|
|
1590
|
+
}
|
|
1591
|
+
#disarmKeepingContent(keepContent, onlyAtRest = false) {
|
|
1592
|
+
const position = keepContent && this.#built ? this.#naturalScrollTop(this.#builtLayers) : null;
|
|
1593
|
+
this.#keepContentPending = false;
|
|
1594
|
+
this.#disarm();
|
|
1595
|
+
if (!position) return;
|
|
1596
|
+
if (this.#gsapScrollRecord()) {
|
|
1597
|
+
this.#keepContent(position);
|
|
1598
|
+
return;
|
|
1599
|
+
}
|
|
1600
|
+
if (this.#pendingKeep) pendingKeeps.delete(this.#pendingKeep);
|
|
1601
|
+
this.#pendingKeep = {
|
|
1602
|
+
owner: this,
|
|
1603
|
+
position,
|
|
1604
|
+
cls: this.#scrollTriggerClass,
|
|
1605
|
+
onlyAtRest,
|
|
1606
|
+
cancelled: false
|
|
1607
|
+
};
|
|
1608
|
+
pendingKeeps.add(this.#pendingKeep);
|
|
1609
|
+
queueMicrotask(() => this.#applyPendingKeep());
|
|
1610
|
+
}
|
|
1611
|
+
#applyPendingKeep() {
|
|
1612
|
+
const keep = this.#pendingKeep;
|
|
1613
|
+
if (!keep) return;
|
|
1614
|
+
this.#pendingKeep = null;
|
|
1615
|
+
pendingKeeps.delete(keep);
|
|
1616
|
+
if (keep.cancelled || keep.onlyAtRest && keep.cls?.isScrolling?.()) return;
|
|
1617
|
+
this.#keepContent(keep.position, keep.cls);
|
|
1618
|
+
}
|
|
1619
|
+
#overlaps(other) {
|
|
1620
|
+
return this.#rootElement.contains(other.#rootElement) || other.#rootElement.contains(this.#rootElement);
|
|
1621
|
+
}
|
|
1622
|
+
#settleWhenAtRest() {
|
|
1623
|
+
if (this.#destroyed || this.#restTimer !== null) return;
|
|
1624
|
+
if (this.#scrollTriggerClass?.isScrolling?.()) {
|
|
1625
|
+
this.#restTimer = setTimeout(() => {
|
|
1626
|
+
this.#restTimer = null;
|
|
1627
|
+
this.#settleWhenAtRest();
|
|
1628
|
+
}, REST_POLL_MS);
|
|
1629
|
+
return;
|
|
1630
|
+
}
|
|
1631
|
+
const run = this.#deferredRun;
|
|
1632
|
+
this.#deferredRun = null;
|
|
1633
|
+
runEach(() => this.#flushPendingDisarm(), () => run?.());
|
|
1634
|
+
}
|
|
1635
|
+
#flushPendingDisarm(keepContent = true, onlyAtRest = false) {
|
|
1636
|
+
if (pendingDisarms.delete(this) && !this.#destroyed && !this.#layers.length && !this.#pinLayers.length) this.#disarmKeepingContent(keepContent, onlyAtRest);
|
|
1637
|
+
}
|
|
1638
|
+
#flushPendingDisarmForRevert() {
|
|
1639
|
+
this.#flushPendingDisarm(!this.#scrollTriggerClass?.isScrolling?.(), true);
|
|
1640
|
+
}
|
|
1641
|
+
#disarm() {
|
|
1642
|
+
this.#armed = false;
|
|
1643
|
+
unregisterDormancyClient(this.#dormancyClient);
|
|
1644
|
+
this.#scrollMarginSync?.restore();
|
|
1645
|
+
this.#scrollMarginSync = null;
|
|
1646
|
+
this.#restoreCoverLifts();
|
|
1647
|
+
this.#unbuild();
|
|
1648
|
+
this.#builtLayers = [];
|
|
1649
|
+
}
|
|
1650
|
+
#ownedStickyElements() {
|
|
1651
|
+
return [...this.#builtLayers.flatMap((layer) => layer.wrapper ? [layer.wrapper] : []), ...this.#wrappedPinTriggers()];
|
|
1652
|
+
}
|
|
1653
|
+
#wrappedPinTriggers() {
|
|
1654
|
+
return this.#pinLayers.flatMap(({ trigger, inner }) => inner ? [trigger] : []);
|
|
1058
1655
|
}
|
|
1059
1656
|
#unbuild() {
|
|
1060
1657
|
if (!this.#built) return;
|
|
1061
1658
|
unbuildStructure(this.#rootElement, this.#builtLayers, this.#outermostContainer);
|
|
1659
|
+
unbuildCount += 1;
|
|
1660
|
+
this.#measuredStructure = null;
|
|
1062
1661
|
this.#outermostContainer = null;
|
|
1063
1662
|
this.#built = false;
|
|
1064
1663
|
}
|
|
@@ -1068,14 +1667,48 @@ var StickyScrollTrigger = class {
|
|
|
1068
1667
|
this.#built = true;
|
|
1069
1668
|
this.#dirty = false;
|
|
1070
1669
|
}
|
|
1670
|
+
#catchUp() {
|
|
1671
|
+
this.#flushPendingDisarm();
|
|
1672
|
+
this.refresh();
|
|
1673
|
+
this.#applyPendingKeep();
|
|
1674
|
+
}
|
|
1071
1675
|
#scheduleRebuild() {
|
|
1072
|
-
if (this.#
|
|
1676
|
+
if (this.#destroyed) return;
|
|
1677
|
+
if (this.#built) this.#keepContentPending = true;
|
|
1678
|
+
if (this.#rebuildScheduled) return;
|
|
1073
1679
|
this.#rebuildScheduled = true;
|
|
1074
|
-
|
|
1680
|
+
const cls = this.#scrollTriggerClass;
|
|
1681
|
+
const onRefreshInit = () => {
|
|
1682
|
+
if (!this.#rebuildScheduled || this.#destroyed) return void 0;
|
|
1683
|
+
try {
|
|
1684
|
+
this.#catchUp();
|
|
1685
|
+
} catch (error) {
|
|
1686
|
+
queueMicrotask(() => {
|
|
1687
|
+
throw error;
|
|
1688
|
+
});
|
|
1689
|
+
} finally {
|
|
1690
|
+
this.#rebuildScheduled = false;
|
|
1691
|
+
}
|
|
1692
|
+
const restoreDormancy = takeGsapRefreshRestore();
|
|
1693
|
+
return restoreDormancy ? { render: () => restoreDormancy() } : void 0;
|
|
1694
|
+
};
|
|
1695
|
+
cls?.addEventListener?.("refreshInit", onRefreshInit);
|
|
1696
|
+
const run = () => {
|
|
1697
|
+
cls?.removeEventListener?.("refreshInit", onRefreshInit);
|
|
1698
|
+
if (!this.#rebuildScheduled) return;
|
|
1075
1699
|
this.#rebuildScheduled = false;
|
|
1076
1700
|
if (this.#destroyed) return;
|
|
1077
|
-
this
|
|
1701
|
+
this.#catchUp();
|
|
1078
1702
|
if (typeof this.#scrollTriggerClass?.refresh === "function") this.#scrollTriggerClass.refresh();
|
|
1703
|
+
};
|
|
1704
|
+
queueMicrotask(() => {
|
|
1705
|
+
if (this.#rebuildScheduled && !this.#destroyed && cls?.isScrolling?.()) {
|
|
1706
|
+
const earlier = this.#deferredRun;
|
|
1707
|
+
this.#deferredRun = earlier ? () => runEach(earlier, run) : run;
|
|
1708
|
+
this.#settleWhenAtRest();
|
|
1709
|
+
return;
|
|
1710
|
+
}
|
|
1711
|
+
run();
|
|
1079
1712
|
});
|
|
1080
1713
|
}
|
|
1081
1714
|
#captureScrollTriggerClass(self) {
|
|
@@ -1086,16 +1719,46 @@ var StickyScrollTrigger = class {
|
|
|
1086
1719
|
return (self) => {
|
|
1087
1720
|
this.#captureScrollTriggerClass(self);
|
|
1088
1721
|
const result = userOnRefreshInit?.(self);
|
|
1722
|
+
const inGsapRefresh = this.#scrollTriggerClass?.isRefreshing === true;
|
|
1723
|
+
const unchanged = inGsapRefresh && !userOnRefreshInit && this.#dispatchObserver !== null && this.#dispatchObserver.takeRecords().length === 0;
|
|
1089
1724
|
try {
|
|
1090
|
-
this.refresh();
|
|
1725
|
+
if (!unchanged) this.refresh();
|
|
1091
1726
|
} catch (error) {
|
|
1092
1727
|
queueMicrotask(() => {
|
|
1093
1728
|
throw error;
|
|
1094
1729
|
});
|
|
1095
1730
|
}
|
|
1096
|
-
|
|
1731
|
+
if (inGsapRefresh) this.#watchGsapRefresh();
|
|
1732
|
+
const restoreDormancy = takeGsapRefreshRestore();
|
|
1733
|
+
if (!restoreDormancy) return result;
|
|
1734
|
+
return { render: (time) => {
|
|
1735
|
+
result?.render?.(time);
|
|
1736
|
+
restoreDormancy();
|
|
1737
|
+
} };
|
|
1097
1738
|
};
|
|
1098
1739
|
}
|
|
1740
|
+
#watchGsapRefresh() {
|
|
1741
|
+
if (this.#dispatchObserver) {
|
|
1742
|
+
this.#dispatchObserver.takeRecords();
|
|
1743
|
+
return;
|
|
1744
|
+
}
|
|
1745
|
+
const observer = new MutationObserver(() => {});
|
|
1746
|
+
const cls = this.#scrollTriggerClass;
|
|
1747
|
+
const end = () => {
|
|
1748
|
+
observer.disconnect();
|
|
1749
|
+
if (this.#dispatchObserver === observer) this.#dispatchObserver = null;
|
|
1750
|
+
queueMicrotask(() => cls?.removeEventListener?.("refresh", end));
|
|
1751
|
+
};
|
|
1752
|
+
observer.observe(document, {
|
|
1753
|
+
subtree: true,
|
|
1754
|
+
childList: true,
|
|
1755
|
+
attributes: true,
|
|
1756
|
+
characterData: true
|
|
1757
|
+
});
|
|
1758
|
+
this.#dispatchObserver = observer;
|
|
1759
|
+
cls?.addEventListener?.("refresh", end);
|
|
1760
|
+
queueMicrotask(end);
|
|
1761
|
+
}
|
|
1099
1762
|
#wrapUnwrappedPins() {
|
|
1100
1763
|
this.#pinLayers.forEach((layer) => {
|
|
1101
1764
|
if (layer.outer) return;
|
|
@@ -1125,34 +1788,17 @@ var StickyScrollTrigger = class {
|
|
|
1125
1788
|
}
|
|
1126
1789
|
#unwrapPinLayer(layer) {
|
|
1127
1790
|
if (!layer.outer) return false;
|
|
1791
|
+
releaseDormant([layer.trigger]);
|
|
1128
1792
|
unwrapPin(layer.outer, layer.trigger, layer.savedPosition);
|
|
1129
1793
|
layer.outer = null;
|
|
1130
1794
|
layer.inner = null;
|
|
1131
1795
|
return true;
|
|
1132
1796
|
}
|
|
1133
1797
|
#resetSceneCoverStickyState() {
|
|
1134
|
-
|
|
1135
|
-
wrapper,
|
|
1136
|
-
position: wrapper.style.position,
|
|
1137
|
-
top: wrapper.style.top
|
|
1138
|
-
}));
|
|
1139
|
-
wrapperSnapshots.forEach(({ wrapper }) => resetStickyPosition(wrapper));
|
|
1140
|
-
return () => {
|
|
1141
|
-
wrapperSnapshots.forEach(({ wrapper, position, top }) => {
|
|
1142
|
-
wrapper.style.position = position;
|
|
1143
|
-
wrapper.style.top = top;
|
|
1144
|
-
});
|
|
1145
|
-
};
|
|
1798
|
+
return stripStickyPositions(this.#builtLayers.flatMap(({ wrapper }) => wrapper ? [wrapper] : []));
|
|
1146
1799
|
}
|
|
1147
1800
|
#resetPinStickyState() {
|
|
1148
|
-
|
|
1149
|
-
trigger,
|
|
1150
|
-
position: captureInlinePosition(trigger)
|
|
1151
|
-
}));
|
|
1152
|
-
snapshots.forEach(({ trigger }) => resetStickyPosition(trigger));
|
|
1153
|
-
return () => {
|
|
1154
|
-
snapshots.forEach(({ trigger, position }) => restoreInlinePosition(trigger, position));
|
|
1155
|
-
};
|
|
1801
|
+
return stripStickyPositions(this.#wrappedPinTriggers());
|
|
1156
1802
|
}
|
|
1157
1803
|
#resetStickyState() {
|
|
1158
1804
|
const restoreSceneCoverStickyState = this.#resetSceneCoverStickyState();
|
|
@@ -1198,7 +1844,7 @@ var StickyScrollTrigger = class {
|
|
|
1198
1844
|
const resolvedStart = resolveMaybeFn(layer.start);
|
|
1199
1845
|
const resolvedEnd = prefixSpacedRelativeEnd(resolvedStart, resolveMaybeFn(layer.end));
|
|
1200
1846
|
const classifiedEnd = classifyPosition(resolvedEnd);
|
|
1201
|
-
if (classifiedEnd.kind === "max") throw new Error(`StickyScrollTrigger: createStickyPin's end "${resolvedEnd}" uses GSAP's 'max' keyword, which isn't supported here
|
|
1847
|
+
if (classifiedEnd.kind === "max") throw new Error(`StickyScrollTrigger: createStickyPin's end "${resolvedEnd}" uses GSAP's 'max' keyword, which isn't supported here. Use a position clause against endTrigger, such as 'top top'.`);
|
|
1202
1848
|
const triggerTop = documentTop(layer.trigger);
|
|
1203
1849
|
const triggerHeight = measureUsedHeight(layer.trigger);
|
|
1204
1850
|
const triggerMarginBottom = parseFloat(getComputedStyle(layer.trigger).marginBottom) || 0;
|
|
@@ -1230,12 +1876,17 @@ var StickyScrollTrigger = class {
|
|
|
1230
1876
|
}
|
|
1231
1877
|
#planLayerPositions(viewportHeight, active) {
|
|
1232
1878
|
const structureTop = this.#outermostContainer ? documentTop(this.#outermostContainer) : 0;
|
|
1879
|
+
this.#measuredStructure = {
|
|
1880
|
+
top: structureTop,
|
|
1881
|
+
unbuilds: unbuildCount
|
|
1882
|
+
};
|
|
1233
1883
|
const indexByTrigger = new Map();
|
|
1234
1884
|
active.forEach((layer, index) => {
|
|
1235
1885
|
indexByTrigger.set(layer.trigger, index);
|
|
1236
1886
|
});
|
|
1237
1887
|
const measured = active.map((layer, ownIndex) => measureLayer(this.#rootElement, viewportHeight, layer, ownIndex, indexByTrigger));
|
|
1238
1888
|
let documentMaxScroll = 0;
|
|
1889
|
+
const stickyTops = new Map();
|
|
1239
1890
|
const applyLayers = () => planLayers(measured, {
|
|
1240
1891
|
viewportHeight,
|
|
1241
1892
|
structureTop,
|
|
@@ -1248,7 +1899,7 @@ var StickyScrollTrigger = class {
|
|
|
1248
1899
|
layer.engagedStart = plan.engagedStart;
|
|
1249
1900
|
layer.engagedEnd = plan.engagedEnd;
|
|
1250
1901
|
if (!layer.wrapper) return;
|
|
1251
|
-
|
|
1902
|
+
stickyTops.set(layer.wrapper, plan.stickyTop);
|
|
1252
1903
|
if (layer.kind === "scene" && layer.padding && plan.paddingHeight !== null) layer.padding.style.height = `${plan.paddingHeight}px`;
|
|
1253
1904
|
}
|
|
1254
1905
|
});
|
|
@@ -1257,9 +1908,79 @@ var StickyScrollTrigger = class {
|
|
|
1257
1908
|
const needsMaxRemeasure = measured.some((measurement) => measurement.end.mode === "max");
|
|
1258
1909
|
if (needsMaxRemeasure) documentMaxScroll = measureDocumentMaxScroll(viewportHeight);
|
|
1259
1910
|
if (needsLiveRemeasure || needsMaxRemeasure) applyLayers();
|
|
1911
|
+
stickyTops.forEach((top, wrapper) => applyStickyPosition(wrapper, top));
|
|
1260
1912
|
}
|
|
1261
1913
|
refresh() {
|
|
1262
|
-
if (this.#destroyed) return;
|
|
1914
|
+
if (this.#destroyed || !this.#armed || pendingDisarms.has(this)) return;
|
|
1915
|
+
let failure = null;
|
|
1916
|
+
const position = this.#keepContentPending ? this.#naturalScrollTop(this.#builtLayers) : null;
|
|
1917
|
+
this.#keepContentPending = false;
|
|
1918
|
+
try {
|
|
1919
|
+
this.#refreshLayout();
|
|
1920
|
+
} catch (error) {
|
|
1921
|
+
failure = { error };
|
|
1922
|
+
}
|
|
1923
|
+
try {
|
|
1924
|
+
rebuildDormancy(this.#scrollTriggerClass);
|
|
1925
|
+
} catch (error) {
|
|
1926
|
+
clearDormancy();
|
|
1927
|
+
queueMicrotask(() => {
|
|
1928
|
+
throw error;
|
|
1929
|
+
});
|
|
1930
|
+
}
|
|
1931
|
+
this.#scrollMarginSync?.applyRamps();
|
|
1932
|
+
if (!failure && position !== null) this.#keepContent(position);
|
|
1933
|
+
if (failure) throw failure.error;
|
|
1934
|
+
}
|
|
1935
|
+
#gsapScrollRecord() {
|
|
1936
|
+
if (!this.#scrollTriggerClass?.isRefreshing) return null;
|
|
1937
|
+
const scrollFunc = this.#scrollTriggerClass.getScrollFunc?.(window);
|
|
1938
|
+
return scrollFunc && typeof scrollFunc.rec === "number" && scrollFunc.rec ? scrollFunc : null;
|
|
1939
|
+
}
|
|
1940
|
+
#naturalScrollTop(layers) {
|
|
1941
|
+
const measured = this.#measuredStructure;
|
|
1942
|
+
const moved = measured && this.#outermostContainer && measured.unbuilds !== unbuildCount ? documentTop(this.#outermostContainer) - measured.top : 0;
|
|
1943
|
+
const scroll = (this.#gsapScrollRecord()?.rec ?? window.scrollY) - moved;
|
|
1944
|
+
const consumed = new Map();
|
|
1945
|
+
layers.forEach((layer) => {
|
|
1946
|
+
if (layer.kind !== "scene") return;
|
|
1947
|
+
consumed.set(layer.trigger, scroll > layer.engagedEnd ? Infinity : dwellConsumedAt(scroll, this.#engagedWindows([layer])));
|
|
1948
|
+
});
|
|
1949
|
+
return {
|
|
1950
|
+
natural: scroll - dwellConsumedAt(scroll, this.#engagedWindows(layers)) + moved,
|
|
1951
|
+
consumed
|
|
1952
|
+
};
|
|
1953
|
+
}
|
|
1954
|
+
#keepContent(position, cls = this.#scrollTriggerClass) {
|
|
1955
|
+
const target = this.#keptScrollTop(position);
|
|
1956
|
+
const record = this.#gsapScrollRecord();
|
|
1957
|
+
if (record) {
|
|
1958
|
+
record.rec = target;
|
|
1959
|
+
return;
|
|
1960
|
+
}
|
|
1961
|
+
if (cls?.isRefreshing) return;
|
|
1962
|
+
if (Math.abs(window.scrollY - target) >= .5) window.scrollTo({
|
|
1963
|
+
top: Math.round(target),
|
|
1964
|
+
behavior: "instant"
|
|
1965
|
+
});
|
|
1966
|
+
const scrollFunc = cls?.getScrollFunc?.(window);
|
|
1967
|
+
if (typeof scrollFunc !== "function") return;
|
|
1968
|
+
const cached = scrollFunc();
|
|
1969
|
+
if (typeof cached !== "number" || Math.abs(cached - window.scrollY) >= .5) scrollFunc(window.scrollY);
|
|
1970
|
+
}
|
|
1971
|
+
#keptScrollTop({ natural, consumed }) {
|
|
1972
|
+
let earlierDwell = 0;
|
|
1973
|
+
let target = natural;
|
|
1974
|
+
this.#builtLayers.flatMap((layer) => layer.kind === "scene" ? [layer] : []).sort((a, b) => a.engagedStart - b.engagedStart).forEach((layer) => {
|
|
1975
|
+
const dwell = layer.engagedEnd - layer.engagedStart;
|
|
1976
|
+
const before = consumed.get(layer.trigger);
|
|
1977
|
+
if (before !== void 0) target += Math.min(before, dwell);
|
|
1978
|
+
else if (layer.engagedStart < natural + earlierDwell - .05) target += dwell;
|
|
1979
|
+
earlierDwell += dwell;
|
|
1980
|
+
});
|
|
1981
|
+
return target;
|
|
1982
|
+
}
|
|
1983
|
+
#refreshLayout() {
|
|
1263
1984
|
if (this.#dirty || !this.#built || isDomOrderStale(this.#layers)) {
|
|
1264
1985
|
this.#unbuild();
|
|
1265
1986
|
this.#build();
|
|
@@ -1289,7 +2010,7 @@ var StickyScrollTrigger = class {
|
|
|
1289
2010
|
});
|
|
1290
2011
|
}
|
|
1291
2012
|
#syncScrollMargins() {
|
|
1292
|
-
this.#scrollMarginSync
|
|
2013
|
+
this.#scrollMarginSync?.sync(this.#engagedWindows(), this.#outermostContainer, (targets) => {
|
|
1293
2014
|
if (!targets.length) return [];
|
|
1294
2015
|
const restoreStickyState = this.#resetStickyState();
|
|
1295
2016
|
const tops = targets.map((target) => documentTop(target));
|
|
@@ -1318,6 +2039,7 @@ var StickyScrollTrigger = class {
|
|
|
1318
2039
|
if (this.#destroyed) throw new Error("StickyScrollTrigger: cannot register a new layer after destroy() has been called.");
|
|
1319
2040
|
this.#assertTriggerAvailable(this.#layers, layer.trigger, `StickyScrollTrigger: ${describeElement(layer.trigger)} is already registered as a trigger for another layer. Each element can be used once per controller instance, across createStickyTrigger and createOverlapScroll: reusing one makes endTrigger resolution ambiguous.`);
|
|
1320
2041
|
this.#assertTriggerAvailable(this.#pinLayers, layer.trigger, `StickyScrollTrigger: ${describeElement(layer.trigger)} is already registered as a pin trigger (createStickyPin). Using the same element as both a Scene/Cover trigger and a pin trigger wraps it twice and produces conflicting position:sticky behavior.`);
|
|
2042
|
+
this.#arm();
|
|
1321
2043
|
this.#layers.push(layer);
|
|
1322
2044
|
this.#dirty = true;
|
|
1323
2045
|
return {
|
|
@@ -1328,10 +2050,10 @@ var StickyScrollTrigger = class {
|
|
|
1328
2050
|
invalidateOnRefresh: rest.invalidateOnRefresh ?? true,
|
|
1329
2051
|
onKill: (self) => {
|
|
1330
2052
|
this.#captureScrollTriggerClass(self);
|
|
1331
|
-
|
|
1332
|
-
if (index !== -1) this.#layers.splice(index, 1);
|
|
2053
|
+
removeItem(this.#layers, layer);
|
|
1333
2054
|
this.#dirty = true;
|
|
1334
2055
|
this.#scheduleRebuild();
|
|
2056
|
+
this.#disarmIfEmpty();
|
|
1335
2057
|
onKill?.(self);
|
|
1336
2058
|
},
|
|
1337
2059
|
onRefreshInit: this.#createAutoRefreshHandler(rest.onRefreshInit)
|
|
@@ -1339,6 +2061,7 @@ var StickyScrollTrigger = class {
|
|
|
1339
2061
|
}
|
|
1340
2062
|
createStickyTrigger({ trigger: triggerInput, start = "0 0", end = "100% 0", endTrigger: endTriggerInput, onKill, ...rest }) {
|
|
1341
2063
|
this.#assertNoExcludedVars(rest, "createStickyTrigger");
|
|
2064
|
+
this.#flushOverlappingDisarms();
|
|
1342
2065
|
const trigger = resolveElement(triggerInput, "createStickyTrigger");
|
|
1343
2066
|
this.#assertTriggerInsideRoot(trigger, "createStickyTrigger");
|
|
1344
2067
|
const endTrigger = resolveEndTrigger(trigger, endTriggerInput, "createStickyTrigger");
|
|
@@ -1359,6 +2082,7 @@ var StickyScrollTrigger = class {
|
|
|
1359
2082
|
}
|
|
1360
2083
|
createOverlapScroll({ trigger: triggerInput, cover, start = "bottom bottom", end = null, endTrigger: endTriggerInput, onKill, ...rest }) {
|
|
1361
2084
|
this.#assertNoExcludedVars(rest, "createOverlapScroll");
|
|
2085
|
+
this.#flushOverlappingDisarms();
|
|
1362
2086
|
const trigger = resolveElement(triggerInput, "createOverlapScroll");
|
|
1363
2087
|
this.#assertTriggerInsideRoot(trigger, "createOverlapScroll");
|
|
1364
2088
|
const endTrigger = resolveEndTrigger(trigger, endTriggerInput, "createOverlapScroll");
|
|
@@ -1380,6 +2104,7 @@ var StickyScrollTrigger = class {
|
|
|
1380
2104
|
};
|
|
1381
2105
|
const vars = this.#registerLayer(layer, (self) => {
|
|
1382
2106
|
this.#coverRestoreByLayer.get(layer)?.();
|
|
2107
|
+
unbuildCover(layer);
|
|
1383
2108
|
onKill?.(self);
|
|
1384
2109
|
}, rest);
|
|
1385
2110
|
this.#coverRestoreByLayer.set(layer, liftAboveStickyWrapper(coverElement));
|
|
@@ -1387,6 +2112,7 @@ var StickyScrollTrigger = class {
|
|
|
1387
2112
|
}
|
|
1388
2113
|
createStickyPin({ trigger: triggerInput, start, top, endTrigger: endTriggerInput, end = "bottom top", onKill, ...rest }) {
|
|
1389
2114
|
this.#assertNoExcludedVars(rest, "createStickyPin");
|
|
2115
|
+
this.#flushOverlappingDisarms();
|
|
1390
2116
|
if (this.#destroyed) throw new Error("StickyScrollTrigger: cannot register a new pin after destroy() has been called.");
|
|
1391
2117
|
if (start !== void 0 && top !== void 0) throw new Error("StickyScrollTrigger: createStickyPin accepts either start or top, not both (top: 20 is start: 'top 20px'). Use top for a plain px distance from the viewport's top edge, start for anything else.");
|
|
1392
2118
|
const trigger = resolveElement(triggerInput, "createStickyPin");
|
|
@@ -1403,15 +2129,16 @@ var StickyScrollTrigger = class {
|
|
|
1403
2129
|
endTrigger,
|
|
1404
2130
|
end
|
|
1405
2131
|
};
|
|
2132
|
+
this.#arm();
|
|
1406
2133
|
this.#pinLayers.push(layer);
|
|
1407
2134
|
return {
|
|
1408
2135
|
...rest,
|
|
1409
2136
|
trigger,
|
|
1410
2137
|
onKill: (self) => {
|
|
1411
2138
|
this.#captureScrollTriggerClass(self);
|
|
1412
|
-
|
|
1413
|
-
if (index !== -1) this.#pinLayers.splice(index, 1);
|
|
2139
|
+
removeItem(this.#pinLayers, layer);
|
|
1414
2140
|
if (this.#unwrapPinLayer(layer)) this.#scheduleRebuild();
|
|
2141
|
+
this.#disarmIfEmpty();
|
|
1415
2142
|
onKill?.(self);
|
|
1416
2143
|
},
|
|
1417
2144
|
onRefreshInit: this.#createAutoRefreshHandler(rest.onRefreshInit)
|
|
@@ -1440,8 +2167,8 @@ var StickyScrollTrigger = class {
|
|
|
1440
2167
|
if (!this.#rootElement.contains(element)) return reachedAt;
|
|
1441
2168
|
return reachedAt + dwellBeforeReach(reachedAt, this.#engagedWindows());
|
|
1442
2169
|
}
|
|
1443
|
-
#engagedWindows() {
|
|
1444
|
-
return
|
|
2170
|
+
#engagedWindows(layers = this.#builtLayers) {
|
|
2171
|
+
return layers.flatMap((layer) => layer.kind === "scene" ? [{
|
|
1445
2172
|
freezeStart: layer.engagedStart,
|
|
1446
2173
|
freezeEnd: layer.engagedEnd
|
|
1447
2174
|
}] : []);
|
|
@@ -1457,6 +2184,7 @@ var StickyScrollTrigger = class {
|
|
|
1457
2184
|
}
|
|
1458
2185
|
createResolvedTrigger({ trigger: triggerInput, start = "top bottom", end = "bottom top", endTrigger: endTriggerInput, ...rest }) {
|
|
1459
2186
|
this.#assertNoExcludedVars(rest, "createResolvedTrigger");
|
|
2187
|
+
this.#flushOverlappingDisarms();
|
|
1460
2188
|
const trigger = resolveElement(triggerInput, "createResolvedTrigger");
|
|
1461
2189
|
const endTrigger = resolveEndTrigger(trigger, endTriggerInput, "createResolvedTrigger");
|
|
1462
2190
|
return {
|
|
@@ -1473,12 +2201,17 @@ var StickyScrollTrigger = class {
|
|
|
1473
2201
|
}
|
|
1474
2202
|
destroy() {
|
|
1475
2203
|
if (this.#destroyed) return;
|
|
2204
|
+
runEach(() => this.#flushPendingDisarmForRevert(), () => this.#tearDown());
|
|
2205
|
+
}
|
|
2206
|
+
#tearDown() {
|
|
2207
|
+
if (this.#restTimer !== null) clearTimeout(this.#restTimer);
|
|
2208
|
+
this.#restTimer = null;
|
|
1476
2209
|
this.#destroyed = true;
|
|
1477
|
-
this.#
|
|
1478
|
-
this.#
|
|
1479
|
-
|
|
2210
|
+
const run = this.#deferredRun;
|
|
2211
|
+
this.#deferredRun = null;
|
|
2212
|
+
run?.();
|
|
2213
|
+
if (this.#armed) this.#disarm();
|
|
1480
2214
|
this.#layers.length = 0;
|
|
1481
|
-
this.#builtLayers = [];
|
|
1482
2215
|
this.#pinLayers.forEach((layer) => this.#unwrapPinLayer(layer));
|
|
1483
2216
|
this.#pinLayers.length = 0;
|
|
1484
2217
|
this.#scrollTriggerClass = null;
|