• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Scroll to the Top of the Page

Using JavaScript to Scroll to the Top of the Page

April 3, 2022 Leave a Comment

We can use JavaScript to scroll to the top of the page by using the scrollTo method along with the top and behavior properties.

window.scrollTo({ top: 0, behavior: 'smooth' });

Let’s say I have the following HTML:

<div id="top-link">Top</div>

To scroll to the top of the page, we can use the scrollTo() method and pass it the top and behavior properties. We set the top property to 0 so that it scrolls to the top of the page, and behavior to “smooth” to get a smooth scroll to the top.

Here is the JavaScript code:

window.scrollTo({ top: 0, behavior: 'smooth' });

Note that we can also do this with jQuery using the animate method.

Using JavaScript to Scroll to the Top of the Page With a Click

Let’s say we have the following HTML code and we want to scroll smoothly to the top of the page.

<div id="click-me" onclick="scrollToTop()">Scroll Top</div>

We can utilize both an onclick event method along with the Window scrollTo() method to scroll to the top of the page.

We add an onclick event to the button so that we can scroll to the top of the page when the user clicks a button.

Below is the JavaScript code which will allow the user to scroll to the top of the page:

function scrollToTop() {
  window.scrollTo({ top: 0, behavior: 'smooth' });
};

The final code and output for this example of how to scroll to the top of the page using JavaScript is below.

Code Output:

Scroll Top

Full Code:

<div id="click-me" onclick="scrollToTop()">Scroll Top</div>

<script>

function scrollToTop() {
  window.scrollTo({ top: 0, behavior: 'smooth' });
};

</script>

Hopefully this article has been useful for you to understand how to use JavaScript to scroll to the top of the page.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Declare Multiple Variables
  • 2.  Using JavaScript to Resize an Image
  • 3.  JavaScript Random Number – How to Generate a Random Number In JavaScript
  • 4.  Finding the Length of a String in JavaScript
  • 5.  Using JavaScript to Get the Height of an Element
  • 6.  JavaScript value – Get the Value from an Input Field
  • 7.  JavaScript tanh – Find Hyperbolic Tangent of Number Using Math.tanh()
  • 8.  Remove Special Characters From String in JavaScript
  • 9.  JavaScript Array includes Method
  • 10.  Using JavaScript to Replace a Space with an Underscore

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy