fix: avoid scroll behavior reliance on .nav-bar class

pull/317/head
Evan You 3 years ago
parent 3fed6f94e5
commit 9b35dfcde4

@ -5,6 +5,9 @@ export const Content = defineComponent({
name: 'VitePressContent', name: 'VitePressContent',
setup() { setup() {
const route = useRoute() const route = useRoute()
return () => (route.component ? h(route.component) : null) return () =>
h('div', { style: { position: 'relative' } }, [
route.component ? h(route.component) : null
])
} }
}) })

@ -172,13 +172,11 @@ export function useRoute(): Route {
} }
function scrollTo(el: HTMLElement, hash: string, smooth = false) { 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') const target = el.classList.contains('.header-anchor')
? el ? el
: document.querySelector(decodeURIComponent(hash)) : document.querySelector(decodeURIComponent(hash))
if (target) { 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. // only smooth scroll if distance is smaller than screen height.
if (!smooth || Math.abs(targetTop - window.scrollY) > window.innerHeight) { if (!smooth || Math.abs(targetTop - window.scrollY) > window.innerHeight) {
window.scrollTo(0, targetTop) window.scrollTo(0, targetTop)

@ -8,12 +8,8 @@ import NextAndPrevLinks from './NextAndPrevLinks.vue'
<div class="container"> <div class="container">
<slot name="top" /> <slot name="top" />
<div class="content"> <Content class="content" />
<Content />
</div>
<PageFooter /> <PageFooter />
<NextAndPrevLinks /> <NextAndPrevLinks />
<slot name="bottom" /> <slot name="bottom" />

Loading…
Cancel
Save