UNPKG

vuepress-plugin-smooth-scroll

Version:

VuePress plugin for smooth scrolling

43 lines 1.36 kB
// fork from vue-router@3.0.2 // src/util/scroll.js function getElementPosition(el) { const docEl = document.documentElement; const docRect = docEl.getBoundingClientRect(); const elRect = el.getBoundingClientRect(); return { x: elRect.left - docRect.left, y: elRect.top - docRect.top, }; } const enhanceApp = ({ Vue, router }) => { router.options.scrollBehavior = (to, from, savedPosition) => { if (savedPosition) { return window.scrollTo({ top: savedPosition.y, behavior: 'smooth', }); } else if (to.hash) { if (Vue.$vuepress.$get('disableScrollBehavior')) { return; } const targetAnchor = to.hash.slice(1); const targetElement = document.getElementById(targetAnchor) || document.querySelector(`[name='${targetAnchor}']`); if (targetElement) { return window.scrollTo({ top: getElementPosition(targetElement).y, behavior: 'smooth', }); } } else { return window.scrollTo({ top: 0, behavior: 'smooth', }); } }; }; export default enhanceApp; //# sourceMappingURL=enhanceApp.js.map