Skip to content

Commit d79227e

Browse files
Merge pull request #13 from webdevnerdstuff/dev
making scroll to better
2 parents 9134c0d + 4aca1f7 commit d79227e

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

src/documentation/components/MenuComponent.vue

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,27 +74,43 @@ onMounted(() => {
7474
active.value = window.location.hash || '#home';
7575
});
7676
77+
function scrollToHash(hash: string): void {
78+
const id: string = hash.replace('#', '');
79+
const yOffset: number = -55;
80+
const element: HTMLElement | null = document.getElementById(id);
81+
82+
if (element) {
83+
const y: number = element.getBoundingClientRect().top + window.scrollY + yOffset;
84+
85+
active.value = hash;
86+
87+
window.location.hash = hash;
88+
window.scrollTo({ behavior: 'smooth', top: y });
89+
}
90+
}
91+
7792
function smoothScroll(): void {
7893
document.querySelectorAll<HTMLAnchorElement>('a[href^="#"]').forEach((anchor) => {
7994
anchor.addEventListener('click', (e: MouseEvent) => {
8095
e.preventDefault();
8196
8297
const hash: string = anchor.hash;
83-
const id: string = hash.replace('#', '');
84-
const yOffset: number = -55;
85-
const element: HTMLElement | null = document.getElementById(id);
86-
87-
if (element) {
88-
const y: number = element.getBoundingClientRect().top + window.pageYOffset + yOffset;
8998
90-
active.value = hash;
91-
92-
window.location.hash = hash;
93-
window.scrollTo({ behavior: 'smooth', top: y });
99+
if (hash) {
100+
scrollToHash(hash);
94101
}
95102
});
96103
});
104+
105+
window.addEventListener('load', () => {
106+
const hash = window.location.hash;
107+
if (hash) {
108+
scrollToHash(hash);
109+
}
110+
});
97111
}
112+
113+
98114
</script>
99115

100116
<style lang="scss">

0 commit comments

Comments
 (0)