sticky-scroll-trigger 0.15.0 → 0.17.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.
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @license
3
- * StickyScrollTrigger v0.15.0
3
+ * StickyScrollTrigger v0.17.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
@@ -69,14 +69,18 @@ var StickyScrollTrigger = (function() {
69
69
  };
70
70
  const measureDocumentMaxScroll = (viewportHeight) => Math.max(0, measureDocumentHeight() - viewportHeight);
71
71
  const measureBorderWidth = (borderStyle, width) => borderStyle === "none" || borderStyle === "hidden" ? 0 : parseFloat(width);
72
- const measureUsedHeight = (el) => {
73
- const rounded = el.offsetHeight;
72
+ const measureUsedSize = (el, axis) => {
73
+ const rounded = axis === "height" ? el.offsetHeight : el.offsetWidth;
74
74
  const style = getComputedStyle(el);
75
- const height = parseFloat(style.height);
76
- if (!Number.isFinite(height)) return rounded;
77
- const measured = style.boxSizing === "border-box" ? height : height + parseFloat(style.paddingTop) + parseFloat(style.paddingBottom) + measureBorderWidth(style.borderTopStyle, style.borderTopWidth) + measureBorderWidth(style.borderBottomStyle, style.borderBottomWidth);
75
+ const size = parseFloat(style[axis]);
76
+ if (!Number.isFinite(size)) return rounded;
77
+ const [start, end] = axis === "height" ? ["Top", "Bottom"] : ["Left", "Right"];
78
+ const measured = style.boxSizing === "border-box" ? size : size + parseFloat(style[`padding${start}`]) + parseFloat(style[`padding${end}`]) + measureBorderWidth(style[`border${start}Style`], style[`border${start}Width`]) + measureBorderWidth(style[`border${end}Style`], style[`border${end}Width`]);
78
79
  return rounded > 0 && Math.abs(measured - rounded) >= 1 ? rounded : measured;
79
80
  };
81
+ const measureUsedHeight = (el) => measureUsedSize(el, "height");
82
+ const measureUsedWidth = (el) => measureUsedSize(el, "width");
83
+ const marginBoxHeight = (el, style) => measureUsedHeight(el) + parseFloat(style.marginTop) + parseFloat(style.marginBottom);
80
84
  const compareDocumentOrder = (a, b) => {
81
85
  const position = a.compareDocumentPosition(b);
82
86
  if (position & Node.DOCUMENT_POSITION_FOLLOWING) return -1;
@@ -84,16 +88,36 @@ var StickyScrollTrigger = (function() {
84
88
  return 0;
85
89
  };
86
90
  const resetStickyPosition = (el) => {
87
- el.style.position = "";
88
- el.style.top = "";
91
+ el.style.setProperty("position", "relative", "important");
92
+ el.style.setProperty("top", "auto", "important");
93
+ el.style.setProperty("bottom", "auto", "important");
94
+ };
95
+ const captureInline = (el, property) => ({
96
+ value: el.style.getPropertyValue(property),
97
+ priority: el.style.getPropertyPriority(property)
98
+ });
99
+ const restoreInline = (el, property, saved) => {
100
+ el.style.setProperty(property, saved.value, saved.priority);
89
101
  };
90
102
  const captureInlinePosition = (el) => ({
91
- position: el.style.position,
92
- top: el.style.top
103
+ position: captureInline(el, "position"),
104
+ top: captureInline(el, "top"),
105
+ bottom: captureInline(el, "bottom")
93
106
  });
94
107
  const restoreInlinePosition = (el, saved) => {
95
- el.style.position = saved.position;
96
- el.style.top = saved.top;
108
+ restoreInline(el, "position", saved.position);
109
+ restoreInline(el, "top", saved.top);
110
+ restoreIfWritten(el, "bottom", saved.bottom, "auto", "important");
111
+ };
112
+ const writeInline = (el, property, value, priority = "") => {
113
+ el.style.setProperty(property, value, priority);
114
+ return el.style.getPropertyValue(property);
115
+ };
116
+ const restoreIfWritten = (el, property, saved, written, priority = "") => {
117
+ const { style } = el;
118
+ if (style.getPropertyValue(property) !== written || style.getPropertyPriority(property) !== priority) return false;
119
+ restoreInline(el, property, saved);
120
+ return true;
97
121
  };
98
122
  const stripStickyPositions = (elements) => {
99
123
  const saved = elements.map(captureInlinePosition);
@@ -121,20 +145,72 @@ var StickyScrollTrigger = (function() {
121
145
  el.style.setProperty("transition", "none", "important");
122
146
  return el;
123
147
  };
148
+ let movesUnderWay = 0;
149
+ const isMovingDom = () => movesUnderWay > 0;
150
+ const moveInto = (parent, node, before = null) => {
151
+ const target = parent;
152
+ movesUnderWay += 1;
153
+ try {
154
+ if (target.moveBefore && parent.isConnected && node.isConnected) try {
155
+ target.moveBefore(node, before);
156
+ return;
157
+ } catch {}
158
+ parent.insertBefore(node, before);
159
+ } finally {
160
+ movesUnderWay -= 1;
161
+ }
162
+ };
163
+ const transparentNodes = new WeakSet();
164
+ const isTransparent = (el, own) => transparentNodes.has(el) || own.has(el);
165
+ const logicalParent = (el, own) => {
166
+ let parent = el.parentElement;
167
+ while (parent && isTransparent(parent, own)) parent = parent.parentElement;
168
+ return parent;
169
+ };
170
+ const firstLogicalAt = (el, own) => {
171
+ if (!isTransparent(el, own)) return el;
172
+ for (const child of Array.from(el.children)) {
173
+ const found = firstLogicalAt(child, own);
174
+ if (found) return found;
175
+ }
176
+ return null;
177
+ };
178
+ const logicalNextSibling = (el, own) => {
179
+ for (let node = el;;) {
180
+ for (let sibling = node.nextElementSibling; sibling; sibling = sibling.nextElementSibling) {
181
+ const found = firstLogicalAt(sibling, own);
182
+ if (found) return found;
183
+ }
184
+ const parent = node.parentElement;
185
+ if (!parent || !isTransparent(parent, own)) return null;
186
+ node = parent;
187
+ }
188
+ };
189
+ const physicalHolder = (el) => {
190
+ let node = el;
191
+ while (node.parentElement && transparentNodes.has(node.parentElement)) node = node.parentElement;
192
+ return node;
193
+ };
124
194
  const wrapScene = (inner, holdTrailingMargin) => {
125
195
  if (!inner.parentNode) throw new Error(`StickyScrollTrigger: ${describeElement(inner)} is not attached to the document`);
126
196
  const container = createStructureElement();
127
197
  const wrapper = createStructureElement();
128
198
  const padding = createStructureElement();
199
+ [
200
+ container,
201
+ wrapper,
202
+ padding
203
+ ].forEach((node) => transparentNodes.add(node));
129
204
  padding.setAttribute("aria-hidden", "true");
130
205
  padding.style.pointerEvents = "none";
131
206
  inner.parentNode.insertBefore(container, inner);
132
207
  container.appendChild(wrapper);
133
- wrapper.appendChild(inner);
208
+ moveInto(wrapper, inner);
134
209
  container.appendChild(padding);
135
210
  if (holdTrailingMargin) {
136
211
  const sentinel = createStructureElement();
137
212
  sentinel.style.display = "flow-root";
213
+ transparentNodes.add(sentinel);
138
214
  wrapper.appendChild(sentinel);
139
215
  }
140
216
  return {
@@ -144,21 +220,22 @@ var StickyScrollTrigger = (function() {
144
220
  };
145
221
  };
146
222
  const wrapCover = (trigger) => {
147
- const parent = trigger.parentNode;
223
+ const holder = physicalHolder(trigger);
224
+ const parent = holder.parentNode;
148
225
  if (!parent) throw new Error(`createOverlapScroll: trigger ${describeElement(trigger)} is not attached to the document`);
149
226
  const wrapper = createStructureElement();
150
227
  parent.insertBefore(wrapper, parent.firstChild);
151
228
  while (wrapper.nextSibling) {
152
229
  const node = wrapper.nextSibling;
153
- wrapper.appendChild(node);
154
- if (node === trigger) break;
230
+ moveInto(wrapper, node);
231
+ if (node === holder) break;
155
232
  }
156
233
  return wrapper;
157
234
  };
158
235
  const unwrapCover = (wrapper) => {
159
236
  const parent = wrapper.parentNode;
160
237
  if (!parent) return;
161
- while (wrapper.firstChild) parent.insertBefore(wrapper.firstChild, wrapper);
238
+ while (wrapper.firstChild) moveInto(parent, wrapper.firstChild, wrapper);
162
239
  wrapper.remove();
163
240
  };
164
241
  const wrapPin = (trigger) => {
@@ -166,25 +243,216 @@ var StickyScrollTrigger = (function() {
166
243
  const outer = createStructureElement();
167
244
  const inner = createStructureElement();
168
245
  outer.style.display = "flow-root";
246
+ transparentNodes.add(outer);
247
+ transparentNodes.add(inner);
169
248
  trigger.parentNode.insertBefore(outer, trigger);
170
249
  outer.appendChild(inner);
171
- inner.appendChild(trigger);
250
+ moveInto(inner, trigger);
172
251
  pinRanges.add(inner);
173
252
  return {
174
253
  outer,
175
254
  inner
176
255
  };
177
256
  };
257
+ const PIN_ITEM_PROPERTIES = [
258
+ "flexGrow",
259
+ "flexShrink",
260
+ "flexBasis",
261
+ "alignSelf",
262
+ "justifySelf",
263
+ "order",
264
+ "gridColumnStart",
265
+ "gridColumnEnd",
266
+ "gridRowStart",
267
+ "gridRowEnd",
268
+ "cssFloat",
269
+ "clear",
270
+ "verticalAlign"
271
+ ];
272
+ const layoutContainer = (outer) => {
273
+ let parent = outer.parentElement;
274
+ while (parent && getComputedStyle(parent).display === "contents") parent = parent.parentElement;
275
+ return parent;
276
+ };
277
+ const FLOATS = [
278
+ "left",
279
+ "right",
280
+ "inline-start",
281
+ "inline-end"
282
+ ];
283
+ const hasOwnBox = (style) => style.display !== "none" && style.display !== "contents";
284
+ const isBlockLevel = (style) => hasOwnBox(style) && !style.display.startsWith("inline");
285
+ const itemLayout = (outer, style) => {
286
+ if (!hasOwnBox(style)) return null;
287
+ const container = layoutContainer(outer);
288
+ const display = container ? getComputedStyle(container).display : "";
289
+ if (display.includes("flex")) return "flex";
290
+ if (display.includes("grid")) return "grid";
291
+ if (FLOATS.includes(style.cssFloat)) return "float";
292
+ return style.display.startsWith("inline-") ? "inline" : null;
293
+ };
294
+ const outerDisplay = (layout) => layout === "inline" ? "inline-block" : "flow-root";
295
+ const TRIGGER_BOX = [
296
+ "width",
297
+ "max-width",
298
+ "height",
299
+ "max-height",
300
+ "min-height",
301
+ "padding-top",
302
+ "padding-right",
303
+ "padding-bottom",
304
+ "padding-left",
305
+ "margin-top",
306
+ "margin-right",
307
+ "margin-bottom",
308
+ "margin-left"
309
+ ];
310
+ const BLOCK_BOX = [
311
+ "height",
312
+ "max-height",
313
+ "min-height"
314
+ ];
315
+ const PROBE_HEIGHT = "123456px";
316
+ const readHeldBox = (style, properties) => properties.map((property) => style.getPropertyValue(property.replace(/^m(ax|in)-/, "")));
317
+ const HELD_PRIORITY = "important";
318
+ const heldBoxes = new WeakMap();
319
+ const holdTriggerBox = (trigger, properties, values) => {
320
+ const authored = properties.map((property) => captureInline(trigger, property));
321
+ heldBoxes.set(trigger, {
322
+ properties,
323
+ authored,
324
+ written: properties.map((property, i) => writeInline(trigger, property, values[i], HELD_PRIORITY))
325
+ });
326
+ };
327
+ const releaseTriggerBox = (trigger) => {
328
+ const held = heldBoxes.get(trigger);
329
+ if (!held) return void 0;
330
+ held.properties.forEach((property, i) => {
331
+ restoreIfWritten(trigger, property, held.authored[i], held.written[i], HELD_PRIORITY);
332
+ });
333
+ heldBoxes.delete(trigger);
334
+ return held;
335
+ };
336
+ const fitPinsToItems = (pins) => {
337
+ const held = pins.map(({ trigger }) => releaseTriggerBox(trigger));
338
+ const enclosing = pins.map(({ trigger }) => pins.filter((other) => other.trigger !== trigger && other.trigger.contains(trigger)));
339
+ const reserved = pins.map(() => void 0);
340
+ try {
341
+ for (let depth = Math.max(0, ...enclosing.map(({ length }) => length)); depth >= 0; depth--) {
342
+ const indexes = pins.flatMap((_, i) => enclosing[i].length === depth ? [i] : []);
343
+ const around = new Set(indexes.flatMap((i) => enclosing[i]));
344
+ fitReleasedPins(indexes.map((i) => pins[i]), [...around]).forEach((height, j) => {
345
+ reserved[indexes[j]] = height;
346
+ });
347
+ }
348
+ return reserved;
349
+ } catch (error) {
350
+ pins.forEach(({ trigger }, i) => {
351
+ const box = held[i];
352
+ releaseTriggerBox(trigger);
353
+ if (box) holdTriggerBox(trigger, box.properties, box.written);
354
+ });
355
+ throw error;
356
+ }
357
+ };
358
+ const fitReleasedPins = (pins, around) => {
359
+ const styles = pins.map(({ trigger }) => getComputedStyle(trigger));
360
+ const layouts = pins.map(({ outer }, i) => itemLayout(outer, styles[i]));
361
+ const rendered = pins.map(({ trigger }) => trigger.getClientRects().length > 0);
362
+ const copied = styles.map((style, i) => PIN_ITEM_PROPERTIES.map((property) => layouts[i] || property === "clear" && isBlockLevel(style) ? style[property] : ""));
363
+ const measured = pins.flatMap((pin, index) => rendered[index] ? [{
364
+ ...pin,
365
+ index,
366
+ layout: layouts[index]
367
+ }] : []);
368
+ const aroundDisplays = around.map(({ outer, inner }) => [outer.style.display, inner.style.display]);
369
+ pins.forEach(({ outer }, i) => {
370
+ outer.style.width = "";
371
+ PIN_ITEM_PROPERTIES.forEach((property, j) => {
372
+ outer.style[property] = copied[i][j];
373
+ });
374
+ outer.style.display = rendered[i] ? "contents" : outerDisplay(layouts[i]);
375
+ });
376
+ [...measured, ...around].forEach(({ outer, inner }) => {
377
+ outer.style.display = "contents";
378
+ inner.style.display = "contents";
379
+ });
380
+ let boxes;
381
+ try {
382
+ boxes = measured.map(({ trigger, index, layout }) => {
383
+ const style = styles[index];
384
+ if (!layout) return { box: readHeldBox(style, BLOCK_BOX) };
385
+ return {
386
+ item: {
387
+ width: layout === "grid" ? void 0 : measureUsedWidth(trigger) + parseFloat(style.marginLeft) + parseFloat(style.marginRight),
388
+ height: marginBoxHeight(trigger, style)
389
+ },
390
+ box: readHeldBox(style, TRIGGER_BOX)
391
+ };
392
+ });
393
+ } finally {
394
+ measured.forEach(({ outer, inner, layout }) => {
395
+ outer.style.display = outerDisplay(layout);
396
+ inner.style.display = "";
397
+ });
398
+ around.forEach(({ outer, inner }, i) => {
399
+ [outer.style.display, inner.style.display] = aroundDisplays[i];
400
+ });
401
+ }
402
+ const inPx = boxes.map(({ box }) => box.every((value) => value.endsWith("px")));
403
+ const dependent = heightsFollowingInner(measured, boxes, inPx, styles);
404
+ const reserved = pins.map(() => void 0);
405
+ measured.forEach(({ outer, trigger, index, layout }, i) => {
406
+ const { item, box } = boxes[i];
407
+ if (!item) {
408
+ if (inPx[i] && dependent.has(i)) holdTriggerBox(trigger, BLOCK_BOX, box);
409
+ return;
410
+ }
411
+ const { width, height } = item;
412
+ if (!Number.isFinite(height) || width !== void 0 && !Number.isFinite(width)) return;
413
+ if (layout === "flex") outer.style.flex = "0 0 auto";
414
+ if (width !== void 0) outer.style.width = `${width}px`;
415
+ reserved[index] = height;
416
+ if (inPx[i]) holdTriggerBox(trigger, TRIGGER_BOX, box);
417
+ });
418
+ return reserved;
419
+ };
420
+ const heightsFollowingInner = (measured, boxes, inPx, styles) => {
421
+ const blocks = measured.flatMap((pin, i) => !boxes[i].item && inPx[i] ? [{
422
+ ...pin,
423
+ i
424
+ }] : []);
425
+ const dependent = new Set(probeInner(blocks, () => PROBE_HEIGHT, boxes));
426
+ const capped = blocks.filter(({ index, i }) => !dependent.has(i) && styles[index].maxHeight.includes("%"));
427
+ probeInner(capped, ({ i }) => boxes[i].box[0], boxes).forEach((i) => dependent.add(i));
428
+ return dependent;
429
+ };
430
+ const probeInner = (blocks, height, boxes) => {
431
+ const saved = blocks.map(({ outer, inner }) => [outer.style.height, inner.style.height]);
432
+ const outerHeights = blocks.map(({ outer }) => getComputedStyle(outer).height);
433
+ blocks.forEach((block, j) => {
434
+ block.outer.style.height = outerHeights[j];
435
+ block.inner.style.height = height(block);
436
+ });
437
+ try {
438
+ return blocks.flatMap(({ trigger, i }) => getComputedStyle(trigger).height !== boxes[i].box[0] ? [i] : []);
439
+ } finally {
440
+ blocks.forEach(({ outer, inner }, j) => {
441
+ [outer.style.height, inner.style.height] = saved[j];
442
+ });
443
+ }
444
+ };
178
445
  const unwrapPin = (outer, trigger, saved) => {
179
446
  const parent = outer.parentNode;
180
447
  restoreInlinePosition(trigger, saved);
181
- if (parent) parent.insertBefore(trigger, outer);
448
+ releaseTriggerBox(trigger);
449
+ if (parent) moveInto(parent, trigger, outer);
182
450
  if (outer.firstElementChild instanceof HTMLElement) pinRanges.delete(outer.firstElementChild);
183
451
  outer.remove();
184
452
  };
185
453
  const LIFT_VALUES = {
186
- position: "relative",
187
- zIndex: "1"
454
+ "position": "relative",
455
+ "z-index": "1"
188
456
  };
189
457
  const liftStates = new WeakMap();
190
458
  const liftAboveStickyWrapper = (cover) => {
@@ -196,12 +464,14 @@ var StickyScrollTrigger = (function() {
196
464
  if (existing) existing.count += 1;
197
465
  else {
198
466
  const computed = getComputedStyle(element);
199
- const applied = [];
200
- if (computed.position === "static") applied.push("position");
201
- if (computed.zIndex === "auto") applied.push("zIndex");
202
- applied.forEach((property) => {
203
- element.style[property] = LIFT_VALUES[property];
204
- });
467
+ const properties = [];
468
+ if (computed.position === "static") properties.push("position");
469
+ if (computed.zIndex === "auto") properties.push("z-index");
470
+ const applied = properties.map((property) => ({
471
+ property,
472
+ authored: captureInline(element, property),
473
+ written: writeInline(element, property, LIFT_VALUES[property])
474
+ }));
205
475
  liftStates.set(element, {
206
476
  count: 1,
207
477
  applied
@@ -215,8 +485,8 @@ var StickyScrollTrigger = (function() {
215
485
  if (!state) return;
216
486
  state.count -= 1;
217
487
  if (state.count > 0) return;
218
- state.applied.forEach((property) => {
219
- if (el.style[property] === LIFT_VALUES[property]) el.style[property] = "";
488
+ state.applied.forEach(({ property, authored, written }) => {
489
+ restoreIfWritten(el, property, authored, written);
220
490
  });
221
491
  liftStates.delete(el);
222
492
  });
@@ -630,15 +900,15 @@ var StickyScrollTrigger = (function() {
630
900
  return;
631
901
  }
632
902
  const toMeasure = all.filter((r) => owned.has(r.el) || holdsOwned.has(r) || r.state !== "in");
633
- const authorStyle = new Map(toMeasure.filter((r) => !owned.has(r.el) && r.state === "in").map(({ el }) => [el, el.getAttribute("style")]));
903
+ const authorStyle = new Map(toMeasure.filter((r) => !owned.has(r.el) && r.state === "in").map(({ el }) => [el, el.hasAttribute("style") ? captureInline(el, "position") : null]));
634
904
  const restorePositions = () => toMeasure.forEach((record) => {
635
905
  if (!authorStyle.has(record.el)) {
636
906
  promote(record, true);
637
907
  return;
638
908
  }
639
909
  const saved = authorStyle.get(record.el);
640
- if (saved === null || saved === void 0) record.el.removeAttribute("style");
641
- else record.el.setAttribute("style", saved);
910
+ if (saved) restoreInline(record.el, "position", saved);
911
+ else record.el.removeAttribute("style");
642
912
  });
643
913
  toMeasure.forEach(({ el }) => {
644
914
  el.style.position = "static";
@@ -718,12 +988,14 @@ var StickyScrollTrigger = (function() {
718
988
  el.style.top = `${topPx}px`;
719
989
  };
720
990
  const commitPosition = (el, saved) => {
721
- el.style.position = saved.position;
722
- el.style.top = saved.top;
991
+ restoreInlinePosition(el, saved);
723
992
  };
724
993
  const commitHeight = (el, height) => {
725
994
  el.style.height = height;
726
995
  };
996
+ const commitCssText = (el, cssText) => {
997
+ el.style.cssText = cssText;
998
+ };
727
999
  const commitInert = (wrapper, padding) => {
728
1000
  wrapper.style.position = "";
729
1001
  wrapper.style.top = "";
@@ -736,6 +1008,209 @@ var StickyScrollTrigger = (function() {
736
1008
  const clearHeightForRead = (el) => {
737
1009
  el.style.height = "";
738
1010
  };
1011
+ let scrollZeroReturn = null;
1012
+ const scrollZeroReturnTop = () => scrollZeroReturn;
1013
+ const moveScrollZeroReturn = (top) => {
1014
+ scrollZeroReturn = top;
1015
+ };
1016
+ let ownScrollTop = null;
1017
+ let ownScrollAt = -Infinity;
1018
+ const markOwnScroll = (fromRest) => {
1019
+ ownScrollTop = fromRest ? window.scrollY : null;
1020
+ ownScrollAt = performance.now();
1021
+ };
1022
+ const isOwnTripOnly = () => {
1023
+ if (ownScrollTop !== null && (Math.abs(window.scrollY - ownScrollTop) >= .5 || performance.now() - ownScrollAt >= 500)) ownScrollTop = null;
1024
+ return ownScrollTop !== null;
1025
+ };
1026
+ const isPageScrolling = (cls) => (isRefreshing(cls) ? isScrolling(cls) : isScrollingOrUpdating(cls)) && !isOwnTripOnly();
1027
+ const isUserScrolling = (cls) => isScrolling(cls) && !isOwnTripOnly();
1028
+ const scrollBackTo = (top) => {
1029
+ if (Math.abs(window.scrollY - top) >= .5) window.scrollTo({
1030
+ top,
1031
+ behavior: "instant"
1032
+ });
1033
+ };
1034
+ const enterScrollZero = (cls) => {
1035
+ if (scrollZeroReturn !== null) {
1036
+ scrollBackTo(0);
1037
+ dormancyAtScrollZero();
1038
+ return () => {};
1039
+ }
1040
+ const fromRest = !isUserScrolling(cls);
1041
+ scrollZeroReturn = window.scrollY;
1042
+ dormancyAtScrollZero();
1043
+ window.scrollTo({
1044
+ top: 0,
1045
+ behavior: "instant"
1046
+ });
1047
+ return () => {
1048
+ const back = scrollZeroReturn ?? 0;
1049
+ scrollZeroReturn = null;
1050
+ window.scrollTo({
1051
+ top: back,
1052
+ behavior: "instant"
1053
+ });
1054
+ markOwnScroll(fromRest);
1055
+ syncScrollCache(cls);
1056
+ dormancyAfterScrollZero(window.scrollY);
1057
+ };
1058
+ };
1059
+ let gsapAtZero = false;
1060
+ let gsapZeroReturn = 0;
1061
+ const gsapScrollToZero = (cls) => {
1062
+ if (gsapAtZero) {
1063
+ const back = window.scrollY;
1064
+ if (back === 0) return () => {};
1065
+ const fromRest = !isUserScrolling(cls);
1066
+ window.scrollTo({
1067
+ top: 0,
1068
+ behavior: "instant"
1069
+ });
1070
+ return () => {
1071
+ window.scrollTo({
1072
+ top: back,
1073
+ behavior: "instant"
1074
+ });
1075
+ markOwnScroll(fromRest);
1076
+ };
1077
+ }
1078
+ const record = scrollRecord(cls, true);
1079
+ if (!record || record.read() === 0) return () => {};
1080
+ const fromRest = !isUserScrolling(cls);
1081
+ gsapZeroReturn = record.read();
1082
+ dormancyAtScrollZero();
1083
+ window.scrollTo({
1084
+ top: 0,
1085
+ behavior: "instant"
1086
+ });
1087
+ gsapAtZero = true;
1088
+ return () => {
1089
+ gsapAtZero = false;
1090
+ window.scrollTo({
1091
+ top: record.read(),
1092
+ behavior: "instant"
1093
+ });
1094
+ markOwnScroll(fromRest);
1095
+ };
1096
+ };
1097
+ const isGsapAtZero = () => gsapAtZero;
1098
+ const realScroll = () => scrollZeroReturn ?? (gsapAtZero ? gsapZeroReturn : null);
1099
+ const mayBeStuckAround = (from, own) => {
1100
+ const seen = new Set();
1101
+ const moved = movedPastFreeze();
1102
+ return from.some((el) => {
1103
+ for (let current = el; current && !seen.has(current) && current !== document.body && current !== document.documentElement; current = current.parentElement) {
1104
+ seen.add(current);
1105
+ if (own.has(current)) continue;
1106
+ if (moved.has(current) || getComputedStyle(current).position === "sticky") return true;
1107
+ }
1108
+ return false;
1109
+ });
1110
+ };
1111
+ const afterFunction = () => {
1112
+ if (gsapAtZero || scrollZeroReturn !== null) scrollBackTo(0);
1113
+ if (scrollZeroReturn !== null) dormancyAtScrollZero();
1114
+ };
1115
+ const withAfterFunction = (value) => {
1116
+ if (typeof value !== "function") return value;
1117
+ return () => {
1118
+ try {
1119
+ return value();
1120
+ } finally {
1121
+ afterFunction();
1122
+ }
1123
+ };
1124
+ };
1125
+ const suspendLiveEvaluations = (evaluation, resolving) => {
1126
+ const resumes = [];
1127
+ for (let current = evaluation; current; current = current.outer) resumes.unshift(current.suspend(resolving && current === evaluation));
1128
+ return () => resumes.forEach((resume) => resume());
1129
+ };
1130
+ let liveEvaluation = null;
1131
+ let layerHandlerDepth = 0;
1132
+ const asLayerHandler = (handle) => {
1133
+ layerHandlerDepth += 1;
1134
+ try {
1135
+ return handle();
1136
+ } finally {
1137
+ layerHandlerDepth -= 1;
1138
+ }
1139
+ };
1140
+ const inLayerHandler = () => layerHandlerDepth > 0;
1141
+ const holdLiveEvaluationForGsapRefresh = (cls) => {
1142
+ const evaluation = liveEvaluation;
1143
+ const real = realScroll();
1144
+ if (real !== null && !evaluation) {
1145
+ const zeroRecord = scrollRecord(cls, true);
1146
+ if (zeroRecord && Math.abs(zeroRecord.read() - real) >= .5) {
1147
+ zeroRecord.write(real);
1148
+ setGsapRefreshReturn(real);
1149
+ }
1150
+ }
1151
+ if (!evaluation) return;
1152
+ const resume = suspendLiveEvaluations(evaluation, false);
1153
+ const record = scrollRecord(cls, true);
1154
+ if (record && Math.abs(record.read() - evaluation.scroll) >= .5) {
1155
+ scrollBackTo(Math.round(evaluation.scroll));
1156
+ syncScrollCache(cls);
1157
+ const unclamped = window.scrollY >= Math.round(evaluation.scroll) - .5;
1158
+ const reached = realScroll() !== null && unclamped ? evaluation.scroll : window.scrollY;
1159
+ record.write(reached);
1160
+ setGsapRefreshReturn(reached);
1161
+ }
1162
+ const depth = layerHandlerDepth - 1;
1163
+ let held = true;
1164
+ liveEvaluation = null;
1165
+ const onRefresh = () => {
1166
+ if (!held || layerHandlerDepth > depth) return;
1167
+ held = false;
1168
+ resume();
1169
+ if (!evaluation.ended) liveEvaluation = evaluation;
1170
+ };
1171
+ addGsapListener(cls, "refresh", onRefresh);
1172
+ queueMicrotask(() => removeGsapListener(cls, "refresh", onRefresh));
1173
+ };
1174
+ const evaluateLive = (setUp, scroll, call) => {
1175
+ let takeDown = () => {};
1176
+ let holds = 0;
1177
+ const evaluation = {
1178
+ resolved: false,
1179
+ ended: false,
1180
+ outer: liveEvaluation,
1181
+ scroll: liveEvaluation?.scroll ?? scroll,
1182
+ suspend: (resolving) => {
1183
+ if (resolving) evaluation.resolved = true;
1184
+ holds += 1;
1185
+ if (holds === 1) {
1186
+ takeDown();
1187
+ takeDown = () => {};
1188
+ }
1189
+ return () => {
1190
+ holds -= 1;
1191
+ if (holds === 0 && !evaluation.ended) takeDown = setUp();
1192
+ };
1193
+ }
1194
+ };
1195
+ try {
1196
+ takeDown = setUp();
1197
+ liveEvaluation = evaluation;
1198
+ const next = call();
1199
+ return evaluation.resolved ? null : next;
1200
+ } finally {
1201
+ evaluation.ended = true;
1202
+ liveEvaluation = evaluation.outer;
1203
+ takeDown();
1204
+ }
1205
+ };
1206
+ const resolveOutsideEvaluation = (read) => {
1207
+ const resume = liveEvaluation && suspendLiveEvaluations(liveEvaluation, true);
1208
+ try {
1209
+ return read();
1210
+ } finally {
1211
+ resume?.();
1212
+ }
1213
+ };
739
1214
  const resolveMaybeFn = (value) => typeof value === "function" ? value() : value;
740
1215
  const KEYWORD_FRACTIONS = {
741
1216
  top: 0,
@@ -1325,6 +1800,8 @@ var StickyScrollTrigger = (function() {
1325
1800
  return finalPlans;
1326
1801
  };
1327
1802
  const OFFSET_PROPERTY = "--sst-scroll-margin-top-offset";
1803
+ const PRIORITY = "important";
1804
+ const snapshots = new WeakMap();
1328
1805
  let nextInstanceSuffix = 0;
1329
1806
  const supportsScrollDrivenAnimations = typeof CSS !== "undefined" && typeof CSS.supports === "function" && CSS.supports("animation-timeline", "scroll()");
1330
1807
  const supportsConstructedStyleSheets = typeof CSSStyleSheet === "function" && typeof CSSStyleSheet.prototype.replaceSync === "function" && typeof document !== "undefined" && Array.isArray(document.adoptedStyleSheets);
@@ -1348,7 +1825,6 @@ var StickyScrollTrigger = (function() {
1348
1825
  const createScrollMarginSync = (rootElement, targetSelector) => {
1349
1826
  const instanceId = `sst${nextInstanceSuffix++}`;
1350
1827
  const usesJsRamp = !usesCssRamp && targetSelector !== null;
1351
- const snapshots = new WeakMap();
1352
1828
  let written = new Set();
1353
1829
  let sheet = null;
1354
1830
  let animationHost = null;
@@ -1365,7 +1841,7 @@ var StickyScrollTrigger = (function() {
1365
1841
  if (usesJsRamp) window.addEventListener("scroll", onScroll, { passive: true });
1366
1842
  const restoreTarget = (target) => {
1367
1843
  const snapshot = snapshots.get(target);
1368
- target.style.scrollMarginTop = snapshot ? snapshot.inline : "";
1844
+ if (snapshot?.owner === instanceId) restoreIfWritten(target, "scroll-margin-top", snapshot.authored, snapshot.written, PRIORITY);
1369
1845
  };
1370
1846
  const stopRamps = () => {
1371
1847
  if (sheet) {
@@ -1412,8 +1888,12 @@ var StickyScrollTrigger = (function() {
1412
1888
  const naturalTops = measureNaturalTops(targets);
1413
1889
  targets.forEach((target) => {
1414
1890
  const existing = snapshots.get(target);
1415
- if (!existing) snapshots.set(target, { inline: target.style.scrollMarginTop });
1416
- else if (written.has(target)) target.style.scrollMarginTop = existing.inline;
1891
+ if (existing && restoreIfWritten(target, "scroll-margin-top", existing.authored, existing.written, PRIORITY)) return;
1892
+ snapshots.set(target, {
1893
+ authored: captureInline(target, "scroll-margin-top"),
1894
+ written: "",
1895
+ owner: instanceId
1896
+ });
1417
1897
  });
1418
1898
  const consumed = ramps.map((_, index) => `var(--${instanceId}-c${index}, 0px)`).join(" + ");
1419
1899
  targets.forEach((target, targetIndex) => {
@@ -1424,7 +1904,9 @@ var StickyScrollTrigger = (function() {
1424
1904
  earlierDwell += freezeEnd - freezeStart;
1425
1905
  return `clamp(0px, (${margin}px - var(${OFFSET_PROPERTY}, 0px)) * 1000000, ${freezeEnd - freezeStart}px)`;
1426
1906
  }).join(" + ");
1427
- target.style.scrollMarginTop = `calc(${authorPx}px + var(${OFFSET_PROPERTY}, 0px) + ${consumed} - (${lag}))`;
1907
+ const snapshot = snapshots.get(target);
1908
+ snapshot.written = writeInline(target, "scroll-margin-top", `calc(${authorPx}px + var(${OFFSET_PROPERTY}, 0px) + ${consumed} - (${lag}))`, PRIORITY);
1909
+ snapshot.owner = instanceId;
1428
1910
  });
1429
1911
  const next = new Set(targets);
1430
1912
  written.forEach((target) => {
@@ -1471,7 +1953,8 @@ var StickyScrollTrigger = (function() {
1471
1953
  };
1472
1954
  const unbuildStructure = (rootElement, layers, outermostContainer) => {
1473
1955
  if (outermostContainer) {
1474
- outermostContainer.parentNode?.insertBefore(rootElement, outermostContainer);
1956
+ const parent = outermostContainer.parentNode;
1957
+ if (parent) moveInto(parent, rootElement, outermostContainer);
1475
1958
  outermostContainer.remove();
1476
1959
  }
1477
1960
  for (let i = layers.length - 1; i >= 0; i -= 1) {
@@ -1599,11 +2082,6 @@ var StickyScrollTrigger = (function() {
1599
2082
  const index = list.indexOf(item);
1600
2083
  if (index !== -1) list.splice(index, 1);
1601
2084
  };
1602
- const suspendLiveEvaluations = (evaluation, resolving) => {
1603
- const resumes = [];
1604
- for (let current = evaluation; current; current = current.outer) resumes.unshift(current.suspend(resolving && current === evaluation));
1605
- return () => resumes.forEach((resume) => resume());
1606
- };
1607
2085
  const liveEndValue = (spec) => {
1608
2086
  if (spec.mode === "dwell") return spec.distancePx;
1609
2087
  return spec.mode === "absolute" ? spec.value : 0;
@@ -1617,152 +2095,9 @@ var StickyScrollTrigger = (function() {
1617
2095
  let structureChanges = 0;
1618
2096
  let refreshedInGsapRefresh = null;
1619
2097
  const armedInstances = new Set();
1620
- let scrollZeroReturn = null;
1621
2098
  let lastScrollTriggerClass = null;
1622
- let ownScrollTop = null;
1623
- let ownScrollAt = -Infinity;
1624
- const markOwnScroll = (fromRest) => {
1625
- ownScrollTop = fromRest ? window.scrollY : null;
1626
- ownScrollAt = performance.now();
1627
- };
1628
- const isOwnTripOnly = () => {
1629
- if (ownScrollTop !== null && (Math.abs(window.scrollY - ownScrollTop) >= .5 || performance.now() - ownScrollAt >= 500)) ownScrollTop = null;
1630
- return ownScrollTop !== null;
1631
- };
1632
- const isPageScrolling = (cls) => (isRefreshing(cls) ? isScrolling(cls) : isScrollingOrUpdating(cls)) && !isOwnTripOnly();
1633
- const isUserScrolling = (cls) => isScrolling(cls) && !isOwnTripOnly();
1634
- const scrollBackTo = (top) => {
1635
- if (Math.abs(window.scrollY - top) >= .5) window.scrollTo({
1636
- top,
1637
- behavior: "instant"
1638
- });
1639
- };
1640
- const enterScrollZero = (cls) => {
1641
- if (scrollZeroReturn !== null) {
1642
- scrollBackTo(0);
1643
- dormancyAtScrollZero();
1644
- return () => {};
1645
- }
1646
- const fromRest = !isUserScrolling(cls);
1647
- scrollZeroReturn = window.scrollY;
1648
- dormancyAtScrollZero();
1649
- window.scrollTo({
1650
- top: 0,
1651
- behavior: "instant"
1652
- });
1653
- return () => {
1654
- const back = scrollZeroReturn ?? 0;
1655
- scrollZeroReturn = null;
1656
- window.scrollTo({
1657
- top: back,
1658
- behavior: "instant"
1659
- });
1660
- markOwnScroll(fromRest);
1661
- syncScrollCache(cls);
1662
- dormancyAfterScrollZero(window.scrollY);
1663
- };
1664
- };
1665
- let gsapAtZero = false;
1666
- let gsapZeroReturn = 0;
1667
- const gsapScrollToZero = (cls) => {
1668
- if (gsapAtZero) {
1669
- const back = window.scrollY;
1670
- if (back === 0) return () => {};
1671
- const fromRest = !isUserScrolling(cls);
1672
- window.scrollTo({
1673
- top: 0,
1674
- behavior: "instant"
1675
- });
1676
- return () => {
1677
- window.scrollTo({
1678
- top: back,
1679
- behavior: "instant"
1680
- });
1681
- markOwnScroll(fromRest);
1682
- };
1683
- }
1684
- const record = scrollRecord(cls, true);
1685
- if (!record || record.read() === 0) return () => {};
1686
- const fromRest = !isUserScrolling(cls);
1687
- gsapZeroReturn = record.read();
1688
- dormancyAtScrollZero();
1689
- window.scrollTo({
1690
- top: 0,
1691
- behavior: "instant"
1692
- });
1693
- gsapAtZero = true;
1694
- return () => {
1695
- gsapAtZero = false;
1696
- window.scrollTo({
1697
- top: record.read(),
1698
- behavior: "instant"
1699
- });
1700
- markOwnScroll(fromRest);
1701
- };
1702
- };
1703
- const realScroll = () => scrollZeroReturn ?? (gsapAtZero ? gsapZeroReturn : null);
1704
- const mayBeStuckAround = (from, own) => {
1705
- const seen = new Set();
1706
- const moved = movedPastFreeze();
1707
- return from.some((el) => {
1708
- for (let current = el; current && !seen.has(current) && current !== document.body && current !== document.documentElement; current = current.parentElement) {
1709
- seen.add(current);
1710
- if (own.has(current)) continue;
1711
- if (moved.has(current) || getComputedStyle(current).position === "sticky") return true;
1712
- }
1713
- return false;
1714
- });
1715
- };
1716
- const afterFunction = () => {
1717
- if (gsapAtZero || scrollZeroReturn !== null) scrollBackTo(0);
1718
- if (scrollZeroReturn !== null) dormancyAtScrollZero();
1719
- };
1720
- const withAfterFunction = (value) => {
1721
- if (typeof value !== "function") return value;
1722
- return () => {
1723
- try {
1724
- return value();
1725
- } finally {
1726
- afterFunction();
1727
- }
1728
- };
1729
- };
1730
- let liveEvaluation = null;
1731
- let layerHandlerDepth = 0;
1732
- const holdLiveEvaluationForGsapRefresh = (cls) => {
1733
- const evaluation = liveEvaluation;
1734
- const real = realScroll();
1735
- if (real !== null && !evaluation) {
1736
- const zeroRecord = scrollRecord(cls, true);
1737
- if (zeroRecord && Math.abs(zeroRecord.read() - real) >= .5) {
1738
- zeroRecord.write(real);
1739
- setGsapRefreshReturn(real);
1740
- }
1741
- }
1742
- if (!evaluation) return;
1743
- const resume = suspendLiveEvaluations(evaluation, false);
1744
- const record = scrollRecord(cls, true);
1745
- if (record && Math.abs(record.read() - evaluation.scroll) >= .5) {
1746
- scrollBackTo(Math.round(evaluation.scroll));
1747
- syncScrollCache(cls);
1748
- const unclamped = window.scrollY >= Math.round(evaluation.scroll) - .5;
1749
- const reached = realScroll() !== null && unclamped ? evaluation.scroll : window.scrollY;
1750
- record.write(reached);
1751
- setGsapRefreshReturn(reached);
1752
- }
1753
- const depth = layerHandlerDepth - 1;
1754
- let held = true;
1755
- liveEvaluation = null;
1756
- const onRefresh = () => {
1757
- if (!held || layerHandlerDepth > depth) return;
1758
- held = false;
1759
- resume();
1760
- if (!evaluation.ended) liveEvaluation = evaluation;
1761
- };
1762
- addGsapListener(cls, "refresh", onRefresh);
1763
- queueMicrotask(() => removeGsapListener(cls, "refresh", onRefresh));
1764
- };
1765
2099
  const REST_POLL_MS = 100;
2100
+ const refreshedDuringMove = new Map();
1766
2101
  const runEach = (...steps) => {
1767
2102
  let failure = null;
1768
2103
  steps.forEach((step) => {
@@ -1792,6 +2127,7 @@ var StickyScrollTrigger = (function() {
1792
2127
  #refreshInitNestedUsed = false;
1793
2128
  #measureAgain = false;
1794
2129
  #keepContentPending = false;
2130
+ #refreshOwed = false;
1795
2131
  #deferredRun = null;
1796
2132
  #restTimer = null;
1797
2133
  #pendingKeep = null;
@@ -2046,14 +2382,7 @@ var StickyScrollTrigger = (function() {
2046
2382
  const restoreDormancy = takeGsapRefreshRestore();
2047
2383
  return restoreDormancy ? revertAfterMeasure(restoreDormancy, result) : result;
2048
2384
  };
2049
- return (self) => {
2050
- layerHandlerDepth += 1;
2051
- try {
2052
- return handle(self);
2053
- } finally {
2054
- layerHandlerDepth -= 1;
2055
- }
2056
- };
2385
+ return (self) => asLayerHandler(() => handle(self));
2057
2386
  }
2058
2387
  #watchGsapRefresh() {
2059
2388
  if (this.#dispatchObserver) {
@@ -2117,16 +2446,28 @@ var StickyScrollTrigger = (function() {
2117
2446
  });
2118
2447
  }
2119
2448
  #reservePinSpace() {
2120
- const wrapped = this.#pinLayers.flatMap(({ outer, inner }) => outer && inner ? [{
2449
+ const wrapped = this.#pinLayers.flatMap(({ outer, inner, trigger }) => outer && inner ? [{
2121
2450
  outer,
2122
2451
  inner,
2452
+ trigger,
2453
+ savedStyle: outer.style.cssText,
2123
2454
  range: inner.style.height
2124
2455
  }] : []);
2125
2456
  wrapped.forEach(({ outer, inner }) => {
2126
2457
  clearHeightForRead(outer);
2127
2458
  clearHeightForRead(inner);
2128
2459
  });
2129
- const reserved = wrapped.map(({ outer }) => measureUsedHeight(outer));
2460
+ let reserved;
2461
+ try {
2462
+ const itemHeights = fitPinsToItems(wrapped);
2463
+ reserved = wrapped.map(({ outer }, index) => itemHeights[index] ?? measureUsedHeight(outer));
2464
+ } catch (error) {
2465
+ wrapped.forEach(({ outer, inner, savedStyle, range }) => {
2466
+ commitCssText(outer, savedStyle);
2467
+ commitHeight(inner, range);
2468
+ });
2469
+ throw error;
2470
+ }
2130
2471
  wrapped.forEach(({ outer }, index) => {
2131
2472
  commitHeight(outer, `${reserved[index]}px`);
2132
2473
  });
@@ -2154,10 +2495,10 @@ var StickyScrollTrigger = (function() {
2154
2495
  static #resetStickyAround(self, from) {
2155
2496
  const withClass = self ?? [...armedInstances].find((instance) => instance.#scrollTriggerClass);
2156
2497
  const cls = (withClass ? withClass.#scrollTriggerClass : null) ?? lastScrollTriggerClass;
2157
- if (scrollZeroReturn !== null) return enterScrollZero(cls);
2498
+ if (scrollZeroReturnTop() !== null) return enterScrollZero(cls);
2158
2499
  const refreshing = isRefreshing(cls);
2159
- if (refreshing && gsapAtZero) return gsapScrollToZero(cls);
2160
- if (!(refreshing && layerHandlerDepth > 0) && !isPageScrolling(cls) && window.scrollY !== 0) {
2500
+ if (refreshing && isGsapAtZero()) return gsapScrollToZero(cls);
2501
+ if (!(refreshing && inLayerHandler()) && !isPageScrolling(cls) && window.scrollY !== 0) {
2161
2502
  const own = new Set(self ? self.#ownedStickyElements() : []);
2162
2503
  return mayBeStuckAround(from, own) ? enterScrollZero(cls) : () => {};
2163
2504
  }
@@ -2265,10 +2606,11 @@ var StickyScrollTrigger = (function() {
2265
2606
  const releaseScroll = resolvePinReleaseScroll(layer.endTrigger, classifiedEnd, toScroll(layer.trigger, triggerTop - topPx), viewportHeight, toScroll);
2266
2607
  const height = releaseScroll - (this.#rootElement.contains(layer.trigger) ? dwellConsumedAt(releaseScroll, this.#engagedWindows()) : 0) - triggerTop + topPx + triggerHeight + triggerMarginBottom;
2267
2608
  const room = documentHeight > 0 ? documentHeight - triggerTop : Infinity;
2609
+ const still = triggerHeight + triggerMarginBottom;
2268
2610
  return [{
2269
2611
  layer,
2270
2612
  topPx,
2271
- range: Math.max(0, Math.min(height, room))
2613
+ range: Math.max(0, Math.min(Math.max(height, still), room))
2272
2614
  }];
2273
2615
  });
2274
2616
  }
@@ -2377,39 +2719,10 @@ var StickyScrollTrigger = (function() {
2377
2719
  });
2378
2720
  };
2379
2721
  };
2380
- let takeDown = () => {};
2381
- let holds = 0;
2382
- const evaluation = {
2383
- resolved: false,
2384
- ended: false,
2385
- outer: liveEvaluation,
2386
- scroll: liveEvaluation?.scroll ?? scroll,
2387
- suspend: (resolving) => {
2388
- if (resolving) evaluation.resolved = true;
2389
- holds += 1;
2390
- if (holds === 1) {
2391
- takeDown();
2392
- takeDown = () => {};
2393
- }
2394
- return () => {
2395
- holds -= 1;
2396
- if (holds === 0 && !evaluation.ended) takeDown = setUp();
2397
- };
2398
- }
2399
- };
2400
- try {
2401
- takeDown = setUp();
2402
- liveEvaluation = evaluation;
2403
- const next = liveEnd();
2404
- return evaluation.resolved ? null : next;
2405
- } finally {
2406
- evaluation.ended = true;
2407
- liveEvaluation = evaluation.outer;
2408
- takeDown();
2409
- }
2722
+ return evaluateLive(setUp, scroll, liveEnd);
2410
2723
  }
2411
2724
  refresh() {
2412
- if (this.#depth > 0 || isRefreshing(this.#scrollTriggerClass) || this.#destroyed || !this.#armed || pendingDisarms.has(this) || window.scrollY === 0 || this.#measuresInPlace()) {
2725
+ if (this.#depth > 0 || isRefreshing(this.#scrollTriggerClass) || this.#destroyed || !this.#armed || pendingDisarms.has(this) || isMovingDom() || window.scrollY === 0 || this.#measuresInPlace()) {
2413
2726
  this.#refreshFrom(false);
2414
2727
  return;
2415
2728
  }
@@ -2430,6 +2743,13 @@ var StickyScrollTrigger = (function() {
2430
2743
  this.#runRefresh();
2431
2744
  }
2432
2745
  #runRefresh() {
2746
+ if (isMovingDom()) {
2747
+ const cls = isRefreshing(this.#scrollTriggerClass) ? this.#scrollTriggerClass : null;
2748
+ this.#refreshOwed = true;
2749
+ if (refreshedDuringMove.size === 0) queueMicrotask(StickyScrollTrigger.#runRefreshesOwed);
2750
+ refreshedDuringMove.set(this, refreshedDuringMove.get(this) ?? cls);
2751
+ return;
2752
+ }
2433
2753
  if (this.#depth > 0) this.#measureAgain = true;
2434
2754
  if (this.#destroyed || !this.#armed || pendingDisarms.has(this)) return;
2435
2755
  let failure = null;
@@ -2446,6 +2766,7 @@ var StickyScrollTrigger = (function() {
2446
2766
  if (this.#depth === 0) {
2447
2767
  this.#refreshInitNestedUsed = false;
2448
2768
  this.#measureAgain = false;
2769
+ this.#refreshOwed = false;
2449
2770
  }
2450
2771
  }
2451
2772
  try {
@@ -2460,12 +2781,19 @@ var StickyScrollTrigger = (function() {
2460
2781
  this.#scrollMarginSync?.applyRamps(realScroll() ?? scrollRecord(this.#scrollTriggerClass)?.read());
2461
2782
  if (failure) throw failure.error;
2462
2783
  }
2784
+ static #runRefreshesOwed() {
2785
+ const owed = [...refreshedDuringMove];
2786
+ refreshedDuringMove.clear();
2787
+ const classes = new Set(owed.flatMap(([, cls]) => cls ? [cls] : []));
2788
+ const direct = owed.flatMap(([instance, cls]) => !cls && instance.#refreshOwed ? [instance] : []);
2789
+ runEach(...[...classes].map((cls) => () => refreshAll(cls)), ...direct.map((instance) => () => instance.refresh()));
2790
+ }
2463
2791
  #naturalScrollTop(layers) {
2464
2792
  const measured = this.#measuredStructure;
2465
2793
  const container = this.#outermostContainer;
2466
- const containerTop = (el) => readThenRestore(isRefreshing(this.#scrollTriggerClass) && layerHandlerDepth > 0 ? gsapScrollToZero(this.#scrollTriggerClass) : StickyScrollTrigger.#resetStickyAround(this, [el]), () => documentTop(el));
2794
+ const containerTop = (el) => readThenRestore(isRefreshing(this.#scrollTriggerClass) && inLayerHandler() ? gsapScrollToZero(this.#scrollTriggerClass) : StickyScrollTrigger.#resetStickyAround(this, [el]), () => documentTop(el));
2467
2795
  const moved = measured && container && measured.changes !== structureChanges ? containerTop(container) - measured.top : 0;
2468
- const scroll = (scrollRecord(this.#scrollTriggerClass)?.read() ?? scrollZeroReturn ?? window.scrollY) - moved;
2796
+ const scroll = (scrollRecord(this.#scrollTriggerClass)?.read() ?? scrollZeroReturnTop() ?? window.scrollY) - moved;
2469
2797
  const consumed = new Map();
2470
2798
  layers.forEach((layer) => {
2471
2799
  if (layer.kind !== "scene") return;
@@ -2483,8 +2811,9 @@ var StickyScrollTrigger = (function() {
2483
2811
  record.write(target);
2484
2812
  return;
2485
2813
  }
2486
- if (scrollZeroReturn !== null) {
2487
- if (Math.abs(scrollZeroReturn - target) >= .5) scrollZeroReturn = Math.round(target);
2814
+ const zeroReturn = scrollZeroReturnTop();
2815
+ if (zeroReturn !== null) {
2816
+ if (Math.abs(zeroReturn - target) >= .5) moveScrollZeroReturn(Math.round(target));
2488
2817
  return;
2489
2818
  }
2490
2819
  if (isRefreshing(cls)) return;
@@ -2529,8 +2858,7 @@ var StickyScrollTrigger = (function() {
2529
2858
  }
2530
2859
  #liftCovers() {
2531
2860
  this.#coverLayers().forEach((layer) => {
2532
- const pin = this.#pinLayers.find(({ trigger }) => trigger === layer.cover);
2533
- this.#coverRestoreByLayer.set(layer, liftAboveStickyWrapper(pin?.outer ?? layer.cover));
2861
+ this.#coverRestoreByLayer.set(layer, liftAboveStickyWrapper(physicalHolder(layer.cover)));
2534
2862
  });
2535
2863
  }
2536
2864
  #syncScrollMargins(tops) {
@@ -2617,9 +2945,10 @@ var StickyScrollTrigger = (function() {
2617
2945
  const trigger = resolveElement(triggerInput, "createOverlapScroll");
2618
2946
  this.#assertTriggerInsideRoot(trigger, "createOverlapScroll");
2619
2947
  const endTrigger = resolveEndTrigger(trigger, endTriggerInput, "createOverlapScroll");
2620
- const coverElement = cover === void 0 ? trigger.nextElementSibling : resolveElement(cover, "createOverlapScroll");
2621
- if (!(coverElement instanceof HTMLElement)) throw new Error(`createOverlapScroll: cover element (trigger.nextElementSibling) not found for trigger ${describeElement(trigger)}`);
2622
- if (coverElement.parentNode !== trigger.parentNode) throw new Error(`createOverlapScroll: cover ${describeElement(coverElement)} must be a sibling of trigger ` + describeElement(trigger));
2948
+ const ownWrappers = new Set(this.#coverLayers().flatMap(({ wrapper }) => wrapper ? [wrapper] : []));
2949
+ const coverElement = cover === void 0 ? logicalNextSibling(trigger, ownWrappers) : resolveElement(cover, "createOverlapScroll");
2950
+ if (!(coverElement instanceof HTMLElement)) throw new Error(`createOverlapScroll: cover element (trigger's next sibling) not found for trigger ${describeElement(trigger)}`);
2951
+ if (logicalParent(coverElement, ownWrappers) !== logicalParent(trigger, ownWrappers)) throw new Error(`createOverlapScroll: cover ${describeElement(coverElement)} must be a sibling of trigger ` + describeElement(trigger));
2623
2952
  const layer = {
2624
2953
  kind: "cover",
2625
2954
  trigger,
@@ -2638,7 +2967,7 @@ var StickyScrollTrigger = (function() {
2638
2967
  unbuildCover(layer);
2639
2968
  onKill?.(self);
2640
2969
  }, rest);
2641
- this.#coverRestoreByLayer.set(layer, liftAboveStickyWrapper(coverElement));
2970
+ this.#coverRestoreByLayer.set(layer, liftAboveStickyWrapper(physicalHolder(coverElement)));
2642
2971
  return vars;
2643
2972
  }
2644
2973
  createStickyPin({ trigger: triggerInput, start, top, endTrigger: endTriggerInput, end = "bottom top", onKill, ...rest }) {
@@ -2677,12 +3006,7 @@ var StickyScrollTrigger = (function() {
2677
3006
  };
2678
3007
  }
2679
3008
  resolveScrollPosition(elementInput, position) {
2680
- const resume = liveEvaluation && suspendLiveEvaluations(liveEvaluation, true);
2681
- try {
2682
- return this.#resolveScrollPosition(elementInput, position);
2683
- } finally {
2684
- resume?.();
2685
- }
3009
+ return resolveOutsideEvaluation(() => this.#resolveScrollPosition(elementInput, position));
2686
3010
  }
2687
3011
  #resolveScrollPosition(elementInput, position) {
2688
3012
  const element = resolveElement(elementInput, "resolveScrollPosition");
@@ -2719,7 +3043,7 @@ var StickyScrollTrigger = (function() {
2719
3043
  try {
2720
3044
  return readThenRestore(StickyScrollTrigger.#resetStickyAround(null, [element]), () => documentTop(element));
2721
3045
  } finally {
2722
- restores.forEach((restore) => restore());
3046
+ restores.reverse().forEach((restore) => restore());
2723
3047
  }
2724
3048
  }
2725
3049
  createResolvedTrigger({ trigger: triggerInput, start = "top bottom", end = "bottom top", endTrigger: endTriggerInput, ...rest }) {