|
1 | 1 | import { isMobile } from '../util/env' |
2 | 2 | import * as dom from '../util/dom' |
| 3 | +import Tweezer from 'tweezer.js' |
3 | 4 |
|
4 | 5 | const nav = {} |
5 | 6 | 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 | +} |
6 | 22 |
|
7 | 23 | function highlight () { |
| 24 | + if (!enableScrollEvent) return |
8 | 25 | const sidebar = dom.getNode('.sidebar') |
9 | 26 | const anchors = dom.findAll('.anchor') |
10 | 27 | const wrap = dom.find(sidebar, '.sidebar-nav') |
11 | 28 | 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 |
13 | 31 | let last |
14 | 32 |
|
15 | 33 | for (let i = 0, len = anchors.length; i < len; i += 1) { |
@@ -79,7 +97,13 @@ export function scrollActiveSidebar (router) { |
79 | 97 |
|
80 | 98 | export function scrollIntoView (id) { |
81 | 99 | 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') |
83 | 107 | } |
84 | 108 |
|
85 | 109 | const scrollEl = dom.$.scrollingElement || dom.$.documentElement |
|
0 commit comments