JavaScript window.scroll vs. window.scrollTo?
Asked Answered
A

4

96

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

Alienee answered 18/12, 2009 at 1:6 Comment(0)
C
89

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

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

Cenozoic answered 18/12, 2009 at 1:10 Comment(7)
These are now in a draft specification: dev.w3.org/csswg/cssom-view/…, they are all supported in the spec to maintain backwards compatibilityIrritating
If they are the same, then why are there two different functions?Lecturer
The authoritative answer is here: When the scrollTo() method is invoked, the user agent must act as if the scroll() method was invoked with the same arguments.Wroth
well, .scroll only went half way up the page, and .scrollTo worked, maybe it is chrome or iframe issue, anyways, bad, maybe use both .scroll and .scrollToAurify
Edge doesn't seem to like either scroll() or scrollTo(), at least on divs. You can still set the value of scrollTop though.Sulfapyrazine
Actually, scroll() 'scrolls the window to a particular place in the document' and scrollTo() 'scrolls to a particular set of coordinates in the document' ;)Peddler
The Mozilla documentation seems to be wrong regarding the Options method of scroll() and scrollTo(). It says: "Top: Specifies the number of pixels along the Y axis to scroll the window or element.". And similarly for Left. This looks like a copy/paste from ScrollBy() error. I think it should be: "Top: Specifies the pixel along the horizontal axis that you want displayed in the upper left".Modesta
P
13

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

Phyle answered 30/4, 2018 at 20:10 Comment(0)
C
7

In the absence of deprecation, I prefer window.scroll instead of window.scrollTo:

  • The specification only describes window.scrollTo by referring to window.scroll, suggesting that window.scroll is authoritative.
  • window.scroll has ~3M search results; window.scrollTo has ~0.5M.
Counterweigh answered 13/1, 2022 at 12:3 Comment(0)
P
1

There is scrollTo, scroll, and scrollBy! Apparently there is no standard covering this functionality so all Browsers may not implement it the same.

Pollywog answered 18/12, 2009 at 1:17 Comment(4)
scrollBy is unlike the others in that it scrolls relative to the current position.Fetterlock
Ok, thanks - I did not know that and the MDC does not state itPollywog
These are now in a draft specification: dev.w3.org/csswg/cssom-view/…, they are all supported in the spec to maintain backwards compatibilityIrritating
Thanks, I was struggling with this and scrollBy was just what I needed!Occupier

© 2022 - 2024 — McMap. All rights reserved.