1.0 features (#21)

* feat: hash routing, close #2

* Fix router bug

* Remove console

* Add hash router docs

* Improved scrolling on mobile

* Add change log

* Use hash router
This commit is contained in:
cinwell.li 2016-12-08 21:11:18 +08:00 committed by GitHub
commit 864935bfc1
15 changed files with 174 additions and 60 deletions

View file

@ -13,9 +13,11 @@ export function scrollActiveSidebar () {
for (let i = 0, len = lis.length; i < len; i += 1) {
const li = lis[i]
const a = li.querySelector('a')
let href = li.querySelector('a').getAttribute('href')
nav[a.getAttribute('href').slice(1)] = li
if (href !== '/') href = href.match(/#([^#]+)$/g)[0].slice(1)
nav[href] = li
}
function highlight () {
@ -26,8 +28,7 @@ export function scrollActiveSidebar () {
if (bcr.top < 10 && bcr.bottom > 10) {
const li = nav[node.id]
if (!li) return
if (li === active) return
if (!li || li === active) return
if (active) active.setAttribute('class', '')
li.setAttribute('class', 'active')
@ -42,9 +43,9 @@ export function scrollActiveSidebar () {
highlight()
function scrollIntoView () {
const id = window.location.hash.slice(1)
if (!id) return
const section = document.querySelector('#' + id)
const id = window.location.hash.match(/#[^#\/]+$/g)
if (!id || !id.length) return
const section = document.querySelector(id[0])
if (section) section.scrollIntoView()
}
@ -57,7 +58,7 @@ export function scrollActiveSidebar () {
* Acitve link
*/
export function activeLink (dom, activeParent) {
const host = document.location.origin + document.location.pathname
const host = window.location.href
dom = typeof dom === 'object' ? dom : document.querySelector(dom)
if (!dom) return
@ -67,6 +68,10 @@ export function activeLink (dom, activeParent) {
activeParent
? node.parentNode.setAttribute('class', 'active')
: node.setAttribute('class', 'active')
} else {
activeParent
? node.parentNode.removeAttribute('class')
: node.removeAttribute('class')
}
})
}