Add a Scroll to Top Button to your Website

Adding a sticky scroll-to-top button to your website adds further ease of navigation to your website by allowing a user to scroll to the top of any given web page with the click of a button.

Below is the HTML, CSS, and JavaScript required to add this functionality to your website.

Step 1

Add the HTML below to your web page

<div class="container" style="height: 1500px; width: 100%;">
<!-- Static navbar -->
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">
<img src="https://www.solodev.com/assets/email/logo.png" alt="Logo Solodev">
</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li class="dropdown-header">Nav header</li>
<li><a href="#">Separated link</a></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
</ul>
</div>
<!--/.nav-collapse -->
</div>
<!--/.container-fluid -->
</nav>
<!-- Main component for a primary marketing message or call to action -->
<div class="jumbotron">
<h1>Scroll Down</h1>
<p>This example is a quick exercise to illustrate how a sticky scroll to top button works. It includes responsive CSS and HTML, so it also adapts to your viewport and device.</p>
</div>
</div>
<!-- /container -->
<button class="btn btn-primary scroll-top" data-scroll="up" type="button">
<i class="fa fa-chevron-up"></i>
</button>

Step 2

Add the CSS below to the main stylesheet of your website

.scroll-top {
width: 75px;
height: 75px;
position: fixed;
bottom: 25px;
left: 20px;
display: none;
}
.scroll-top i {
display: inline-block;
color: #FFFFFF;
}
.navbar-brand>img {
padding-top: 11px;
width: 130px;
margin-left: 60px;
}
.navbar-brand {
height: auto;
margin: 0;
padding: 0;
margin-right: 20px;
}
.navbar {
background-color: #000000;
}
.navbar-default .navbar-nav > .active > a {
padding: 8px 19px 9px !important;
}
.navbar-nav > li.active {
padding: 8px 0px 9px 0;
}
.navbar-right {
padding-top: 0;
}
.navbar-default .navbar-nav > li > a::after {
background-color: transparent;
border-bottom: 3px solid #d2282e;
}
.navbar-default .navbar-nav>li {
display: inline-block;
text-align: center;
float: none;
}
.navbar-default .navbar-nav>li>a {
color: #fff;
}
.navbar-default .navbar-nav>li>a:hover {
color: #fff;
background-color: #0392CC;
}

Step 3

Add the code below to a file called scroll.js

$(document).ready(function () {
$(window).scroll(function () {
if ($(this).scrollTop() > 100) {
$('.scroll-top').fadeIn();
} else {
$('.scroll-top').fadeOut();
}
});
$('.scroll-top').click(function () {
$("html, body").animate({
scrollTop: 0
}, 100);
return false;
});
});

Step 4

Add the includes below to the pages where your button lives

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="scroll.js"></script>

Demo on JSFiddle

Download from GitHub

Originally Posted here on the Solodev Web Design Blog

To learn more about the power of the Solodev Web Design Platform visit solodev.com/features/!