86

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,580
  • 4
  • 32
  • 51
Tom
  • 6,757
  • 7
  • 45
  • 74

4 Answers4

83

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

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

Thomas Bonini
  • 42,536
  • 29
  • 119
  • 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
  • 11
    If they are the same, then why are there two different functions? – SMBiggs Apr 08 '15 at 22:35
  • 7
    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
10

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
  • 18,131
  • 10
  • 32
  • 49
jaguarj
  • 103
  • 1
  • 7
2

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
  • 29,645
  • 26
  • 105
  • 155
1

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

  • 24
    `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