diff --git a/src/core/event/index.js b/src/core/event/index.js index 8489fc13e..79e2d1f34 100644 --- a/src/core/event/index.js +++ b/src/core/event/index.js @@ -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') } } diff --git a/src/core/event/scroll.js b/src/core/event/scroll.js index 0f77efdc4..9aa206354 100644 --- a/src/core/event/scroll.js +++ b/src/core/event/scroll.js @@ -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 @@ -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 @@ -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') diff --git a/src/core/router/history/hash.js b/src/core/router/history/hash.js index eccc6faeb..37858521d 100644 --- a/src/core/router/history/hash.js +++ b/src/core/router/history/hash.js @@ -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) }