AnchorScrolling is applied when fragment does not match any anchor
See original GitHub issueWhich @angular/* package(s) are the source of the bug?
router, Don’t known / other
Is this a regression?
No
Description
When anchorScrolling
and scrollingPositionRestoration
are enabled in router options, accessing a route with fragment that does not contain any element with a matching id or name seems to cause the page to still scroll, even if there’s no apparent target for the scroll.
Despite this effect, from what I’ve tested, scrollToAnchor
does not seem to be the issue since it does not find any element to scroll to.
Steps to reproduce:
- Click on ‘Crisis Center’. Route loaded is /crisis-list
- Scroll down to bottom of page. Click link
- Route loaded is /heroes-list#123. Page is scrolled even though no elements have id or name 123.
Please provide a link to a minimal reproduction of the bug
https://stackblitz.com/edit/angular-ervdaf?file=src/app/heroes-list/heroes-list.component.html
Please provide the exception or error you saw
Please provide the environment you discovered this bug in
Angular CLI: 10.1.7
Node: 12.14.1
OS: darwin x64
Angular: 12.1.1
... animations, common, compiler, core, forms
... platform-browser, platform-browser-dynamic, router
Package Version
------------------------------------------------------
@angular-devkit/architect 0.1001.7 (cli-only)
@angular-devkit/core 10.1.7 (cli-only)
@angular-devkit/schematics 10.1.7 (cli-only)
@schematics/angular 10.1.7 (cli-only)
@schematics/update 0.1001.7 (cli-only)
Anything else?
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (7 by maintainers)
Top Results From Across the Web
Scroll to anchor not working - angular - Stack Overflow
I have recreated an reusalbe service that can be used in any component in order to either scroll to fragment if present else...
Read more >InMemoryScrollingOptions - Angular
When set to 'enabled', scrolls to the anchor element when the URL has a fragment. Anchor scrolling is disabled by default. Anchor scrolling...
Read more >error on anchor scrolling with fragments-angular.js
Got to the answer. It turned out to be a problem on a script on the template I was using. Once I removed...
Read more >Creating A Jump-To-Anchor Fragment Polyfill In Angular 5.2.0
And, it can be seamlessly removed in the future when it is no longer ... An anchor tag with a name-property that matches...
Read more >Guide to scroll anchoring - CSS: Cascading Style Sheets | MDN
If one node doesn't seem appropriate to be an anchor, you can ... which can be used to disable scroll anchoring on all...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@miacrd - If you don’t use the shadow dom, you can just use a one liner to determine if the anchor exists https://github.com/angular/angular/blob/8d0511d009aa01fbebdb61ca4a97dd48d5d1929d/packages/common/src/viewport_scroller.ts#L216 You can just copy any/all of the code in there
https://github.com/angular/angular/pull/43347#issuecomment-914549142.
After more thought, I don’t think this is something we should change in the scrolling implementation. Due to how the child routes and router outlets work, it really may be desirable for an application to not scroll to top if the fragment doesn’t exist. In particular, we certainly shouldn’t scroll if the fragment is the only thing that updated. In addition, because there might be outlets and child outlets, applications very well might only be changing a small section of the page so we wouldn’t even want to always scroll to the top on a path change.
I think this should be something that’s addressed in application code instead by subscribing to Router events, filtering on
NavigationEnd
orScroll
, and determining how exactly the scrolling should be handled at that point.I also wouldn’t think that writing in more complex handling for missing fragments would be something we’d want to bake into the common scroller, as it would increase bundle size for everyone that uses it but wouldn’t be something most applications are concerned about.