The Element
interface offers a scrollIntoView
method that sets the scroll positions of all ancestor elements so that the given element is visible on the screen.
How can I perform an analogous operation with a DOM Range
, which is to say, scroll whatever it is that needs to be scrolled so that the range is visible on the screen?
I can probably live with manually computing the bounding box of the range and finding which element to scroll, but I do not wish to modify the contents of the DOM in any way.
Range
has a useful method calledgetBoundingClientRect
which returns aDOMRect
describing the position of the range relative to the viewport. You can then scroll the element into view usingwindow.scrollBy
. A very simple example:It becomes complicated if the range is present inside nested scrollable containers. A tricky solution is to scroll the "nearest scroll parent" into view and align it with the top of the viewport, then use the above logic to scroll the range into view (the bounding rect will give you the position relative to the viewport but at this point all scroll parents are aligned to the top of the viewport so it should just work).
For simple use cases, a simple solution is to identify the element containing the range using
Range.commonAncestorContainer
and useElement.scrollIntoView
: