94

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

Zooly
  • 4,736
  • 4
  • 34
  • 54
Tom
  • 6,947
  • 7
  • 46
  • 76

4 Answers4

88

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

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

Andreas Bonini
  • 44,018
  • 30
  • 122
  • 156
  • These are now in a draft specification: http://dev.w3.org/csswg/cssom-view/#widl-Window-scroll-void-long-x-long-y, they are all supported in the spec to maintain backwards compatibility – DaveRandom Nov 20 '12 at 14:41
  • 18
    If they are the same, then why are there two different functions? – SMBiggs Apr 08 '15 at 22:35
  • 12
    The authoritative answer is [here](https://drafts.csswg.org/cssom-view/#dom-window-scrollto): *When the scrollTo() method is invoked, the user agent must act as if the scroll() method was invoked with the same arguments.* – Jan Turoň Dec 12 '15 at 15:07
  • 1
    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 .scrollTo – Andrew Oct 06 '17 at 13:55
  • 1
    Edge doesn't seem to like either scroll() or scrollTo(), at least on divs. You can still set the value of scrollTop though. – Josh Powlison Jun 19 '18 at 12:17
  • 1
    Actually, `scroll()` 'scrolls the window to a particular place in the document' and `scrollTo()` 'scrolls to a particular set of coordinates in the document' ;) – Elijah Mock Dec 11 '20 at 02:48
  • 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". – Chris Robinson Feb 17 '23 at 14:04
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

Syscall
  • 19,327
  • 10
  • 37
  • 52
jaguarj
  • 131
  • 1
  • 9
5

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.
jameshfisher
  • 34,029
  • 31
  • 121
  • 167
1

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

  • 28
    `scrollBy` is unlike the others in that it scrolls relative to the current position. – Doug Neiner Dec 18 '09 at 01:18
  • 1
    Ok, thanks - I did not know that and the MDC does not state it –  Dec 18 '09 at 01:22
  • These are now in a draft specification: http://dev.w3.org/csswg/cssom-view/#widl-Window-scroll-void-long-x-long-y, they are all supported in the spec to maintain backwards compatibility – DaveRandom Nov 20 '12 at 14:40
  • Thanks, I was struggling with this and scrollBy was just what I needed! – MinionAttack Jun 02 '23 at 07:58