Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: scrollTo() vs scroll(), which is the accepted/standard method

I know that both scroll() and scrollTo() do the same thing and that browsers support both to ensure backwards compatibility, but which one is deprecated? Which one should I be using? And is there a way to prevent a browser from storing the scroll position on a page so that I don't have to use a "beforeunload" event listener to reset my scroll position to (0,0) right before the user leaves the page? I would always like my page to be rendered every time with the scroll position at (0,0), because it's more of an interactive game than a website, and requires this functionality.

W3Schools seems to only reference scrollTo(), so this may be the standard. Still, a workaround for the "beforeunload" event listener would be nice so that my page always starts at scroll position (0,0) without the extra listener.

like image 841
Frank Avatar asked Oct 28 '25 20:10

Frank


1 Answers

The W3 actually lists both methods in their spec of the Window interface: http://www.w3.org/TR/cssom-view/#extensions-to-the-window-interface

You can safely use either one.

like image 94
skyline3000 Avatar answered Oct 31 '25 10:10

skyline3000



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!