refactor(core): and sidebar event

This commit is contained in:
qingwei.li 2017-02-18 16:18:07 +08:00 • committed by cinwell.li
commit fbd51afdcb
6 changed files with 100 additions and 17 deletions

View file

@ -1,4 +1,4 @@
export function activeSidebar () {
export function scrollActiveSidebar () {
}

View file

@ -1,5 +1,6 @@
import { isMobile } from '../util/env'
import { getNode, on, body } from '../util/dom'
import { getNode, on, body, findAll, toggleClass } from '../util/dom'
import { getHash } from '../route/hash'
/**
* Toggle button
*/
@ -14,7 +15,7 @@ export function btn (el) {
on(sidebar, 'click', () => {
toggle()
setTimeout(() => activeLink(sidebar, true), 0)
setTimeout(() => getAndActive(true), 0)
})
}
}
@ -23,6 +24,27 @@ export function sticky () {
}
export function activeLink () {
export function getAndActive (el, isParent) {
const dom = getNode(el)
const links = findAll(dom, 'a')
const hash = '#' + getHash()
let target
links
.sort((a, b) => b.href.length - a.href.length)
.forEach(a => {
const href = a.getAttribute('href')
const node = isParent ? a.parentNode : a
if (hash.indexOf(href) === 0 && !target) {
target = a
toggleClass(node, 'add', 'active')
} else {
toggleClass(node, 'remove', 'active')
}
})
// TODO FIXED
return target
}