Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll to anchor not working

Tags:

Trying to scroll to an anchor link using the following syntax.

<a [routerLink]="['./']" fragment="test">Testing</a> 

And the anchor node looks like this

<div id="test"> 

When clicked the browser address bar shows the #test fragment but the automatic scrolling does not occur. Any idea why it does not scroll?

like image 924
doorman Avatar asked Oct 09 '16 08:10

doorman


People also ask

Why is my page not working with scroll anchoring?

If your page is not behaving well with scroll anchoring enabled, it is probably because some scroll event listener is not handling well the extra scrolling to compensate for the anchor node movement. You can check whether disabling scroll anchoring fixes the issue in Firefox by changing layout.css.scroll-anchoring.enabled to false in about:config.

How do I fix the scroll anchor issue in Firefox?

You can check whether disabling scroll anchoring fixes the issue in Firefox by changing layout.css.scroll-anchoring.enabled to false in about:config. If it does, you can check what node is Firefox using as the anchor using the layout.css.scroll-anchoring.highlight switch.

What is scroll anchoring in Revit?

Scroll anchoring adjusts the scroll position to compensate for the changes outside of the viewport. This means that the point in the document the user is looking at remains in the viewport, which may mean their scroll position actually changes in terms of how far they have moved through the document.

Why does Firefox scroll to the wrong position when I click?

If user clicks inside of the address bar and presses Enter, Firefox then scrolls to the correct position. Problem only occurs when link is not on the same page as the anchored element. If both are on the same page, Firefox scrolls to the correct position. Bug is not present in other browsers.


1 Answers

Based on @vsavkin workaround and taking advantage that fragments are provided as an observable (using "@angular/core": "^2.3.1"):

class MyAppComponent implements OnInit{    constructor(private route: ActivatedRoute) { }    ngOnInit() {     this.route.fragment.subscribe(f => {       const element = document.querySelector("#" + f)       if (element) element.scrollIntoView()     })   } } 
like image 55
pearpages Avatar answered Sep 28 '22 00:09

pearpages