JavaScript window.scroll() Method

You are Here:

JavaScript window.scroll() Method

The window.scroll() method scrolls the window to a particular place in the document.

Note: The window.scrollTo() is effectively the same as this method.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Scroll</button> <script> function myFunction(){ window.scroll(1500, 1500); } </script> </body> </html>

Syntax

window.scroll(x-coord, y-coord)

Parameter Values

ValueTypeExplanation
x-coordRequiredThe pixel along the horizontal axis of the document that you want displayed in the upper left.
y-coordRequiredThe pixel along the vertical axis of the document that you want displayed in the upper left.

scroll() with behavior

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Scroll</button> <script> function myFunction(){ window.scroll({ top: 1500, left: 1500, behavior: 'smooth' }); } </script> </body> </html>

scroll() for Specific Element

Note: You can also use scroll() for specific element.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <div id="myDiv"> <p>Hello world.</p> </div> <button onclick="myFunction()">Scroll</button> <script> var x = document.getElementById("myDiv"); function myFunction(){ x.scroll({ top: 200, left: 200, behavior: 'smooth' }); } </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author