Skip to content

Commit

Permalink
feat: smooth scrolling
Browse files Browse the repository at this point in the history
Signed-off-by: ZTL-UwU <[email protected]>
  • Loading branch information
ZTL-UwU committed May 27, 2024
1 parent c4b13f4 commit 9bd0c85
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions app/router.options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { RouterConfig } from '@nuxt/schema';

// https://router.vuejs.org/api/interfaces/routeroptions.html
export default <RouterConfig>{
scrollBehavior(to, _form, savedPosition) {
return new Promise((resolve) => {
setTimeout(() => {
if (history.state.smooth) {
resolve({
el: history.state.smooth,
behavior: 'smooth',
});
}

if (to.hash) {
const el = document.querySelector(to.hash) as any;
if (!el)
resolve({ el: to.hash, top: 0 });

const { marginTop } = getComputedStyle(el);
const marginTopValue = Number.parseInt(marginTop);
const offset = (document.querySelector(to.hash) as any).offsetTop - marginTopValue;

resolve({
top: offset,
behavior: 'smooth',
});
}

// Scroll to top of window
if (savedPosition)
resolve(savedPosition);
else
resolve({ top: 0 });
}, 1); // Hack page wise navigation
});
},
};

0 comments on commit 9bd0c85

Please sign in to comment.