site: robust scroll by hash

This commit is contained in:
07akioni 2021-04-21 19:56:30 +08:00
parent 6870bb2777
commit e8355e5074
2 changed files with 7 additions and 6 deletions

View File

@ -29,12 +29,6 @@ export default {
onMounted(() => {
loadingBarApiRef.value = loadingBar
loadingBar.finish()
const memoedHash = window.location.hash
if (memoedHash) {
// scroll to hashed element
window.location.hash = ''
window.location.hash = memoedHash
}
})
return {
isXs: isXsRef

View File

@ -1,3 +1,4 @@
import { nextTick } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
export const loadingBarApiRef = {}
@ -22,6 +23,12 @@ export default function createDemoRouter (app, routes) {
if (loadingBarApiRef.value) {
loadingBarApiRef.value.finish()
}
if (to.hash) {
nextTick(() => {
const el = document.querySelector(to.hash)
if (el) el.scrollIntoView()
})
}
}
})