ServiceOptional

WrScroll

Scroll to an element, id, selector or position, with an offset for sticky headers.

Installation

Import the class. This entry point has no stylesheet.

import { WrScroll } from 'ngwr/scroll';

Usage

Pass container to scroll an element instead of the window.

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.

API

NameDescriptionTypeDefault
to(target, options?)Scroll to an element, id (#foo), selector, or { top, left } coords.(t, opts?) => void—
intoView(el, options?)Scroll to an element. Same options as to().(el, opts?) => void—
toTop(options?)Scroll the page (or container) to the top.(opts?) => void—
options.offsetPixels subtracted from the target, for a sticky header. No effect on toTop().number0
options.smoothSmooth or instant. Always instant under prefers-reduced-motion: reduce.booleantrue
options.containerThe element to scroll instead of the window.Window | Elementwindow

Dependencies

See also