MCPcopy Create free account
hub / github.com/Qinbf/groundmap / scrollElementToTop

Function scrollElementToTop

web/components/HashScroller.tsx:125–136  ·  view source on GitHub ↗

* 把 el 滚到其滚动容器的顶部。 * * 不用 el.scrollIntoView:在多层 overflow 嵌套(如 PageColumns 的 main)下, * 浏览器对"该让谁滚"判断不稳;显式找最近 scroll ancestor 自己算 offset 最可控。

(el: HTMLElement, topOffset = 12)

Source from the content-addressed store, hash-verified

123 * 浏览器对"该让谁滚"判断不稳;显式找最近 scroll ancestor 自己算 offset 最可控。
124 */
125function scrollElementToTop(el: HTMLElement, topOffset = 12) {
126 const scrollable = findScrollableAncestor(el);
127 if (scrollable instanceof Window) {
128 const top = el.getBoundingClientRect().top + window.scrollY - topOffset;
129 window.scrollTo({ top, behavior: "smooth" });
130 } else {
131 const elRect = el.getBoundingClientRect();
132 const containerRect = scrollable.getBoundingClientRect();
133 const top = elRect.top - containerRect.top + scrollable.scrollTop - topOffset;
134 scrollable.scrollTo({ top, behavior: "smooth" });
135 }
136}
137
138/**
139 * 等待 main 容器的 scrollHeight 稳定(连续 N 帧不变),表示 React 渲染 + layout 完成;

Callers 1

tryScrollFunction · 0.85

Calls 1

findScrollableAncestorFunction · 0.85

Tested by

no test coverage detected