Skip to content

Instantly share code, notes, and snippets.

@noahrc
Created March 6, 2013 22:50
Show Gist options
  • Save noahrc/5103933 to your computer and use it in GitHub Desktop.
Save noahrc/5103933 to your computer and use it in GitHub Desktop.
Animate transition to anchor links
/* Animate transitions to links on the same page */
var ANIMATEANCHORS = {}
ANIMATEANCHORS.initialize = function() {
$('a[href*=#]').not('.fancybox').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) +']')
var targetOffset = $target.length ? $target.offset().top - 20 : 0 // Offset the target distance to have some margin at the top
$('html, body').animate({scrollTop: targetOffset}, 700)
return false // prevent default
}
});
}
ANIMATEANCHORS.initialize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment