• 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 / jQuery / Using jQuery to Scroll to Top

Using jQuery to Scroll to Top

November 30, 2021 Leave a Comment

We can use jQuery to scroll to the top of the page by using the jQuery animate method along with the scrollTop property.

$("html, body").animate({ scrollTop: 0 }, "slow");

Let’s say I have the following HTML:

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

To scroll to top of the page, we can use the jQuery animate method in the following JavaScript code:

$("html, body").animate({ scrollTop: 0 }, "slow");

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("html, body").animate({ scrollTop: 0 }, "slow");

Note we can also do this in pure JavaScript using the scrollTo() method.

Scrolling to Top of the Page On Click Using jQuery

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">Scroll Top</div>

We can utilize both the jQuery click() method and jQuery animate() method to scroll to the top of the page.

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

$("#click-me").click(function() {
  $("html, body").animate({ scrollTop: 0 }, "slow");
});

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

Code Output:

Scroll Top

Full Code:

<div id="click-me">Scroll Top</div>

<script>

$("#click-me").click(function() {
  $("html, body").animate({ scrollTop: 0 }, "slow");
});

</script>

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

Other Articles You'll Also Like:

  • 1.  jQuery after – Insert HTML After Another Element
  • 2.  Using jQuery to Set the Id of a Div
  • 3.  Using jQuery to Get the Border Width of an Element
  • 4.  Using jQuery to Remove Background Color
  • 5.  Get Selected Option from Dropdown Using jQuery
  • 6.  Set the Height of a Div Using jQuery
  • 7.  Toggle the Visibility of an Element Using jQuery
  • 8.  Using the jQuery fadeTo Method to Change the Opacity of an Element
  • 9.  jQuery Get Last Child
  • 10.  How to Use jQuery to Remove Element

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