How to scroll automatically to a particular element using JQuery?

The task is to scroll to a particular element automatically. Below are the approaches:

Approach 1:

  • Get the height of the element by .position().top property.
  • Use .scrollTop() method to set the vertical position of the scrollbar equal to the calculated height of the particular element.

Example 1: This example uses the approach discussed above.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        How to scroll automatically to a particular element.
    </title>
    <style>
        #GFG_DOWN {
            margin-top: 370px;
        }
    </style>
    <script src=
    </script>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
    <p id="GFG_UP" 
       style="font-size: 15px;
              font-weight: bold;">
    </p>
    <button onclick="gfg_Run()">
        Click Here
    </button>
    <p id="GFG_DOWN"
       style="color:green;
              font-size: 30px; 
              font-weight: bold;">
        This is element.
    </p>
    <script>
        var el_up = document.getElementById("GFG_UP");
        var el_down = document.getElementById("GFG_DOWN");
        el_up.innerHTML = 
"Click on the button to scroll to the particular element.";
  
        function gfg_Run() {
            $(window).scrollTop($('#GFG_DOWN').position().top);
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

Approach 2:

  • Get the height of the element by .offset().top property.
  • Use .animate() method to animate to the element.

Example 2: This example uses the approach discussed above.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        How to scroll automatically to a particular element.
    </title>
    <style>
        #GFG_DOWN {
            margin-top: 400px;
        }
    </style>
    <script src=
    </script>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
    <p id="GFG_UP" 
       style="font-size: 15px; font-weight: bold;">
    </p>
    <button onclick="gfg_Run()">
        Click Here
    </button>
    <p id="GFG_DOWN" 
       style="color:green; 
              font-size: 30px;
              font-weight: bold;">
        This is element.
    </p>
    <script>
        var el_up = document.getElementById("GFG_UP");
        var el_down = document.getElementById("GFG_DOWN");
        el_up.innerHTML = 
"Click on the button to scroll to the particular element.";
  
        function gfg_Run() {
            $('html, body').animate({
                scrollTop: $("#GFG_DOWN").offset().top
            }, 500);
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.