Scroll Top - A Back-to-Top Button for Your Page (jQuery)
On a page made up of a long piece of writing, getting back to the top after scrolling down can be a hassle at times. You can easily get around this with a button that stays hidden while you are at the top of the page and appears on the right side once you start scrolling down.
First, let's specify the icon file to be used
Then let's add our style to our CSS file
It should sit in the bottom right corner and be invisible
#ScrollTop{
position: fixed;
right: 20px;
bottom: 15px;
cursor: pointer;
display: none;
}
When the icon is clicked, let's scroll the page to the top
$("#ScrollTop").click(function() {
$("html,body").stop().animate({ scrollTop: "0" }, 1000);
});
The icon should become invisible when the page is at the top and become visible when it is scrolled down
$(window).scroll(function() {
var length = $(document).scrollTop();
if (length > 300){
$("#ScrollTop").fadeIn(500);
}
else{
$("#ScrollTop").fadeOut(500);
}
});