Scrolls the specified container to the top.
Supports window or any HTMLElement with optional smooth scrolling and offset. Falls back to instant scroll if smooth scrolling is not supported.
window
HTMLElement
Scroll container (window or HTMLElement). Defaults to window.
Scroll behavior: "smooth" or "auto". Defaults to "smooth".
"smooth"
"auto"
Optional offset from top. Can be negative. Defaults to 0.
0
scrollToTop(); // Scroll window smoothly to topconst container = document.querySelector("#chat");if (container) scrollToTop(container, "auto", -20); // Scroll a div instantly with offset Copy
scrollToTop(); // Scroll window smoothly to topconst container = document.querySelector("#chat");if (container) scrollToTop(container, "auto", -20); // Scroll a div instantly with offset
1.2.0
Scrolls the specified container to the top.
Supports
window
or anyHTMLElement
with optional smooth scrolling and offset. Falls back to instant scroll if smooth scrolling is not supported.