docs: improve anchor scrolling

This commit is contained in:
陈嘉涵
2020-01-04 20:10:29 +08:00
parent bbaa9859be
commit 04f67f16bc
7 changed files with 37 additions and 25 deletions

View File

@@ -1,6 +1,7 @@
import Vue from 'vue';
import App from './App';
import { router } from './router';
import { scrollToAnchor } from './utils';
if (process.env.NODE_ENV !== 'production') {
Vue.config.productionTip = false;
@@ -10,15 +11,7 @@ new Vue({
el: '#app',
mounted() {
if (this.$route.hash) {
// wait page init
setTimeout(() => {
const el = document.querySelector(this.$route.hash);
if (el) {
el.scrollIntoView({
behavior: 'smooth'
});
}
}, 1000);
scrollToAnchor(this.$route.hash);
}
},
render: h => h(App),

View File

@@ -0,0 +1,19 @@
export function scrollToAnchor(selector) {
let count = 0;
const timer = setInterval(() => {
const el = document.querySelector(selector);
if (el) {
el.scrollIntoView({
behavior: 'smooth'
});
clearInterval(timer);
} else {
count++;
if (count > 10) {
clearInterval(timer);
}
}
}, 100);
}