Smooth scrolling for internal links to anchors in the same page with jQuery

Javascript

/* 
 * Smooth scrolling for any links that point to an anchor (e.g. <a href="#anchor">)
 */
$("a[href^='#']").on("click", function(e) {
  e.preventDefault();
  // Offset: use this only if you have (for example) a fixed header
  var offset = $("#fixed-header").height();

  return $("html, body").animate({
    scrollTop: $(this.hash).offset().top - offset
  }, 300);
});
  • Madi

    Nice one!

Categories

Category BootstrapCategory CoffeescriptCategory DrupalCategory GravCategory HTMLCategory JavascriptCategory JoomlaCategory jQueryCategory LaravelCategory MagentoCategory PHPCategory SharePointCategory SpringCategory ThymeleafCategory WordPressCategory Workflow

Comments

Developed and designed by Netgloo
© 2019 Netgloo