JavaScript 的 window.scrollTo 和 window.scrollTo?

你知道 window.scroll(x, y)window.scrollTo(x, y)之间有什么区别吗?

还有关于哪些浏览器支持哪些浏览器的想法吗? 谢谢

72192 次浏览

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

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

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

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

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.