Animated Scroll to Top with jQuery

Today I am going to show you how you can create a smooth animated scroll to top with jQuery. It is very easy and we need to code only couple of lines to implement this. We need to code for

  1. Hide button on load
  2. If user scroll more than 400px then show the button
  3. If user go to top then hide the button
  4. Scroll to top with Animate method

Let's go to implement it..

Download Source

Live Demo

HTML Part
#back-top is the scroll top button. #top is added to the body tag for no JavaScript fallback.

<html>
<head>
  <title>Scroll to top</title>
<head>
<body id="top">
  <div class="wrap"> <!--main div-->
  <!-- contents here -->
  </div>
  <div id="back-top"><!-- scroll top button -->
    <a href="#top"><span></span>Back to Top</a>
  </div>
</body>
</html>

CSS Part
#back-top position is fixed 30px bottom and 100px right, top.png as background and transition is 1 second.
/*
Back to top button 
*/
#back-top {
  position: fixed;
  bottom: 30px;
  right: 100px;
}
#back-top a {
  width: 64px;
  display: block;
  text-align: center;
  font: 11px/100% Arial, Helvetica, sans-serif;
  text-transform: uppercase;
  text-decoration: none;
  color: #bbb;
  /* background color transition */
  -webkit-transition: 1s;
  -moz-transition: 1s;
  transition: 1s;
}
#back-top a:hover {
  color: #000;
}
/* arrow icon (span tag) */
#back-top span {
  width: 64px;
  height: 64px;
  display: block;
  margin-bottom: 7px;
  background: #ddd url(top.png) no-repeat center center;
  /* rounded corners */
  -webkit-border-radius: 15px;
  -moz-border-radius: 15px;
  border-radius: 15px;
  /* background color transition */
  -webkit-transition: 1s;
  -moz-transition: 1s;
  transition: 1s;
}
#back-top a:hover span {
  background-color: #fff;
}
#back{
  font-weight: bold;
  font-size: 22px;
  padding:10px;
  border: 2px solid #333;
  border-radius: 29px;
}
jQuery Part
$(function() {
  // cache scroll to top button
  var b = $('#back-top');
  // Hide scroll top button
  b.hide();
  // FadeIn or FadeOut scroll to top button on scroll
  $(window).on('scroll', function(){
    // if you scroll more then 400px then fadein goto top button
    if ($(this).scrollTop() > 400) {
      b.fadeIn();
    // otherwise fadeout button
    } else {
      b.fadeOut();
    }
  });
  // Animated scroll to top
  b.on('click', function(){
    $('html,body').animate({
      scrollTop: 0
    }, 500 );
    return false;
  });
});

3 comments:

  1. Great and helpful tutorial. I am using this , This is also simple
    $(document).ready(function(e) {
    var a = 400,
    t = 1200,
    l = 700,
    s = e(".scrool-top");
    e(window).scroll(function() {
    e(this).scrollTop() > a ? s.addClass("scrool-is-visible") : s.removeClass("scrool-is-visible scrool-fade-out"), e(this).scrollTop() > t && s.addClass("scrool-fade-out")
    }), s.on("click", function(a) {
    a.preventDefault(), e("body,html").animate({
    scrollTop: 0
    }, l)
    })
    })
    Animated Scroll To Top Using jQuery

    ReplyDelete

Go to top