Skip to content

Commit

Permalink
fix: avoid scroll behavior reliance on .nav-bar class
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 16, 2021
1 parent 3fed6f9 commit 9b35dfc
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/client/app/components/Content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export const Content = defineComponent({
name: 'VitePressContent',
setup() {
const route = useRoute()
return () => (route.component ? h(route.component) : null)
return () =>
h('div', { style: { position: 'relative' } }, [
route.component ? h(route.component) : null
])
}
})
4 changes: 1 addition & 3 deletions src/client/app/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,11 @@ export function useRoute(): Route {
}

function scrollTo(el: HTMLElement, hash: string, smooth = false) {
const nav = document.querySelector('.nav-bar')
const pageOffset = nav ? (nav as HTMLElement).offsetHeight : 0
const target = el.classList.contains('.header-anchor')
? el
: document.querySelector(decodeURIComponent(hash))
if (target) {
const targetTop = (target as HTMLElement).offsetTop - pageOffset - 15
const targetTop = (target as HTMLElement).offsetTop
// only smooth scroll if distance is smaller than screen height.
if (!smooth || Math.abs(targetTop - window.scrollY) > window.innerHeight) {
window.scrollTo(0, targetTop)
Expand Down
6 changes: 1 addition & 5 deletions src/client/theme-default/components/Page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ import NextAndPrevLinks from './NextAndPrevLinks.vue'
<div class="container">
<slot name="top" />

<div class="content">
<Content />
</div>

<Content class="content" />
<PageFooter />

<NextAndPrevLinks />

<slot name="bottom" />
Expand Down

0 comments on commit 9b35dfc

Please sign in to comment.