Skip to content
Related Articles

Related Articles

How to animate scrollTop using jQuery ?
  • Last Updated : 15 Nov, 2019

The scrollTop property in JavaScript is used to set or return the vertical scrollbar position of any element. Setting the scrollTop to any value will make the page scroll to that location. The scroll by default takes place immediately and abruptly scrolls to the value. This scrolling can be animated using jQuery.

The animate() method is used to perform a custom animation on a set of CSS properties. It works by changing the value of the property in gradual steps to create an animated effect. Only the properties having numeric values can be animated. The animation can be modified with 2 additional parameters that can help to change the speed of the animation.

This method is used with the scrollTop property to animate the scrolling on the page. The jQuery selector is used to select both the “html” and “body” tags of the page. This is done to ensure compatibility with some browsers where selecting only the body element does not work.

The animate() method is used on this selected element with the scrollTop property in the styles argument. The speed and easing of the animation can be changes as required.

Syntax:



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

Example 1: This example animating the scroll with the default speed.




<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to animate scrollTop with jQuery?
    </title>
      
    <script src=
    </script>
      
    <style>
        .scrollable {
            background-color: green;
            height: 1000px;
        }
    </style>
</head>
  
<body>
    <h1 style="color: green">
        GeeksForGeeks
    </h1>
      
    <b>
        How to animate scrollTop with jQuery?
    </b>
      
    <p>
        Click on the button to scroll
        to the top of the page.
    </p>
      
    <p class="scrollable">
        This is a large div to
        help in scrolling.
    </p>
      
    <button onclick="scrollTopAnimated()">
        Scroll To Top
    </button>
      
    <script type="text/javascript">
        function scrollTopAnimated() {
            $("html, body").animate({ scrollTop: "0" });
        }
    </script>
</body>
  
</html>

Output:
scroll-default

Example 2: This example animating the scroll with a speed of 3000.




<!DOCTYPE html>
<html>
      
<head>
    <title>
        How to animate scrollTop with jQuery?
    </title>
      
    <script src=
    </script>
      
    <style>
        .scrollable {
            background-color: green;
            height: 1000px;
        }
    </style>
</head>
  
<body>
    <h1 style="color: green">
        GeeksForGeeks
    </h1>
      
    <b>
        How to animate scrollTop with jQuery?
    </b>
      
    <p>
        Click on the button to scroll to the
        top of the page.
    </p>
      
    <p class="scrollable">
        This is a large div to help in scrolling.
    </p>
      
    <button onclick="scrollTopAnimated()">
        Scroll To Top
    </button>
      
    <script type="text/javascript">
        function scrollTopAnimated() {
            $("html, body").animate(
                { scrollTop: "0" }, 3000);
        }
    </script>
</body>
  
</html>

Output:
scroll-slow

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :