• 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
  • VBA
  • 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.  How to Use jQuery to Check if an Element is Visible
  • 2.  jQuery mouseleave – An Interactive Example of the mouseleave Method
  • 3.  Using jQuery to Capitalize the First Letter of a String
  • 4.  jQuery fadeIn – Fading In Element in HTML
  • 5.  Using jQuery to Detect Window Resize
  • 6.  jQuery val Method – Get the Value from an Input Field
  • 7.  Sort List of Divs with Data Attribute with Radio Buttons Using Javascript
  • 8.  Using jQuery to Select Multiple Classes
  • 9.  How to Set Checkbox Checked With jQuery
  • 10.  jQuery fadeOut – Fading Out Element in HTML

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy