Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take scroll margin of element into account #2443

Open
JoostKersjes opened this issue Jan 23, 2025 · 3 comments · May be fixed by #2448
Open

Take scroll margin of element into account #2443

JoostKersjes opened this issue Jan 23, 2025 · 3 comments · May be fixed by #2448
Labels
docs Concerns the documentation content or the documentation website

Comments

@JoostKersjes
Copy link

What problem is this solving

When the scrollBehavior gets passed an el element, it scrolls precisely to the element.

If the page has e.g. a position: sticky banner, the element will partially be hidden behind this banner. The simple solution for a non-JS webpage in this case would be to add a scroll-margin: 60px; CSS rule to the element in question. Right now vue-router doesn't support this solution.

I couldn't find an existing issue for this suggestion, but I did find a mention of other people with the same problem including a workaround: gridsome/gridsome#1361 (comment)

Proposed solution

In my opinion, the scrollBehavior should automatically check if the target element has a scroll-margin and use it to offset the scroll position destination.

This allows a stylesheet to be responsible for defining the scroll margin, making it easy to have different offset values depending on the styling of the element and/or the layout of the page.

Adding this to the library would call for a bit of documentation to point searches for a solution in the right direction.

Describe alternatives you've considered

  • Keep it undocumented and unsupported. There might not be many apps that have to deal with this, and there is a pretty simple workaround out there.
  • Add some documentation that mentions this potentially unexpected behaviour, ideally including a code snippet as a suggestion to solve it.
@JoostKersjes
Copy link
Author

Would you be open to a PR for this? I might be able find time to create one.

Copy link
Member

posva commented Jan 23, 2025

I would be open to a pool request for the documentation on this: this is one of many edge cases in CSS about scrolling restoration and I believe it’s safer to document some of these To let users know that they have to compute any offsets by themselves

@posva posva added the docs Concerns the documentation content or the documentation website label Jan 23, 2025 — with Volta.net
@JoostKersjes
Copy link
Author

I've created one: #2448

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Concerns the documentation content or the documentation website
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants