MoreLesson 284Free preview
JS Scroll Into View
This lesson is part of How To · All levels
Summary
ScrollIntoView is a method on a DOM element that causes a browser to scroll through a page until the element is visible. You can set the roll behavior to smooth and determine the position of the elements on the screen, for example, at the top or center. This method is often used for anchor navigation menus, upward return buttons, and section markers being read.
Key points
- Called directly to the element, for example target.scrollIntoView()
- Behavioral options: "smooth" gives animation, "auto" moves directly
- The block option sets the vertical position: start, center, end, or nearest
- The inline option sets the horizontal position on the rolling container
- Combine with CSS scroll-margin-top to avoid closed fixed header
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.