JSFiddle - React, Tailwind, and code Playground

by Eduardo Almeida

HTML

<!-- Back to top button using pure JavaScript -->
<a href="#" id="back-to-top">^</a>
<div style="height: 900px;"></div>

CSS

#back-to-top {
  text-decoration: none;
  font-size: 24pt;
  font-weight: bold;
  padding: 15px 20px 7px 20px;
  background-color: blue;
  color: white;
  position: fixed;
  bottom: 10px;
  right: 10px;
  border-radius: 15px;
  opacity: .7;
  transition: opacity .7s ease-out;
  display: none;
}

JavaScript

var backToTop = document.getElementById("back-to-top");

// Faz o link aparecer ou desaparecer dependendo da distância pro topo
window.onscroll = function(){
    if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
        backToTop.style.display = "block";
    } else {
        backToTop.style.display = "none";
    }
};

// Scrolling pro topo da página ao clicar o link
backToTop.addEventListener("click", function() {
    document.body.scrollTop = 0;
    document.documentElement.scrollTop = 0;
});