How to set smoth scroll in website

Hello friends, want to set smoth scroll on your one page template. It's so easy. Just past this code in your website:




  1. this is a jQuery code. so you should keep in script tag. such as <script> <script>.
  2. Past this code  before footer section.

  3. $(document).ready(function(){
  4.   $('a[href*=#]').click(function() {
  5.     if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
  6.     && location.hostname == this.hostname) {
  7.       var $target = $(this.hash);
  8.       $target = $target.length && $target
  9.       || $('[name=' + this.hash.slice(1) +']');
  10.       if ($target.length) {
  11.         var targetOffset = $target.offset().top;
  12.         $('html,body')
  13.         .animate({scrollTop: targetOffset}, 1000);
  14.        return false;
  15.       }
  16.     }
  17.   });
  18. });




Full code with script tag:


<script>
  1. $(document).ready(function(){
  2.   $('a[href*=#]').click(function() {
  3.     if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
  4.     && location.hostname == this.hostname) {
  5.       var $target = $(this.hash);
  6.       $target = $target.length && $target
  7.       || $('[name=' + this.hash.slice(1) +']');
  8.       if ($target.length) {
  9.         var targetOffset = $target.offset().top;
  10.         $('html,body')
  11.         .animate({scrollTop: targetOffset}, 1000);
  12.        return false;
  13.       }
  14.     }
  15.   });
  16. });

</script>



Comments