Posted By

j4v3d on 06/16/16


Tagged

scroll jquery animation smooth


Versions (?)

Smooth Scroll on Anchor Links


 / Published in: jQuery
 

Performs a smooth page scroll to an anchor on the same page.

  1. jQuery(function() {
  2. // This will select everything with the class smoothScroll
  3. // This should prevent problems with carousel, scrollspy, etc...
  4. jQuery('.smoothScroll').click(function() {
  5. if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
  6. var target = jQuery(this.hash);
  7. target = target.length ? target : jQuery('[name=' + this.hash.slice(1) + ']');
  8. if (target.length) {
  9. jQuery('html,body').animate({
  10. scrollTop: target.offset().top
  11. }, 1000); // The number here represents the speed of the scroll in milliseconds
  12. return false;
  13. }
  14. }
  15. });
  16. });

Report this snippet  

You need to login to post a comment.