-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathenhanceApp.js
More file actions
71 lines (68 loc) · 1.98 KB
/
enhanceApp.js
File metadata and controls
71 lines (68 loc) · 1.98 KB
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
68
69
70
71
const SCROLL_OFFSET = 75
// fork from vue-router@3.0.2
// src/util/scroll.js
function getElementPosition (el) {
const docEl = document.documentElement
const docRect = docEl.getBoundingClientRect()
const elRect = el.getBoundingClientRect()
return {
x: elRect.left - docRect.left,
y: elRect.top - docRect.top
}
}
module.exports = ({ Vue, options, router }) => {
router.options.scrollBehavior = (to, from, savedPosition) => {
if (savedPosition) {
return window.scrollTo({
top: savedPosition.y - SCROLL_OFFSET,
behavior: 'smooth'
})
} else if (from.path === to.path && to.hash) {
if (Vue.$vuepress.$get('disableScrollBehavior')) {
return
}
const targetAnchor = to.hash
const targetName = targetAnchor.slice(1)
const targetElement =
document.getElementById(targetName) ||
document.querySelector(`[name=${targetName}]`)
if (targetElement) {
return window.scrollTo({
top: getElementPosition(targetElement).y - SCROLL_OFFSET,
behavior: 'smooth'
})
}
window.onload = () => {
if (location.hash.slice(1) != "") {
const element = document.getElementById(location.hash.slice(1))
if (element) {
element.scrollIntoView()
}
}
}
} else {
const html = document.querySelector('html')
html.style.scrollBehavior = 'auto'
window.scrollTo({ top: 0 })
html.style.scrollBehavior = ''
}
if (location.hash) {
setTimeout(function() {
if (location.hash.slice(1) != "") {
const element = document.getElementById(location.hash.slice(1))
if (element) {
element.scrollIntoView()
}
}
}, 250);
}
window.onload = () => {
if (location.hash.slice(1) != "") {
const element = document.getElementById(location.hash.slice(1))
if (element) {
element.scrollIntoView()
}
}
}
}
}