Skip to content

Instantly share code, notes, and snippets.

@patotoma
Last active Aug 29, 2015
Embed
What would you like to do?
Smooth Scroll with jQuery
$.fn.smoothscroll = function(offset, speed){
$(this).find('a').on('click', function(e){
e.preventDefault();
$('html,body').animate({scrollTop: $(this.hash).offset().top - offset}, speed);
});
};

Smooth Scroll with jQuery

$.fn.smoothscroll = function(offset, speed){
  $(this).find('a').on('click', function(e){
    e.preventDefault();
      $('html,body').animate({scrollTop: $(this.hash).offset().top - offset}, speed);
  });
};

How to use:

  • Download smoothscroll.js or smoothscroll_min.js(minified version).
  • Include the file in your html.
  • Use like following:
$(document).ready(function(){
  $('nav').smoothscroll(0, 1000);
});

You can adjust the values of offset and speed to suit your needs!

Working fiddle

If you have any questions or innovations please leave me a comment.

patriktoma.studenthosting.sk

$.fn.smoothscroll=function(a,b){$(this).find("a").on("click",function(c){c.preventDefault();$("html,body").animate({scrollTop:$(this.hash).offset().top-a},b)})};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment