Something went wrong. Try again.
a tool for shared writing and social publishing
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627export function scrollIntoViewIfNeeded( el: Element | null, centerIfNeeded: boolean = true, behavior?: ScrollBehavior,) { if (!el) { return; } let observer = new IntersectionObserver(function ([entry]) { const ratio = entry.intersectionRatio;
if (ratio < 1) { let place = ratio <= 0 && centerIfNeeded ? ("center" as const) : ("nearest" as const); el.scrollIntoView({ block: place, inline: place, behavior: behavior ? behavior : "auto", }); } observer.disconnect(); }); observer.observe(el);}