I am using React and trying to scroll a little past where an anchor link is supposed to link to after a hash change.
I have made a codesandbox here.
The answer provided here should work but for some reason it isn't. Can anyone see where I may be going wrong? The scrollToId
function does get called, but for some reason doesn't scroll to my desired location.
const Page = () => {
let scrollToId = () => {
if(location.hash.length !== 0) {
window.scrollTo(window.scrollX, window.scrollY + 200);
}
}
window.addEventListener("hashchange", scrollToId);
return (
<div>
<a href="#one">Link</a>
<p id="one">One</p>
</div>
);
}