HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptCritical

Scroll to the top of the page using JavaScript?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
pagetheusingtopscrolljavascript

Problem

How do I scroll to the top of the page using JavaScript? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.

Solution

If you don't need the change to animate then you don't need to use any special plugins - I'd just use the native JavaScript window.scrollTo() method -- passing in 0, 0 will scroll the page to the top left instantly.

window.scrollTo(xCoord, yCoord);


Parameters

  • xCoord is the pixel along the horizontal axis.



  • yCoord is the pixel along the vertical axis.

Code Snippets

window.scrollTo(xCoord, yCoord);

Context

Stack Overflow Q#1144805, score: 2731

Revisions (0)

No revisions yet.