Revision 60968
Added by Konstantinos Triantafyllou over 2 years ago
smooth-scroll.ts | ||
---|---|---|
11 | 11 |
private lastRoute; |
12 | 12 |
|
13 | 13 |
constructor(private router: Router) { |
14 |
this.sub = router.events.subscribe(event => { |
|
15 |
if (event instanceof NavigationEnd) { |
|
16 |
if (this.interval) { |
|
17 |
clearInterval(this.interval); |
|
18 |
} |
|
19 |
const fragment = router.parseUrl(router.url).fragment; |
|
20 |
if (this.lastRoute !== this.getUrl(event.url)) { |
|
21 |
window.scrollTo({top: 0}); |
|
22 |
} |
|
23 |
if (fragment) { |
|
24 |
let i = 0; |
|
25 |
this.interval = setInterval(() => { |
|
26 |
i++; |
|
27 |
const element = document.getElementById(fragment); |
|
28 |
if (element) { |
|
29 |
if (this.interval) { |
|
14 |
if(typeof window !== "undefined") { |
|
15 |
this.sub = router.events.subscribe(event => { |
|
16 |
if (event instanceof NavigationEnd) { |
|
17 |
if (this.interval) { |
|
18 |
clearInterval(this.interval); |
|
19 |
} |
|
20 |
const fragment = router.parseUrl(router.url).fragment; |
|
21 |
if (this.lastRoute !== this.getUrl(event.url)) { |
|
22 |
window.scrollTo({top: 0}); |
|
23 |
} |
|
24 |
if (fragment) { |
|
25 |
let i = 0; |
|
26 |
this.interval = setInterval(() => { |
|
27 |
i++; |
|
28 |
const element = document.getElementById(fragment); |
|
29 |
if (element) { |
|
30 |
if (this.interval) { |
|
31 |
clearInterval(this.interval); |
|
32 |
} |
|
33 |
const yOffset = -100; |
|
34 |
let position = 0; |
|
35 |
let interval = setInterval(() => { |
|
36 |
if (position !== element.getBoundingClientRect().top) { |
|
37 |
position = element.getBoundingClientRect().top; |
|
38 |
} else { |
|
39 |
clearInterval(interval); |
|
40 |
const y = element.getBoundingClientRect().top + window.pageYOffset + yOffset; |
|
41 |
window.scrollTo({top: y, behavior: 'smooth'}); |
|
42 |
} |
|
43 |
}, 50); |
|
44 |
} |
|
45 |
if (i > 4 && this.interval) { |
|
30 | 46 |
clearInterval(this.interval); |
31 | 47 |
} |
32 |
const yOffset = -100; |
|
33 |
let position = 0; |
|
34 |
let interval = setInterval(() => { |
|
35 |
if (position !== element.getBoundingClientRect().top) { |
|
36 |
position = element.getBoundingClientRect().top; |
|
37 |
} else { |
|
38 |
clearInterval(interval); |
|
39 |
const y = element.getBoundingClientRect().top + window.pageYOffset + yOffset; |
|
40 |
window.scrollTo({top: y, behavior: 'smooth'}); |
|
41 |
} |
|
42 |
}, 50); |
|
43 |
} |
|
44 |
if (i > 4 && this.interval) { |
|
45 |
clearInterval(this.interval); |
|
46 |
} |
|
47 |
}, 100); |
|
48 |
} else { |
|
49 |
window.scrollTo({top: 0, behavior: 'smooth'}); |
|
48 |
}, 100); |
|
49 |
} else { |
|
50 |
window.scrollTo({top: 0, behavior: 'smooth'}); |
|
51 |
} |
|
52 |
this.lastRoute = this.getUrl(event.url); |
|
50 | 53 |
} |
51 |
this.lastRoute = this.getUrl(event.url); |
|
52 |
} |
|
53 |
}); |
|
54 |
}); |
|
55 |
} |
|
54 | 56 |
} |
55 | 57 |
|
56 | 58 |
private getUrl(url: string) { |
Also available in: Unified diff
[Library | Trunk]: Fix server errors. Input add validators for chips. Subscriber-invite: Change To field to chips, add short view