Created
March 6, 2013 22:50
-
-
Save noahrc/5103933 to your computer and use it in GitHub Desktop.
Animate transition to anchor links
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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