JavaScript Example | Create a Scroll Back to Top of the Page Button

This javascript example code will tell you how to create a scroll back to top of the page button.

1. Create a button

<a id="back2Top" title="Back to top" href="#">&#10148;</a>

2. Create css stylesheet

#back2Top {
    width: 40px;
    line-height: 40px;
    overflow: hidden;
    z-index: 999;
    display: none;
    cursor: pointer;
    -moz-transform: rotate(270deg);
    -webkit-transform: rotate(270deg);
    -o-transform: rotate(270deg);
    -ms-transform: rotate(270deg);
    transform: rotate(270deg);
    position: fixed;
    bottom: 50px;
    right: 0;
    background-color: #DDD;
    color: #555;
    text-align: center;
    font-size: 30px;
    text-decoration: none;
#back2Top:hover {
    background-color: #DDF;
    color: #000;

3. create javascript code

/*Scroll to top when arrow up clicked BEGIN*/
$(window).scroll(function() {
    var height = $(window).scrollTop();
    if (height > 100) {
    } else {
$(document).ready(function() {
    $("#back2Top").click(function(event) {
        $("html, body").animate({ scrollTop: 0 }, "slow");
        return false;

 /*Scroll to top when arrow up clicked END*/