0

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.

iSaumya
  • 1,503
  • 5
  • 21
  • 50
  • i have try this, https://stackblitz.com/edit/typescript-cp7vih?file=index.ts i am not myself satisfy by this solution but you can have look it can give you inspiration – Yanis-git May 07 '20 at 17:09

2 Answers2

1

You can use JQuery animate function

$('html,body').animate({scrollTop: $("#blog").offset().top},'slow');

Or you can use JavaScript scrollIntoView

document.getElementById('blog').scrollIntoView(true);

Taken answer from henser

If you simply want to get rid of #tag from your URL you can use history replace state

document.getElementById("move-to-top").addEventListener( "click", (e) => {
  history.replaceState([],"",window.location.href.split('#')[0])
});
Siraj Hussain
  • 874
  • 9
  • 25
  • Hi, thanks for your answer, but as mentioned above I am using the CSS `scroll-behaviour` and don't wanna use JS for this task which CSS is doing perfectly. – iSaumya May 07 '20 at 16:52
  • Please check my updated answer, you can remove #tag from url using browser history API – Siraj Hussain May 07 '20 at 17:10
  • Hey man thanks a lot for the help and especially the last `history.replaceState` code. But the crazy thing is when I am running it normally on the global scope it is working, but inside the event listener it is not working I can't find out why. This is super wired. – iSaumya May 08 '20 at 16:17
0

As you se, this uses Javascript to go to the top, not a link

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 20px;
}

#myBtn {
  display: none;
  position: fixed;
  bottom: 20px;
  right: 30px;
  z-index: 99;
  font-size: 18px;
  border: none;
  outline: none;
  background-color: red;
  color: white;
  cursor: pointer;
  padding: 15px;
  border-radius: 4px;
}

#myBtn:hover {
  background-color: #555;
}
</style>
</head>
<body>

<button onclick="topFunction()" id="myBtn" title="Go to top">Top</button>

<div style="background-color:black;color:white;padding:30px">Scroll Down</div>
<div style="background-color:lightgrey;padding:30px 30px 2500px">This example demonstrates how to create a "scroll to top" button that becomes visible 
  <strong>when the user starts to scroll the page</strong>.</div>

<script>
//Get the button
var mybutton = document.getElementById("myBtn");

// When the user scrolls down 20px from the top of the document, show the button
window.onscroll = function() {scrollFunction()};

function scrollFunction() {
  if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
    mybutton.style.display = "block";
  } else {
    mybutton.style.display = "none";
  }
}

// When the user clicks on the button, scroll to the top of the document
function topFunction() {
  document.body.scrollTop = 0;
  document.documentElement.scrollTop = 0;
}
</script>

</body>
</html>
  • See a tutorial at: https://www.w3schools.com/howto/howto_js_scroll_to_top.asp –  May 07 '20 at 16:41
  • Hi, I am well aware of the javascript approach of scrolling to top as I mentioned in my post but I don't want to use the javascript approach, instead, I want to use the CSS approach i.e. `scroll-behavior`. – iSaumya May 07 '20 at 16:44