JSFiddle - React, Tailwind, and code Playground
by Desain Kode
HTML
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<div id="content">Scroll ↓</div>
<a href="#" id="back-to-top" title="Back to top"><i class='fa fa-chevron-up'></i></a>
CSS
#back-to-top {
visibility: hidden;
background-color: rgba(0, 0, 0, .7);
color: #fff;
width: 42px;
height: 42px;
text-align: center;
line-height: 42px;
position: fixed;
bottom: 20px;
right: 20px;
z-index: 90;
cursor: pointer;
opacity: 0;
border-radius: 3px;
-webkit-transform: translateZ(0);
transition: all .4s
}
#back-to-top .fa {
font-size: 22px;
vertical-align: middle
}
#back-to-top:hover {
background-color: #46a3fa;
color: #fff;
opacity: 1;
}
#back-to-top.show {
visibility: visible;
opacity: 1;
}
#content {
height: 2000px;
}
JavaScript
if ($('#back-to-top').length) {
var scrollTrigger = 100, // px
backToTop = function() {
var scrollTop = $(window).scrollTop();
if (scrollTop > scrollTrigger) {
$('#back-to-top').addClass('show');
} else {
$('#back-to-top').removeClass('show');
}
};
backToTop();
$(window).on('scroll', function() {
backToTop();
});
$('#back-to-top').on('click', function(e) {
e.preventDefault();
$('html,body').animate({
scrollTop: 0
}, 700);
});
}