Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to scroll window using jQuery ?

  • Last Updated : 12 Sep, 2019

Given a HTML document and the task is to scroll the document with the help of jQuery. There are two methods to scroll the HTML document window which are discussed below:

Approach:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • Select the document by using selector.
  • Use either of the scrollTop() or scrollTo() method to move the document to that position.

Example 1: This example using the scrollTop() method to scroll the HTML document.




<!DOCTYPE HTML>  
<html>  
  
<head
    <title
        How to scroll window using jQuery ?
    </title>
      
    <script src=
    </script>
      
    <style>
        #body {
            height: 1000px;
        }
    </style>
</head
  
<body style = "text-align:center;" id = "body">  
      
    <h1 id = "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
        "font-size: 23px; font-weight: bold; color: green; ">
    </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"
                        + " the document.";
          
        function gfg_Run() {
            $(document).scrollTop(100);
            el_down.innerHTML = "Position has been set.";
        }         
    </script
</body>  
  
</html>

Output:

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

Example 2: This example using the scrollTo() method to scroll the HTML document.




<!DOCTYPE HTML>  
<html>  
  
<head
    <title
        How to scroll window using jQuery ?
    </title>
      
    <script src=
    </script>
      
    <style>
        #body {
            height: 1000px;
        }
    </style>
</head
  
<body style = "text-align:center;" id = "body">  
      
    <h1 id = "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
        "font-size: 23px; font-weight: bold; color: green; ">
    </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"
                        + " the document.";
          
        function gfg_Run() {
            window.scrollTo(0, 100);
            el_down.innerHTML = "Position has been set.";
        }         
    </script
</body>  
  
</html>

Output:

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!