@mr-hope/vuepress-plugin-smooth-scroll
Version:
smooth-scroll plugin for vuepress
42 lines • 1.44 kB
JavaScript
import "./styles/index.styl";
const 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) {
window.scrollTo({
top: savedPosition.y,
behavior: "smooth",
});
}
else if (to.hash) {
if (!Vue.$vuepress.$get("disableScrollBehavior"))
setTimeout(() => {
const targetAnchor = decodeURI(to.hash.slice(1));
const targetElement = document.getElementById(targetAnchor) ||
document.querySelector(`[name='${targetAnchor}']`);
if (targetElement) {
window.scrollTo({
top: getElementPosition(targetElement).y,
behavior: "smooth",
});
}
}, SMOOTH_SCROLL_DELAY);
}
else {
window.scrollTo({
top: 0,
behavior: "smooth",
});
}
};
};
export default enhanceApp;
//# sourceMappingURL=enhanceApp.js.map