harmonize on a single getWindowScroll (#1054)

* Fix issue where only indication I could see in any attribute that the document was scrolling was on `window.pageYOffset`, so we hadn't been able to replay scrolling

* Apply formatting changes

* Update observer.ts

help you fix typescript error

* Update utils.ts

help you fix typescript error

Co-authored-by: eoghanmurray <eoghanmurray@users.noreply.github.com>
Co-authored-by: Yun Feng <yun.feng0817@gmail.com>
This commit is contained in:
Eoghan Murray
2026-04-01 12:00:00 +08:00
committed by GitHub
parent bad9c131e9
commit c62f888ec5
3 changed files with 29 additions and 20 deletions

View File

@@ -9,6 +9,7 @@ import {
on, on,
getWindowWidth, getWindowWidth,
getWindowHeight, getWindowHeight,
getWindowScroll,
polyfill, polyfill,
hasShadowRoot, hasShadowRoot,
isSerializedIframe, isSerializedIframe,
@@ -379,22 +380,7 @@ function record<T = eventWithTime>(
type: EventType.FullSnapshot, type: EventType.FullSnapshot,
data: { data: {
node, node,
initialOffset: { initialOffset: getWindowScroll(window),
left:
window.pageXOffset !== undefined
? window.pageXOffset
: document?.documentElement.scrollLeft ||
document?.body?.parentElement?.scrollLeft ||
document?.body?.scrollLeft ||
0,
top:
window.pageYOffset !== undefined
? window.pageYOffset
: document?.documentElement.scrollTop ||
document?.body?.parentElement?.scrollTop ||
document?.body?.scrollTop ||
0,
},
}, },
}), }),
); );

View File

@@ -4,6 +4,7 @@ import {
throttle, throttle,
on, on,
hookSetter, hookSetter,
getWindowScroll,
getWindowHeight, getWindowHeight,
getWindowWidth, getWindowWidth,
isBlocked, isBlocked,
@@ -279,12 +280,12 @@ export function initScrollObserver({
return; return;
} }
const id = mirror.getId(target as Node); const id = mirror.getId(target as Node);
if (target === doc) { if (target === doc && doc.defaultView) {
const scrollEl = (doc.scrollingElement || doc.documentElement)!; const scrollLeftTop = getWindowScroll(doc.defaultView);
scrollCb({ scrollCb({
id, id,
x: scrollEl.scrollLeft, x: scrollLeftTop.left,
y: scrollEl.scrollTop, y: scrollLeftTop.top,
}); });
} else { } else {
scrollCb({ scrollCb({

View File

@@ -167,6 +167,28 @@ export function patch(
} }
} }
export function getWindowScroll(win: Window) {
const doc = win.document;
return {
left: doc.scrollingElement
? doc.scrollingElement.scrollLeft
: win.pageXOffset !== undefined
? win.pageXOffset
: doc?.documentElement.scrollLeft ||
doc?.body?.parentElement?.scrollLeft ||
doc?.body?.scrollLeft ||
0,
top: doc.scrollingElement
? doc.scrollingElement.scrollTop
: win.pageYOffset !== undefined
? win.pageYOffset
: doc?.documentElement.scrollTop ||
doc?.body?.parentElement?.scrollTop ||
doc?.body?.scrollTop ||
0,
};
}
export function getWindowHeight(): number { export function getWindowHeight(): number {
return ( return (
window.innerHeight || window.innerHeight ||