[Solved] JQuery issue with a back to top function


You should edit your CSS to hide your “Back to Top” button by default, and then show it when the show class is added.

#toTop {
    display: none;
    background-color: #FF9800;
    width: 50px;
    height: 50px;
    text-align: center;
    border-radius: 4px;
    margin: 30px;
    position: fixed;
    bottom: 30px;
    right: 30px;
    transition: background-color .3s;
    z-index: 1000;
}
#toTop.show {
    display: inline-block;
}

jQuery(document).ready(function() {
    var btn = $('#toTop');

    $(window).scroll(function() {
        if ($(window).scrollTop() > 300) {
            btn.addClass('show');
        } else {
            btn.removeClass('show');
        }
    });

    btn.on('click', function(e) {
        e.preventDefault();
        $('html, body').animate({
          scrollTop: 0
        }, 700);
    });
});
.sectionA {
  width: 100%;
  height: 600px;
  background-color: pink;
}

.sectionB {
  width: 100%;
  height: 600px;
  background-color: green;
}

.sectionC {
  width: 100%;
  height: 600px;
  background-color: purple;
}

.sectionD {
  width: 100%;
  height: 600px;
  background-color: orange;
}

#toTop {
    display: none;
    background-color: #FF9800;
    width: 50px;
    height: 50px;
    text-align: center;
    border-radius: 4px;
    margin: 30px;
    position: fixed;
    bottom: 30px;
    right: 30px;
    transition: background-color .3s;
    z-index: 1000;
}
#toTop.show {
    display: inline-block;
}
#toTop:hover {
    cursor: pointer;
    background-color: #333;
}
#toTop:active {
    background-color: #555;
}
#toTop::after {
    content: "\f077";
    font-family: FontAwesome;
    font-weight: normal;
    font-style: normal;
    font-size: 2em;
    line-height: 50px;
    color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<p style="position:fixed">Scroll Down and use the toTop button</p>
<br>
<p style="position:fixed">
At the top, the "Back to Top" button does not show.
<b>It works!</b>
</p>
<section class="sectionA">

</section>
<section class="sectionB">

</section>
<section class="sectionC">

</section>
<section class="sectionD">

</section>
</body>
<a id="toTop"></a>

0

solved JQuery issue with a back to top function