Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save generatepress/60b9235b43866e346320e11cf40bb054 to your computer and use it in GitHub Desktop.
Save generatepress/60b9235b43866e346320e11cf40bb054 to your computer and use it in GitHub Desktop.
Add smooth scroll to anchor links. Just add the "smooth" class to your links: <a class="smooth" href="#my-anchor">My Anchor</a>
<script>
jQuery(function($) {
$('a.smooth[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 500);
return false;
}
}
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment