Help Angular by taking a 1 minute survey!Go to surveyHome

ViewportScroller

Defines a scroll position manager. Implemented by BrowserViewportScroller.

      
      abstract class ViewportScroller {
  static ngInjectableDef: defineInjectable({...})
  abstract setOffset(offset: [number, number] | (() => [number, number])): void
  abstract getScrollPosition(): [number, number]
  abstract scrollToPosition(position: [number, number]): void
  abstract scrollToAnchor(anchor: string): void
  abstract setHistoryScrollRestoration(scrollRestoration: "auto" | "manual"): void
}
    

Static properties

Property Description
static ngInjectableDef: defineInjectable({ providedIn: 'root', factory: () => new BrowserViewportScroller(inject(DOCUMENT), window, inject(ErrorHandler)) })

Methods

Configures the top offset used when scrolling to an anchor.

abstract setOffset(offset: [number, number] | (() => [number, number])): void
      
      abstract setOffset(offset: [number, number] | (() => [number, number])): void
    
Parameters
offset [number, number] | (() => [number, number])

A position in screen coordinates (a tuple with x and y values) or a function that returns the top offset position.

Returns

void

Retrieves the current scroll position.

abstract getScrollPosition(): [number, number]
      
      abstract getScrollPosition(): [number, number]
    
Parameters

There are no parameters.

Returns

[number, number]: A position in screen coordinates (a tuple with x and y values).

Scrolls to a specified position.

abstract scrollToPosition(position: [number, number]): void
      
      abstract scrollToPosition(position: [number, number]): void
    
Parameters
position [number, number]

A position in screen coordinates (a tuple with x and y values).

Returns

void

Scrolls to an anchor element.

abstract scrollToAnchor(anchor: string): void
      
      abstract scrollToAnchor(anchor: string): void
    
Parameters
anchor string

The ID of the anchor element.

Returns

void

Disables automatic scroll restoration provided by the browser. See also window.history.scrollRestoration info.

abstract setHistoryScrollRestoration(scrollRestoration: "auto" | "manual"): void
      
      abstract setHistoryScrollRestoration(scrollRestoration: "auto" | "manual"): void
    
Parameters
scrollRestoration "auto" | "manual"
Returns

void