From 34edadcbb7121f0273603ed9004231336a8751c2 Mon Sep 17 00:00:00 2001 From: Matteias Collet Date: Wed, 31 Mar 2021 23:06:05 +0200 Subject: [PATCH] fix scroll helper --- site/.vuepress/enhanceApp.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/site/.vuepress/enhanceApp.js b/site/.vuepress/enhanceApp.js index 3c41d42..10fc222 100755 --- a/site/.vuepress/enhanceApp.js +++ b/site/.vuepress/enhanceApp.js @@ -13,12 +13,15 @@ export default ({ if (typeof document === 'undefined') return; document.onreadystatechange = () => { if (document.readyState === 'complete') { - const { hash } = location; - const decoded = decodeURIComponent(hash); - const targetAnchor = document.querySelector(decoded) ?? document.querySelector(hash); + if (location.hash && location.hash.length > 0) { + const { hash } = location; - if (targetAnchor && targetAnchor.offsetTop) { - window.scrollTo({ top: targetAnchor.offsetTop, behavior: 'smooth' }); + const decoded = decodeURIComponent(hash); + const targetAnchor = document.querySelector(decoded) ?? document.querySelector(hash); + + if (targetAnchor && targetAnchor.offsetTop) { + window.scrollTo({ top: targetAnchor.offsetTop, behavior: 'smooth' }); + } } } };