I have a scroll to top button on my page, but when I click it, it doesnt scroll to top, it just takes me directly to the top, like as if I loaded a new page, but what I need is the scrolling animation.
javascript
window.onscroll = function() {
scrollFunction()
};
function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
document.getElementById("myBtn").style.display = "block";
} else {
document.getElementById("myBtn").style.display = "none";
}
}
// When the user clicks on the button, scroll to the top of the document
function topFunction() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
css
#myBtn {
display: none;
position: fixed;
bottom: 50px;
right: 20px;
z-index: 99;
border: teal;
outline: none;
background-color: #FF6347;
color: white;
cursor: pointer;
padding: 0px;
border-radius: 100px;
width: 40px;
height: 40px;
}
#myBtn i {
width: 40px;
height: 40px;
text-align: center;
z-index: 100;
line-height: 40px;
color: white;
}
#myBtn:hover {
background-color: #FF6320;
}
html
<button1 onclick="topFunction()" id="myBtn" title="Go to top"><i class="fa fa-angle-double-up" aria-hidden="true"></i></button1>