Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 728
Heyyy Marco Avatar asked Mar 05 '26 05:03

Heyyy Marco


1 Answers

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

like image 149
00-BBB Avatar answered Mar 07 '26 17:03

00-BBB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!