forked from docsifyjs/docsify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscroll.js
146 lines (125 loc) Β· 3.45 KB
/
scroll.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import {isMobile} from '../util/env'
import * as dom from '../util/dom'
import Tweezer from 'tweezer.js'
const nav = {}
let hoverOver = false
let scroller = null
let enableScrollEvent = true
let coverHeight = 0
function scrollTo(el) {
if (scroller) {
scroller.stop()
}
enableScrollEvent = false
scroller = new Tweezer({
start: window.pageYOffset,
end: el ? el.getBoundingClientRect().top + window.pageYOffset : 0,
duration: 500
})
.on('tick', v => window.scrollTo(0, v))
.on('done', () => {
enableScrollEvent = true
scroller = null
})
.begin()
}
function highlight(path) {
if (!enableScrollEvent) {
return
}
const sidebar = dom.getNode('.sidebar')
const anchors = dom.findAll('.anchor')
const wrap = dom.find(sidebar, '.sidebar-nav')
let active = dom.find(sidebar, 'li.active')
const doc = document.documentElement
const top = ((doc && doc.scrollTop) || document.body.scrollTop) - coverHeight
let last
for (let i = 0, len = anchors.length; i < len; i += 1) {
const node = anchors[i]
if (node.offsetTop > top) {
if (!last) {
last = node
}
break
} else {
last = node
}
}
if (!last) {
return
}
const li = nav[getNavKey(decodeURIComponent(path), last.getAttribute('data-id'))]
if (!li || li === active) {
return
}
active && active.classList.remove('active')
li.classList.add('active')
active = li
// Scroll into view
// https://github.com/vuejs/vuejs.org/blob/master/themes/vue/source/js/common.js#L282-L297
if (!hoverOver && dom.body.classList.contains('sticky')) {
const height = sidebar.clientHeight
const curOffset = 0
const cur = active.offsetTop + active.clientHeight + 40
const isInView =
active.offsetTop >= wrap.scrollTop && cur <= wrap.scrollTop + height
const notThan = cur - curOffset < height
const top = isInView ? wrap.scrollTop : notThan ? curOffset : cur - height
sidebar.scrollTop = top
}
}
function getNavKey(path, id) {
return `${path}?id=${id}`
}
export function scrollActiveSidebar(router) {
const cover = dom.find('.cover.show')
coverHeight = cover ? cover.offsetHeight : 0
const sidebar = dom.getNode('.sidebar')
const lis = dom.findAll(sidebar, 'li')
for (let i = 0, len = lis.length; i < len; i += 1) {
const li = lis[i]
const a = li.querySelector('a')
if (!a) {
continue
}
let href = a.getAttribute('href')
if (href !== '/') {
const {query: {id}, path} = router.parse(href)
if (id) {
href = getNavKey(path, id)
}
}
if (href) {
nav[decodeURIComponent(href)] = li
}
}
if (isMobile) {
return
}
const path = router.getCurrentPath()
dom.off('scroll', () => highlight(path))
dom.on('scroll', () => highlight(path))
dom.on(sidebar, 'mouseover', () => {
hoverOver = true
})
dom.on(sidebar, 'mouseleave', () => {
hoverOver = false
})
}
export function scrollIntoView(path, id) {
var sidebar = dom.getNode('.sidebar');
var active = dom.find(sidebar, 'li.active');
active && active.classList.remove('active');
if (!id) {
scrollTo(null);
} else {
var section = dom.find('#' + id);
section && scrollTo(section);
var li = nav[getNavKey(path, id)];
li && li.classList.add('active');
}
}
const scrollEl = dom.$.scrollingElement || dom.$.documentElement
export function scroll2Top(offset = 0) {
scrollEl.scrollTop = offset === true ? 0 : Number(offset)
}