Heyyy Marco
Heyyy Marco

Reputation: 763

Javascript: scrollIntoView() vs scrollIntoViewIfNeeded()

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

Answers (2)

00-BBB
00-BBB

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

olore
olore

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

Related Questions