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
This commit is contained in:
parent
3ad1645b90
commit
4ce118d60f
2 changed files with 27 additions and 2 deletions
|
|
@ -33,6 +33,7 @@
|
||||||
"marked": "^0.3.6",
|
"marked": "^0.3.6",
|
||||||
"prismjs": "^1.6.0",
|
"prismjs": "^1.6.0",
|
||||||
"tinydate": "^1.0.0",
|
"tinydate": "^1.0.0",
|
||||||
|
"tweezer.js": "^1.4.0",
|
||||||
"zoom-image": "^0.1.4"
|
"zoom-image": "^0.1.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,33 @@
|
||||||
import { isMobile } from '../util/env'
|
import { isMobile } from '../util/env'
|
||||||
import * as dom from '../util/dom'
|
import * as dom from '../util/dom'
|
||||||
|
import Tweezer from 'tweezer.js'
|
||||||
|
|
||||||
const nav = {}
|
const nav = {}
|
||||||
let hoverOver = false
|
let hoverOver = false
|
||||||
|
let scroller = null
|
||||||
|
let enableScrollEvent = true
|
||||||
|
|
||||||
|
function scrollTo (el) {
|
||||||
|
if (scroller) scroller.stop();
|
||||||
|
enableScrollEvent = false;
|
||||||
|
scroller = new Tweezer({
|
||||||
|
start: window.scrollY,
|
||||||
|
end: el.getBoundingClientRect().top + window.scrollY,
|
||||||
|
duration: 500
|
||||||
|
})
|
||||||
|
.on('tick', v => window.scrollTo(0, v))
|
||||||
|
.on('done', () => { enableScrollEvent = true; scroller = null; })
|
||||||
|
.begin()
|
||||||
|
}
|
||||||
|
|
||||||
function highlight () {
|
function highlight () {
|
||||||
|
if (!enableScrollEvent) return
|
||||||
const sidebar = dom.getNode('.sidebar')
|
const sidebar = dom.getNode('.sidebar')
|
||||||
const anchors = dom.findAll('.anchor')
|
const anchors = dom.findAll('.anchor')
|
||||||
const wrap = dom.find(sidebar, '.sidebar-nav')
|
const wrap = dom.find(sidebar, '.sidebar-nav')
|
||||||
let active = dom.find(sidebar, 'li.active')
|
let active = dom.find(sidebar, 'li.active')
|
||||||
const top = dom.body.scrollTop
|
let doc = document.documentElement
|
||||||
|
const top = doc && doc.scrollTop || document.body.scrollTop
|
||||||
let last
|
let last
|
||||||
|
|
||||||
for (let i = 0, len = anchors.length; i < len; i += 1) {
|
for (let i = 0, len = anchors.length; i < len; i += 1) {
|
||||||
|
|
@ -79,7 +97,13 @@ export function scrollActiveSidebar (router) {
|
||||||
|
|
||||||
export function scrollIntoView (id) {
|
export function scrollIntoView (id) {
|
||||||
const section = dom.find('#' + id)
|
const section = dom.find('#' + id)
|
||||||
section && section.scrollIntoView()
|
section && scrollTo(section)
|
||||||
|
|
||||||
|
let li = nav[id]
|
||||||
|
const sidebar = dom.getNode('.sidebar')
|
||||||
|
let active = dom.find(sidebar, 'li.active')
|
||||||
|
active && active.classList.remove('active')
|
||||||
|
li && li.classList.add('active')
|
||||||
}
|
}
|
||||||
|
|
||||||
const scrollEl = dom.$.scrollingElement || dom.$.documentElement
|
const scrollEl = dom.$.scrollingElement || dom.$.documentElement
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue