mirror of
https://github.com/youzan/vant.git
synced 2025-10-18 09:24:25 +00:00
docs: improve anchor scrolling
This commit is contained in:
@@ -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),
|
||||
|
19
packages/vant-cli/site/desktop/utils.js
Normal file
19
packages/vant-cli/site/desktop/utils.js
Normal 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);
|
||||
}
|
Reference in New Issue
Block a user