Skip to content

Commit 4ce118d

Browse files
fy0QingWei-Li
authored andcommitted
fixed sidebar item inactive issue & fixed _sidebar.md 404 issue & smooth scroll (#242)
* fixed sidebar item inactive issue & fixed _sidebar.md 404 issue & smooth scroll * sidebar load fix
1 parent 3ad1645 commit 4ce118d

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"marked": "^0.3.6",
3434
"prismjs": "^1.6.0",
3535
"tinydate": "^1.0.0",
36+
"tweezer.js": "^1.4.0",
3637
"zoom-image": "^0.1.4"
3738
},
3839
"devDependencies": {

src/core/event/scroll.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
11
import { isMobile } from '../util/env'
22
import * as dom from '../util/dom'
3+
import Tweezer from 'tweezer.js'
34

45
const nav = {}
56
let hoverOver = false
7+
let scroller = null
8+
let enableScrollEvent = true
9+
10+
function scrollTo (el) {
11+
if (scroller) scroller.stop();
12+
enableScrollEvent = false;
13+
scroller = new Tweezer({
14+
start: window.scrollY,
15+
end: el.getBoundingClientRect().top + window.scrollY,
16+
duration: 500
17+
})
18+
.on('tick', v => window.scrollTo(0, v))
19+
.on('done', () => { enableScrollEvent = true; scroller = null; })
20+
.begin()
21+
}
622

723
function highlight () {
24+
if (!enableScrollEvent) return
825
const sidebar = dom.getNode('.sidebar')
926
const anchors = dom.findAll('.anchor')
1027
const wrap = dom.find(sidebar, '.sidebar-nav')
1128
let active = dom.find(sidebar, 'li.active')
12-
const top = dom.body.scrollTop
29+
let doc = document.documentElement
30+
const top = doc && doc.scrollTop || document.body.scrollTop
1331
let last
1432

1533
for (let i = 0, len = anchors.length; i < len; i += 1) {
@@ -79,7 +97,13 @@ export function scrollActiveSidebar (router) {
7997

8098
export function scrollIntoView (id) {
8199
const section = dom.find('#' + id)
82-
section && section.scrollIntoView()
100+
section && scrollTo(section)
101+
102+
let li = nav[id]
103+
const sidebar = dom.getNode('.sidebar')
104+
let active = dom.find(sidebar, 'li.active')
105+
active && active.classList.remove('active')
106+
li && li.classList.add('active')
83107
}
84108

85109
const scrollEl = dom.$.scrollingElement || dom.$.documentElement

0 commit comments

Comments
 (0)