JSFiddle - React, Tailwind, and code Playground
HTML
<h1>Top of Page</h1>
<p><em>Try scrolling and vertically resizing the window.</em></p>
<ul>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
</ul>
<h2>Bottom of page</h2>
<a id="toTopLink" class="hidden" href="#">Scroll to Top</a>
CSS
#toTopLink {
position: fixed;
bottom: 20px;
right: 20px;
padding: 20px;
background: white;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.25);
display: block;
}
#toTopLink.hidden {
display: none;
}
JavaScript
window.addEventListener('scroll', () => {
const toTopLink = document.getElementById('toTopLink');
if (window.scrollY > 0) {
toTopLink.classList.remove('hidden');
} else {
toTopLink.classList.add('hidden');
}
}, true);