Back to top using css and jquery with example

Create back to top option dynamically on scroll change for long pages. No plugin and complex integration, just add small script process code and use it in page. Back to top option created using CSS and jQuery.

Back to top is suitable for website with lots of page content. Once user scroll page, back to top option display on right bottom area. On click it page will smoothly scroll to top of page and hide option.

Adding Back to top process code

Back to top process code is small code snippets that will create dynamic top option and bind its event for scroll page to top. Just add code anywhere in page using script tag.

var backToTop = {
    set: function(){
      var sTopHtml= '<div class="s-top s-none">Top</div>';

      if($('body').scrollTop() > 40)

      $('body').on('click', '.s-top', function(){
        $('html, body').animate({scrollTop: $('body').offset().top}, 600);
    scroll: function(){
      if($(window).scrollTop() > 40){
      }else if($('body').scrollTop() < 40){
        if($('.s-none').length == 0)

Use Back to top process code

For activate back to top process code, we need to add below code in document ready function. Add backToTop.set() for create html top button option and bind its event. For hide show button on scroll change need to add backToTop.scoll() code in window scroll event.



Style Back to top option

For set back to top option bottom right ‘.s-top’ class used. Add below style code in css file or in page with style tag. Change back to top button style from ‘.s-top’ class to match with page design.