File size: 1,208 Bytes
c453128 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | /** Scroll `el` within `container` only — does not move outer floor/strip scrollers. */
export function scrollIntoContainer(
container: HTMLElement,
el: HTMLElement,
block: "start" | "center" | "end" | "nearest" = "end",
): void {
const cRect = container.getBoundingClientRect();
const eRect = el.getBoundingClientRect();
if (block === "end") {
if (eRect.bottom > cRect.bottom) container.scrollTop += eRect.bottom - cRect.bottom;
else if (eRect.top < cRect.top) container.scrollTop += eRect.top - cRect.top;
return;
}
if (block === "start") {
if (eRect.top < cRect.top) container.scrollTop += eRect.top - cRect.top;
else if (eRect.bottom > cRect.bottom) container.scrollTop += eRect.bottom - cRect.bottom;
return;
}
if (block === "center") {
container.scrollTop += eRect.top - cRect.top - container.clientHeight / 2 + eRect.height / 2;
return;
}
// nearest
if (eRect.bottom > cRect.bottom) container.scrollTop += eRect.bottom - cRect.bottom;
else if (eRect.top < cRect.top) container.scrollTop += eRect.top - cRect.top;
}
export function scrollContainerToBottom(container: HTMLElement): void {
container.scrollTop = container.scrollHeight;
}
|