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