forked from didi/cube-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.vue
67 lines (64 loc) · 1.38 KB
/
App.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<template>
<div id="app">
<router-view></router-view>
</div>
</template>
<script>
export default {
data () {
return {
oldPath: window.location.hash
}
},
mounted () {
const docViewEle = document.body
docViewEle.addEventListener('click', (e) => {
let target = e.target
while (target && target.className !== 'anchor') {
target = target.parentNode
}
if (target) {
e.preventDefault()
this.scrollToHash(target.getAttribute('href'))
}
})
},
watch: {
$route (to, from) {
this.oldPath = `#${to.path}`
this.scrollToHash(to.hash)
}
},
methods: {
scrollToHash (hash) {
const pattern = /#cube-(.*)-anchor/
let newUrl = ''
let matcher
if (!hash) {
matcher = window.location.hash.match(pattern)
if (matcher) {
hash = matcher[0]
}
}
if (hash) {
matcher = hash.match(pattern)
newUrl = this.oldPath + hash
window.location.hash = newUrl
setTimeout(() => {
const anchor = decodeURIComponent(matcher[1])
const el = document.querySelector(`#${anchor}`)
el && el.scrollIntoView()
})
}
}
}
}
</script>
<style lang="stylus" rel="stylesheet/stylus">
#app
width: 100%
height: 100%
background-color: white
.ov-hidden
overflow: hidden
</style>