Smooth Scroll to Element
Smoothly scrolls the webpage to a target HTML element, with optional vertical pixel offset.
smoothScrollTo
Smoothly scrolls the webpage to a target HTML element, with optional vertical pixel offset for precise positioning (e.g. accounting for sticky headers).
Function Signature
function smoothScrollTo(element: HTMLElement, offset?: number): voidParameters
| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | The target DOM element to scroll to. |
offset | number (optional) | Vertical pixel offset (positive shifts viewport further down, negative shifts it up). |
Returns
void: This function does not return a value.
Example Usage
import { smoothScrollTo } from 'toolbox-x/dom';
const section = document.getElementById('contact-us');
if (section) {
smoothScrollTo(section);
}Behavior Details
- Native Animation: Uses the browser's native
scrollIntoView({ behavior: 'smooth' })when no offset is provided. - Offset Handling: When an offset is specified, the function calculates the target element's absolute Y-coordinate relative to the document and uses
window.scrollTowith smooth behavior. - Browser Only: This function depends on DOM APIs and must only be executed client-side.
Last updated: Mon, Jun 15, 2026 11:04:47AM (UTC)
