Skip to content

Function: scrollToElement() ​

ts
function scrollToElement(target, options?): boolean;

Scrolls the page so the target element enters the viewport.

Parameters ​

ParameterTypeDescription
targetElement | null | undefinedThe target element.
options{ behavior?: "auto" | "smooth"; offsetTop?: number; }Optional scroll options.
options.behavior?"auto" | "smooth"-
options.offsetTop?number-

Returns ​

boolean

true when the element could be scrolled into view.

Example ​

ts
const el = document.createElement('div')
document.body.appendChild(el)
scrollToElement(el) // => true

Since ​

1.0.0

Released under the MIT License.