Skip to content

scrollToElement

The scrollToElement function smoothly scrolls the page to a target element identified by selector or reference.

Syntax

typescript
scrollToElement(target: string | HTMLElement, options?: ScrollIntoViewOptions): void;

Parameters

NameTypeDescription
targetstring | HTMLElementCSS selector or target element
optionsScrollIntoViewOptions(optional) Scrolling options

Returns

NameTypeDescription
voidDoes not return a value

Examples

typescript
scrollToElement("#top");
// Scrolls to the element with id "top"

const el = document.getElementById("footer");
scrollToElement(el, { behavior: "smooth" });
// Smoothly scrolls to the footer

Notes

  • Useful for navigation in long pages or anchors.

References

Released under the MIT License.