Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript window.scroll vs. window.scrollTo?

Tags:

javascript

Any idea what, if any, difference there is between window.scroll(x, y) and window.scrollTo(x, y) [not talking about jQuery]?

Also any ideas as to which browsers support which? Thanks

like image 406
Tom Avatar asked Dec 18 '09 01:12

Tom


People also ask

What is window scrollTo?

The Window scrollTo() method is used to scroll to a particular set of coordinates in the document. Syntax: window.scrollTo(x-coord, y-coord)

How do I make Windows scroll smooth?

Now you can use just window. scrollTo({ top: 0, behavior: 'smooth' }) to get the page scrolled with a smooth effect.

How do you scroll down in JavaScript?

window. scrollTo(0, document. body. scrollHeight);

How do you scroll up in JavaScript?

Method 1: Using window.scrollTo() 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.


2 Answers

There are no differences: https://developer.mozilla.org/en/DOM/window.scroll

As far as I know, all major browsers support both.

like image 114
Thomas Bonini Avatar answered Sep 19 '22 06:09

Thomas Bonini


Window.scrollTo() is effectively the same as the window.scroll(x,y) method. For scrolling a particular distance, use Window.scrollBy().

Also see Window.scrollByLines(), Window.scrollByPages() and Element.scrollIntoView()

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

like image 33
jaguarj Avatar answered Sep 20 '22 06:09

jaguarj