Skip to content

Commit

Permalink
fix: incorrect active link (#281)
Browse files Browse the repository at this point in the history
* fix scroll issue in IE

* add meta tag for IE browser

* fix link render issue after page refreshing

* fix issue of incorrect active link

Should use both `path` and `id` to identify link, as `id` might not be unique under different paths
  • Loading branch information
laysent authored and QingWei-Li committed Oct 15, 2017
1 parent 6949455 commit a3ab379
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/core/event/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { scrollIntoView } from './scroll'

export function eventMixin (proto) {
proto.$resetEvents = function () {
scrollIntoView(this.route.query.id)
scrollIntoView(this.route.path, this.route.query.id)
sidebar.getAndActive(this.router, 'nav')
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/core/event/scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ function highlight () {
}
}

function getNavKey (path, id) {
return `${path}?id=${id}`
}

export function scrollActiveSidebar (router) {
const cover = dom.find('.cover.show')
coverHeight = cover ? cover.offsetHeight : 0
Expand All @@ -82,7 +86,8 @@ export function scrollActiveSidebar (router) {
let href = a.getAttribute('href')

if (href !== '/') {
href = router.parse(href).query.id
const { query: { id }, path } = router.parse(href)
if (id) href = getNavKey(path, id)
}

if (href) nav[decodeURIComponent(href)] = li
Expand All @@ -100,13 +105,13 @@ export function scrollActiveSidebar (router) {
})
}

export function scrollIntoView (id) {
export function scrollIntoView (path, id) {
if (!id) return

const section = dom.find('#' + id)
section && scrollTo(section)

const li = nav[id]
const li = nav[getNavKey(path, id)]
const sidebar = dom.getNode('.sidebar')
const active = dom.find(sidebar, 'li.active')
active && active.classList.remove('active')
Expand Down
2 changes: 1 addition & 1 deletion src/core/router/history/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class HashHistory extends History {
let query = ''

const hashIndex = path.indexOf('#')
if (hashIndex) {
if (hashIndex >= 0) {
path = path.slice(hashIndex + 1)
}

Expand Down

0 comments on commit a3ab379

Please sign in to comment.