Reputation: 14076
Using javascript we can bring an element into view using -
document.getElementById('pluginsource_wrapper').scrollIntoView();
I can understand the concept of scrollIntoView()
method but just wanted to know its code and understand how it is implemented.
Could you please point me to the code where I can find implementation of scrollIntoView()
?
Upvotes: 1
Views: 3412
Reputation: 193308
The scrollIntoView()
method scrolls the element's parent container such that the element on which scrollIntoView()
is called is visible to the user.
The CSSOM View Module specification contains the sequence of steps performed by scrollIntoView()
and are as follows:
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:
Let position be the scroll position scrolling box would have by following these steps:
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, 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.
Upvotes: 4
Reputation: 50919
document.getElementById() returns DOM Element object.
Definition and Usage
The scrollIntoView() method scrolls the specified element into the visible area of the browser window.
The only code I could find is a commented line in Element.webidl in partial interface Element
, but the behavior is part of the specifications of Extensions to the Element Interface
scrollIntoView() in drafts.csswg.org
The scrollIntoView(arg) method must run these steps:
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.
Upvotes: 0
Reputation: 11
Try this below code:
WebElement Element = driver.findElement(By.id("paid"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView();", Element);
Upvotes: 0
Reputation: 2250
You can check out the code of the npm package srcoll-into-view
. It's not the same as the browser implementation, but close enough to get an idea of what it's doing.
Upvotes: 1