fix(vue-app): respect scroll-margin-top when navigating with hash (#9187)

This commit is contained in:
Daniel Roe 2021-05-15 12:15:31 +01:00 committed by GitHub
parent 877ebcd3a0
commit 00aeff62e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -66,9 +66,15 @@ export default function (to, from, savedPosition) {
hash = '#' + window.CSS.escape(hash.substr(1))
}
try {
if (document.querySelector(hash)) {
const el = document.querySelector(hash)
if (el) {
// scroll to anchor by returning the selector
position = { selector: hash }
// Respect any scroll-margin-top set in CSS when scrolling to anchor
const y = Number(getComputedStyle(el)['scroll-margin-top']?.replace('px', ''))
if (y) {
position.offset = { y }
}
}
} catch (e) {
<%= isTest ? '// eslint-disable-next-line no-console' : '' %>