From e549f3954fa668536c5cedd878297550002027d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Fri, 16 Oct 2020 09:28:14 +0200 Subject: [PATCH] improve scrolling again --- docs/gridsome.client.js | 15 +++++++++++++++ docs/src/main.js | 23 +---------------------- 2 files changed, 16 insertions(+), 22 deletions(-) create mode 100644 docs/gridsome.client.js diff --git a/docs/gridsome.client.js b/docs/gridsome.client.js new file mode 100644 index 00000000..65d2f3f2 --- /dev/null +++ b/docs/gridsome.client.js @@ -0,0 +1,15 @@ +export default function (Vue, options, context) { + + context.router.afterEach(to => { + if (to.hash) { + setTimeout(() => { + const element = document.getElementById(to.hash.substr(1)) + const top = element.offsetTop + const offset = parseFloat(getComputedStyle(element).scrollMarginTop) + + window.scrollTo(0, top - offset) + }, 0) + } + }) + +} diff --git a/docs/src/main.js b/docs/src/main.js index 68c8b968..99476dbe 100644 --- a/docs/src/main.js +++ b/docs/src/main.js @@ -5,30 +5,9 @@ import 'prismjs/components/prism-scss.js' import PortalVue from 'portal-vue' import App from '~/layouts/App' -export default function (Vue, { router }) { +export default function (Vue) { Vue.use(PortalVue) Vue.component('Layout', App) Vue.component('Demo', () => import(/* webpackChunkName: "demo" */ '~/components/Demo')) Vue.component('LiveDemo', () => import(/* webpackChunkName: "live-demo" */ '~/components/LiveDemo')) - - router.options.scrollBehavior = async (to, from, savedPosition) => { - if (to.hash) { - const elem = document.querySelector(to.hash) - - if (elem) { - const offset = parseFloat(getComputedStyle(elem).scrollMarginTop) - return { - selector: to.hash, - offset: { y: offset }, - } - } - } - - if (savedPosition) { - return savedPosition - } - - return { x: 0, y: 0 } - } - }