📜  window.scroll - Javascript (1)

📅  最后修改于: 2023-12-03 15:05:56.891000             🧑  作者: Mango

Window.scroll - Javascript

When developing web applications, it is often necessary to scroll the page to a certain position. This is where the window.scroll method in Javascript comes in handy.

Syntax
window.scroll(x-coord, y-coord)
  • x-coord: Required. The horizontal pixel value of where to scroll to.
  • y-coord: Required. The vertical pixel value of where to scroll to.
Example
window.scroll(0, 500);

This will scroll the page to a position where the y coordinate is 500.

Additional Notes
  • The window.scroll method is equivalent to window.scrollTo.
  • These methods can also be called with an object containing x and y properties, like so:
window.scroll({ top: 500, left: 0, behavior: 'smooth' });
  • The behavior property can be set to 'auto' or 'smooth' to specify the scrolling behavior. 'smooth' will create a smooth scrolling animation, whereas 'auto' will not. The default value is 'auto'.