Fixed a small issue with the smooth-scroll

This commit is contained in:
Stefania Martziou 2021-07-28 12:42:47 +00:00
parent 8e92ba96e8
commit 7837a0958d
1 changed files with 9 additions and 13 deletions

View File

@ -9,6 +9,7 @@ export class SmoothScroll {
private interval;
private readonly sub;
private lastRoute;
private whitelist = ['/continent', '/country']
constructor(private router: Router) {
if(typeof window !== "undefined") {
@ -19,13 +20,7 @@ export class SmoothScroll {
}
const fragment = router.parseUrl(router.url).fragment;
if (this.lastRoute !== this.getUrl(event.url)) {
// console.log('event.url', event.url);
// window.scrollTo({top: 0});
if (!this.lastRoute || (!this.lastRoute.includes('/continent') && !this.lastRoute.includes('/country'))
|| (!event.url.includes('/continent') && !event.url.includes('/country'))) {
console.log('event.url', event.url);
window.scrollTo({top: 0});
}
window.scrollTo({top: 0});
}
if (fragment) {
let i = 0;
@ -52,10 +47,8 @@ export class SmoothScroll {
clearInterval(this.interval);
}
}, 100);
} else {
if (!event.url.includes('/continent') && !event.url.includes('/country')) {
window.scrollTo({top: 0, behavior: 'smooth'});
}
} else if(!this.whitelist.includes(this.getUrl(event.url))) {
window.scrollTo({top: 0, behavior: 'smooth'});
}
this.lastRoute = this.getUrl(event.url);
}
@ -63,8 +56,10 @@ export class SmoothScroll {
}
}
private getUrl(url: string) {
return url.split('?')[0].split('#')[0];
private getUrl(url: string): string {
let full = url.split('?')[0].split('#')[0];
let route = this.whitelist.find(_ => full.includes(_));
return (route)?(route):full;
}
public clearSubscriptions() {
@ -76,3 +71,4 @@ export class SmoothScroll {
}
}
}