[Doc] support title anchor (#3845)

This commit is contained in:
neverland
2019-07-13 20:41:15 +08:00
committed by GitHub
parent 0d2f010e44
commit 7b9331aee4
5 changed files with 71 additions and 29 deletions
+21 -3
View File
@@ -20,11 +20,17 @@ importAll(docs, docsFromPackages);
const router = new VueRouter({
mode: 'hash',
routes: routes({ componentMap: docs })
routes: routes({ componentMap: docs }),
scrollBehavior(to, from, savedPosition) {
if (to.hash) {
return { selector: to.hash };
}
return savedPosition || { x: 0, y: 0 };
}
});
router.afterEach(() => {
window.scrollTo(0, 0);
router.afterEach(path => {
Vue.nextTick(() => window.syncPath());
});
@@ -36,6 +42,18 @@ if (process.env.NODE_ENV !== 'production') {
new Vue({
el: '#app',
mounted() {
if (this.$route.hash) {
setTimeout(() => {
const el = document.querySelector(this.$route.hash);
if (el) {
el.scrollIntoView({
behavior: 'smooth'
});
}
}, 500);
}
},
render: h => h(App),
router
});
+1
View File
@@ -39,6 +39,7 @@ const registerRoute = ({ mobile, componentMap }) => {
component = DemoPages;
} else if (mobile) {
const module = componentMap[`./${path}/demo/index.vue`];
if (module) {
component = demoWrapper(module, path);
}
+5 -1
View File
@@ -31,6 +31,10 @@ export function initIframeRouter() {
window.changePath = function (lang, path = '') {
setLang(lang);
window.vueRouter.replace(path);
// should preserve hash for anchor
if (window.vueRouter.currentRoute.path !== path) {
window.vueRouter.replace(path);
}
};
}