Skip to content

scrollToTop

Scrolls a page or scrollable element. When no element is provided, it scrolls the current window. Pass an HTMLElement when the scroll position belongs to a sidebar, modal body, panel, or another contained scrolling area.

When options is omitted, the helper uses { top: 0, behavior: 'smooth' }, so it is useful after route changes, pagination changes, filter resets, or workflows where the user should return to the beginning of a view.

Importing

ts
import { scrollToTop } from '@almighty-shogun/utils';

Usage

ts
import { scrollToTop } from '@almighty-shogun/utils';

scrollToTop();
ts
import { scrollToTop } from '@almighty-shogun/utils';

const sidebars = document.getElementsByClassName('VPSidebar');
const sidebar = sidebars[0] as HTMLElement | undefined;

if (sidebar) {
    scrollToTop(sidebar);
}

Parameters

element?: Undefinable<HTMLElement>
Scrollable element to move back to the top. When omitted, the current window is scrolled.

options?: Undefinable<ScrollToOptions>
Native scroll options passed to the target.

Uses

Type signature

ts
declare function scrollToTop(
    element?: Undefinable<HTMLElement>,
    options?: Undefinable<ScrollToOptions>
): void;

All packages are released under the MIT License.