improve scrolling again

This commit is contained in:
Philipp Kühn 2020-10-16 09:28:14 +02:00
parent a6041f2723
commit e549f3954f
2 changed files with 16 additions and 22 deletions

15
docs/gridsome.client.js Normal file
View File

@ -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)
}
})
}

View File

@ -5,30 +5,9 @@ import 'prismjs/components/prism-scss.js'
import PortalVue from 'portal-vue' import PortalVue from 'portal-vue'
import App from '~/layouts/App' import App from '~/layouts/App'
export default function (Vue, { router }) { export default function (Vue) {
Vue.use(PortalVue) Vue.use(PortalVue)
Vue.component('Layout', App) Vue.component('Layout', App)
Vue.component('Demo', () => import(/* webpackChunkName: "demo" */ '~/components/Demo')) Vue.component('Demo', () => import(/* webpackChunkName: "demo" */ '~/components/Demo'))
Vue.component('LiveDemo', () => import(/* webpackChunkName: "live-demo" */ '~/components/LiveDemo')) 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 }
}
} }