DEV Community

Discussion on: Scrolling to a specific element on a page

Collapse
 
moopet profile image
Ben Sinclair

One problem with scrollIntoView is that it scrolls the window until the top of the element is inside the viewport, but many sites (like DEV, for example) have a fixed header.
If you try it on an element on this page, then the element will remain partially hidden under the header. You'll end up needing to follow the scrollIntoView call with one to window.scrollBy passing in the height of the header as currently rendered (so reading it from the element rather than CSS)

Collapse
 
1e4_ profile image
Ian

I don't really call that a problem as you're wanting to do something this function doesn't provide.

But yes it's definitely a gotcha at first if your using fixed/sticky elements 👍