From 3adc965dd09490b7efa1cce9f09b0a3b30970277 Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Wed, 19 Apr 2023 17:30:39 +0800 Subject: ✨优化文档 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/app/router.options.ts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 docs/app/router.options.ts (limited to 'docs/app/router.options.ts') diff --git a/docs/app/router.options.ts b/docs/app/router.options.ts new file mode 100644 index 0000000..9380f05 --- /dev/null +++ b/docs/app/router.options.ts @@ -0,0 +1,38 @@ +import type { RouterConfig } from '@nuxt/schema' +// https://router.vuejs.org/api/interfaces/routeroptions.html +export default { + scrollBehavior (to, _form, savedPosition) { + if (history.state.stop) { return } + + if (history.state.smooth) { + return { + el: history.state.smooth, + behavior: 'smooth' + } + } + + if (to.hash) { + const el = document.querySelector(to.hash) as any + + if (!el) { return } + + const { marginTop } = getComputedStyle(el) + + const marginTopValue = parseInt(marginTop) + + const offset = (document.querySelector(to.hash) as any).offsetTop - marginTopValue + + return { + top: offset, + behavior: 'smooth' + } + } + + // Scroll to top of window + if (savedPosition) { + return savedPosition + } else { + return { top: 0 } + } + } +} -- cgit v1.2.3-70-g09d2