Reputation: 763
Anyone know which is the differences between scrollIntoView()
vs scrollIntoViewIfNeeded()
?
I didn't see the differences between them. They both scroll if the element is not in visible area & do nothing if already visible.
Upvotes: 12
Views: 11249
Reputation: 866
If using the scrollIntoView() 'block' parameters 'start' (default), 'center' or 'end' scrollIntoView() always scrolls the element to the specified position even if it's already all on screen.
If using the parameter block: 'nearest' it behaves more like scrollIntoViewIfNeeded(), i.e. nothing happens if it is already on screen
Upvotes: 20
Reputation: 4857
Your description is correct.
The Element.scrollIntoViewIfNeeded() method scrolls the current element into the visible area of the browser window if it's not already within the visible area of the browser window. If the element is already within the visible area of the browser window, then no scrolling takes place. This method is a proprietary variation of the standard Element.scrollIntoView() method.
Note: it is also considered non-standard.
https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoViewIfNeeded
Upvotes: 7