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