In my website, I have a Scroll to Top section. But as most sites achieve this via jQuery
scrollTop
, I used the latest CSS scroll-behavior: smooth;
. Now the way it worked is I have given my body
tag a id
blog
.
e.g. <body id="blog">
and the scroll to top button like:
<a id="move-to-top" href="#blog">Scroll</a>
Now this is working perfectly fine and I have a smooth scroll on my website. But when anyone clicks the Scroll To Top button the URL gets attended with #blog
which I do not like.
I was wondering if there is any way I can take out the #blog
of the URL when anyone clicks on the button.
If I do the following:
document.getElementById("move-to-top").addEventListener( "click", (e) => {
e.preventDefault()
} )
The scroll to top stops working as I am preventing default. I have no idea how else I can do this. Any guidance will be helpful.