scrollIntoView()
The scrollIntoView()
method scrolls the element's parent container such that the element on which scrollIntoView()
is called is visible to the user.
Implementation of scrollIntoView()
The CSSOM View Module specification contains the sequence of steps performed by scrollIntoView()
and are as follows:
- Let behavior be "auto".
- Let block be "start".
- Let inline be "nearest".
- If arg is a ScrollIntoViewOptions dictionary, then:
- Set behavior to the behavior dictionary member of options.
- Set block to the block dictionary member of options.
- Set inline to the inline dictionary member of options.
- Otherwise, if arg is false, then set block to "end".
- If the element does not have any associated layout box, then return.
- Scroll the element into view with behavior, block, and inline.
- Optionally perform some other action that brings the element to the user’s attention.
Steps for Scroll the element into view
To scroll an element into view element, with a scroll behavior behavior, a block flow direction position block, and an inline base direction position inline, means to run these steps for each ancestor element or viewport that establishes a scrolling box scrolling box, in order of innermost to outermost scrolling box:
- If the Document associated with element is not same origin with the Document associated with the element or viewport associated with box, terminate these steps.
- Let element bounding border box be the box that the return value of invoking getBoundingClientRect() on element represents.
- Let scrolling box edge A be the beginning edge in the block flow direction of scrolling box, and let element edge A be element bounding border box’s edge on the same physical side as that of scrolling box edge A.
- Let scrolling box edge B be the ending edge in the block flow direction of scrolling box, and let element edge B be element bounding border box’s edge on the same physical side as that of scrolling box edge B.
- Let scrolling box edge C be the beginning edge in the inline base direction of scrolling box, and let element edge C be element bounding border box’s edge on the same physical side as that of scrolling box edge C.
- Let scrolling box edge D be the ending edge in the inline base direction of scrolling box, and let element edge D be element bounding border box’s edge on the same physical side as that of scrolling box edge D.
- Let element height be the distance between element edge A and element edge B.
- Let scrolling box height be the distance between scrolling box edge A and scrolling box edge B.
- Let element width be the distance between element edge C and element edge D.
- Let scrolling box width be the distance between scrolling box edge C and scrolling box edge D.
Let position be the scroll position scrolling box would have by following these steps:
- If block is "start", then align element edge A with scrolling box edge A.
- Otherwise, if block is "end", then align element edge B with scrolling box edge B.
- Otherwise, if block is "center", then align the center of element bounding border box with the center of scrolling box in scrolling box’s block flow direction.
Otherwise, block is "nearest":
If element edge A and element edge B are both outside scrolling box edge A and scrolling box edge B
Do nothing.
If element edge A is outside scrolling box edge A and element height is less than scrolling box height
If element edge B is outside scrolling box edge B and element height is greater than scrolling box height
Align element edge A with scrolling box edge A.
If element edge A is outside scrolling box edge A and element height is greater than scrolling box height
If element edge B is outside scrolling box edge B and element height is less than scrolling box height
Align element edge B with scrolling box edge B.
If inline is "start", then align element edge C with scrolling box edge C.
- Otherwise, if inline is "end", then align element edge D with scrolling box edge D.
- Otherwise, if inline is "center", then align the center of element bounding border box with the center of scrolling box in scrolling box’s inline base direction.
Otherwise, inline is "nearest":
If element edge C and element edge D are both outside scrolling box edge C and scrolling box edge D
Do nothing.
If element edge C is outside scrolling box edge C and element width is less than scrolling box width
If element edge D is outside scrolling box edge D and element width is greater than scrolling box width
Align element edge C with scrolling box edge C.
If element edge C is outside scrolling box edge C and element width is greater than scrolling box width
If element edge D is outside scrolling box edge D and element width is less than scrolling box width
Align element edge D with scrolling box edge D.
If position is the same as scrolling box’s current scroll position, and scrolling box does not have an ongoing smooth scroll, then return.
If scrolling box is associated with an element
Let associated element be the element.
If scrolling box is associated with a viewport
Let document be the viewport’s associated Document. Let associated element be document’s root element, if there is one, or null otherwise.
Perform a scroll of scrolling box to position, associated element as the associated element and behavior as the scroll behavior.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…