Home / Snippets / Scroll to Top One-Liner Snippet
Scroll to Top One-Liner Snippet cover

Scroll to Top One-Liner Snippet

170

2 years ago

0 comments

There is a method called scrollTo(x-coordinate, y-coordinate), which allows you to scroll to a particular set of used coordinates. By making use of this one-liner code you will be able to scroll anywhere that you have bind on your web page.
const x = 100; // or any values
const y = 50; // or any values

const scrollToTop = () => window.scrollTo(x, y);

In addition to that, you can also set to have a smooth scrolling animation by adding the "smooth" behavior option.
const scrollToTop = () => window.scrollTo({ top: 100, behavior: 'smooth' });
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this