JSFiddle - React, Tailwind, and code Playground
HTML
<span>space!</span>
<a href="#" onclick="animateToTop(event)">to the top</a>
<span>more space!</span>
<a href="#" onclick="animateToTop(event)">to the top</a>
<span>even more space!</span>
<a href="#" onclick="animateToTop(event)">to the top</a>
CSS
span {
display: block;
margin: 250px 0;
}
JavaScript
function animateToTop(e) {
e.preventDefault();
var scrollToTop = window.setInterval(function() {
var pos = window.pageYOffset;
if ( pos > 0 ) {
window.scrollTo( 0, pos - 20 );
} else {
window.clearInterval( scrollToTop );
}
}, 16);
}