Reputation: 145
I'm working on an Angular 2/4 project and I have to scroll up in case of every navigation change ... which is complicated due to I use sidenav (Angular Material) and a lot of other things. I just experienced a really annoying problem, which is the following:
this.router
.events
.filter(event => event instanceof NavigationEnd)
.subscribe(() => {
const contentContainer = document.querySelector('.mat-drawer-content');
contentContainer.scrollTo(0, 0);
});
It scrolls up in case of any navigation between child components, and even if I navigate somewhere else (to sibling routes). It works properly in Firefox, and newer versions of Chrome ( >=61 ), but unfortunately throws the following exception if I use e.g. Chrome 60 or 59 (not tested for any other versions yet).
Error: Uncaught (in promise): TypeError: e.scrollTo is not a function
I use prebuilt jQuery 2.1.1, and as I've searched for the issue it seems that there was a breaking change in Chrome v61. But my problem is just the opposite: it works higher versions and not on lower ones ...
Is it possible to make it work on every (relevant) Chrome version?
Upvotes: 1
Views: 728
Reputation: 32145
Actually scrollTo()
is not a jQuery function, it's defined in JavaScript and you can't call scrollTo()
on an element, it's a method defined only in window
object, that's why you got e.scrollTo is not a function
.
You can only call it on window
like this:
window.scrollTo(0, 0);
If you check the window.scrollTo() Notes on MDN you will see that:
For scrolling elements, see
Element.scrollTop
andElement.scrollLeft
.
Upvotes: 1