Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vanilla JavaScript: Scroll to Top of Page

I may have missed something, but I cannot get window.scrollTo(0,0) to move to the top of the page.

I am implementing a sticky aside which works well enough. It uses .getBoundingClientRect() to get the initial position.

However, if the page is partly scrolled, and I refresh the page, it reads the position wrongly, and is positioned in the wrong place.

I though I would fix this by executing window.scrollTo(0,0) at the beginning, so that the page is at the top, and the aside is in the right position.

When I run the code, window.scrollTo(0,0) doesn’t seem to make any difference.

What is the correct way to get the reloaded window to start at the top?

I have tested it on Firefox on the Mac. Chrome and Safari gives a similar behaviour.

Please, no jQuery.

like image 626
Manngo Avatar asked Sep 15 '17 08:09

Manngo


People also ask

How do you get to the top of the page in JavaScript?

The scrollTo() method of the window Interface can be used to scroll to a specified location on the page. It accepts 2 parameters the x and y coordinate of the page to scroll to. Passing both the parameters as 0 will scroll the page to the topmost and leftmost point.

How do I jump to the top of the page in HTML?

Next, add the top of page link using the # symbol and "top" anchor name/id as the value of the href attribute, as shown below. If you scrolled down on this page, clicking this link takes you back to the top of the page. All modern browsers understand the "#top" value.

How do I scroll with scrollTop?

The scrollTop() method sets or returns the vertical scrollbar position for the selected elements. Tip: When the scrollbar is on the top, the position is 0. When used to return the position: This method returns the vertical position of the scrollbar for the FIRST matched element.

How do I get the scroll position of an element?

To get or set the scroll position of an element, you follow these steps: First, select the element using the selecting methods such as querySelector() . Second, access the scroll position of the element via the scrollLeft and scrollTop properties.


2 Answers

Have you tried waiting for page load before scrollTo? Try using window.onload

window.onload = function(){
    window.scrollTo(0,0);
}
like image 172
TKoL Avatar answered Oct 23 '22 09:10

TKoL


Going to top of the page with a scroll effect is a bit more easier in javascript now with:

https://developer.mozilla.org/en-US/docs/Web/API/Window/scroll

window.scroll({
 top: 0, 
 left: 0, 
 behavior: 'smooth' 
});

CAUTION

I just checked the mozilla doc right now while updating my answer and I believe it has been updated. Right now the method is window.scroll(x-coord, y-coord) and does not mention or show examples that use the object parameter where you can set the behavior to smooth. I just tried the code and it still works in chrome and firefox and the object parameter is still in the spec.

Or in Vanilla you can use window.onload event with scrollTo(x,y) function

window.onload = function(){
    window.scrollTo(0,0);
}
like image 7
TanvirChowdhury Avatar answered Oct 23 '22 07:10

TanvirChowdhury