Smooth Scrolling - Back to Top or any other Anchor

by davidxmartins

HTML

<span id="back-to-top"></span>
<h2>
The advantage of this aproach when compared to<br>
"html { scroll-behavior: smooth; }"<br>
is that it's cross browser supported, while <br>
"html { scroll-behavior: smooth; }"<br>
is not supported by Safary or Internet-Explorer.
</h2>




<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<a href="#back-to-top" class="backtotop__container">
  <div class="backtotop__arrow"></div>
</a>

<script>
$(document).ready(function(){
  $('a[href^="#"]').on('click',function (e) {
    e.preventDefault();

    var target = this.hash;
    var $target = $(target);

    $('html, body').stop().animate({
      'scrollTop': $target.offset().top
    }, 900, 'swing', function () {
      window.location.hash = target;
    });
  });
});
  </script>

CSS

html {
  font-family: sans-serif;
}

* {
  margin: 0;
  padding: 0;
  border: 0;
  line-height: 1;
  box-sizing: border-box;
}

body {
  height: 2000px;
  background-color: #e5e5f7;
  opacity: 0.8;
  background-image: linear-gradient(#ccc 1px, transparent 1px), linear-gradient(to right, #ccc 1px, #e5e5f7 1px);
  background-size: 20px 20px;
}




/* ARROW - Back to Top  */

.backtotop__container {
  height: 80px;
  width: 60px;
  border-radius: 10px;
  text-align: center;
  display: inline-block;
  transition: all 0.3s ease-in-out;
  position: fixed;
  bottom: 20px;
  right: 20px;
}

.backtotop__arrow {
  height: 50px;
  width: 2px;
  position: relative;
  background: green;
  display: inline-block;
  top: 50%;
  transform: translateY(-50%);
  transition: height 0.3s ease-in-out;
}

.backtotop__arrow:before,
.backtotop__arrow:after {
  content: "";
  position: absolute;
  width: 2px;
  height: 35px;
  background: green;
  top: 0;
  transform-origin: center top;
}

.backtotop__arrow:before {
  left: 0;
  transform: rotate(45deg);
}

.backtotop__arrow:after {
  left: 0;
  transform: rotate(-45deg);
}

.backtotop__container:hover .backtotop__arrow {
  height: 70px;
}