Service

WrScroll

Smooth-scroll utility. Resolves elements / ids / selectors / coords; subtracts an offset for sticky headers; auto-falls back to instant scrolling when the user has prefers-reduced-motion: reduce.

Usage

private readonly scroll = inject(WrScroll);

this.scroll.to('#section-three', { offset: 80 });
this.scroll.toTop({ smooth: false });
this.scroll.intoView(myEl, { offset: 64 });

Section one

Jump between sections with the buttons above — the service scrolls this box, not the page.

Section two

Each jump uses scroll.to() with the box as the container.

Section three

The page-top button scrolls the window instead, with an 80px offset.

Why ngwr provides this

scrollIntoView can't offset for a sticky header and window.scrollTo doesn't understand containers or selectors. This wraps both behind one API with offsets, smooth-scroll opt-out, and SSR safety.

API

NameDescriptionTypeDefault
to(target, options?)Scroll to an element, id (#foo), selector, or { top, left } coords.(t, opts?) => void
intoView(el, options?)Convenience for an Element — same options as to.(el, opts?) => void
toTop(options?)Scroll the page (or container) to the top.(opts?) => void
options.offsetPixel offset subtracted from the target (sticky-header compensation).number0
options.smoothSmooth or instant. Auto-falls back to instant for prefers-reduced-motion: reduce.booleantrue
options.containerOverride the scroll container.Window | Elementwindow