How to scroll window using jQuery ?
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:
- 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> |
chevron_right
filter_none
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> |
chevron_right
filter_none
Output:
- Before clicking on the button:

- After clicking on the button:

Recommended Posts:
- jQuery | scroll() with Examples
- Scroll to the top of the page using JavaScript/jQuery
- How to determine the direction of a jQuery scroll event?
- How to scroll automatically to the Bottom of the Page using jQuery?
- How to set textarea scroll bar to bottom as a default using JavaScript/jQuery ?
- How to disable scroll to change number in <input type="number"> field using JavaScript/jQuery?
- Cross-browser window resize event using JavaScript/jQuery
- How to get the size of screen, current web page and browser window using jQuery?
- Hide scroll bar, but while still being able to scroll using CSS
- Javascript | Window Open() & Window Close Method
- Javascript | Window Blur() and Window Focus() Method
- Web Window API | Window personalbar property
- Window | Window.requestAnimationFrame() Method
- Web Window API | Window statusbar property
- Web Window API | Window menubar property
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.

