Skip to content

Documentation / @andrew_l/dom / fastSmoothScroll

Function: fastSmoothScroll()

fastSmoothScroll(container, element, position, margin, maxDistance, forceDirection?, forceDuration?, forceNormalContainerHeight?, onComplete?): void

Smoothly scrolls the specified container to bring the given element into view, with optional customization for scroll behavior, direction, and duration.

This function calculates the optimal scroll position based on the given parameters and smoothly scrolls the container to that position. It can adjust the scroll behavior for accessibility, animation duration, and other constraints.

Parameters

container: HTMLElement

The container element that will be scrolled.

element: HTMLElement

The element inside the container that needs to be brought into view.

position: ScrollLogicalPosition | "centerOrTop"

The scroll position logic ('centerOrTop', ScrollLogicalPosition).

margin: number = 0

A margin in pixels to be applied around the element when scrolling into view (default is 0).

maxDistance: number = FAST_SMOOTH_MAX_DISTANCE

The maximum distance for smooth scrolling (default is FAST_SMOOTH_MAX_DISTANCE).

forceDirection?: FocusDirection

Optional, the direction to force the scroll (FocusDirection). If not provided, the default scroll direction is calculated.

forceDuration?: number

Optional, a custom duration (in milliseconds) for the smooth scroll. If not provided, a default duration is used.

forceNormalContainerHeight?: boolean

Optional, a flag indicating whether to force the container height calculation for normal behavior.

onComplete?: ScrollCompleteCallback

Optional callback function that is triggered when the scroll operation completes.

Returns

void

Example

ts
fastSmoothScroll(container, element, 'centerOrTop', 10, 500, FocusDirection.Up, 300);