Element: scrollIntoView() method
The Element
interface's
scrollIntoView()
method scrolls the element's ancestor
containers such that the element on which scrollIntoView()
is called is
visible to the user.
Syntax
js
scrollIntoView()
scrollIntoView(alignToTop)
scrollIntoView(scrollIntoViewOptions)
Parameters
alignToTop
Optional-
A boolean value:
-
If
true
, the top of the element will be aligned to the top of the visible area of the scrollable ancestor. Corresponds toscrollIntoViewOptions: {block: "start", inline: "nearest"}
. This is the default value. -
If
false
, the bottom of the element will be aligned to the bottom of the visible area of the scrollable ancestor. Corresponds toscrollIntoViewOptions: {block: "end", inline: "nearest"}
.
-
If
-
scrollIntoViewOptions
Optional Experimental -
An Object with the following properties:
behavior
Optional-
Determines whether scrolling is instant or animates smoothly. This option is a string which must take one of the following values:
smooth
: scrolling should animate smoothlyinstant
: scrolling should happen instantly in a single jumpauto
: scroll behavior is determined by the computed value ofscroll-behavior
block
Optional-
Defines vertical alignment. One of
start
,center
,end
, ornearest
. Defaults tostart
. inline
Optional-
Defines horizontal alignment. One of
start
,center
,end
, ornearest
. Defaults tonearest
.
Return value
None (undefined
).
Examples
js
const element = document.getElementById("box");
element.scrollIntoView();
element.scrollIntoView(false);
element.scrollIntoView({ block: "end" });
element.scrollIntoView({ behavior: "smooth", block: "end", inline: "nearest" });
Notes
The element may not be scrolled completely to the top or bottom depending on the layout of other elements.
Specifications
Specification |
---|
CSSOM View Module # dom-element-scrollintoview |
Browser compatibility
BCD tables only load in the browser
See also
Element.scrollIntoViewIfNeeded()
Non-standard