title: Transition to a scroll back to top button
body: <p>I created a button on my site to scroll back to the top of my page by following this tutorial: <a href="https://www.w3schools.com/howto/howto_js_scroll_to_top.asp" rel="nofollow noreferrer">W3Schools</a></p>
  <p>The problem is that when you click on the button, there is no transition to the top, you are just &quot;teleport&quot; to the top of the page. So if someone will know how to improve the transition or how to make a button otherwise.</p>
  <p>HTML code :</p>
  <p><code>&lt;button onclick=&quot;topFunction()&quot; id=&quot;myBtn&quot; title=&quot;Go to top&quot;&gt;Top&lt;/button&gt;</code></p>
  <p>CSS code :</p>
  <pre><code>#myBtn {
    display: none; /* Hidden by default */
    position: fixed; /* Fixed/sticky position */
    bottom: 20px; /* Place the button at the bottom of the page */
    right: 30px; /* Place the button 30px from the right */
    z-index: 99; /* Make sure it does not overlap */
    border: none; /* Remove borders */
    outline: none; /* Remove outline */
    background-color: red; /* Set a background color */
    color: white; /* Text color */
    cursor: pointer; /* Add a mouse pointer on hover */
    padding: 15px; /* Some padding */
    border-radius: 10px; /* Rounded corners */
    font-size: 18px; /* Increase font size */
  }
  
  #myBtn:hover {
    background-color: #555; /* Add a dark-grey background on hover */
  }
  </code></pre>
  <p>JavaScript code :</p>
  <pre><code>//Get the button:
  mybutton = document.getElementById(&quot;myBtn&quot;);
  
  // 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 &gt; 20 || document.documentElement.scrollTop &gt; 20) {
      mybutton.style.display = &quot;block&quot;;
    } else {
      mybutton.style.display = &quot;none&quot;;
    }
  }
  
  // When the user clicks on the button, scroll to the top of the document
  function topFunction() {
    document.body.scrollTop = 0; // For Safari
    document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
  }
  </code></pre>