fix scroll helper

This commit is contained in:
Matteias Collet 2021-03-31 23:06:05 +02:00
parent 151e94bfac
commit 34edadcbb7

View file

@ -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' });
}
}
}
};